1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
from concurrent.futures import ThreadPoolExecutor
import os
from threading import Event
import time
from unittest.mock import Mock
from unittest.mock import patch
import pytest
from dogpile.cache.region import _backend_loader
from dogpile.testing import eq_
from dogpile.testing.fixtures import _GenericBackendFixture
from dogpile.testing.fixtures import _GenericBackendTestSuite
from dogpile.testing.fixtures import _GenericMutexTestSuite
from dogpile.testing.fixtures import _GenericSerializerTestSuite
REDIS_HOST = "127.0.0.1"
REDIS_PORT = int(os.getenv("DOGPILE_REDIS_PORT", "6379"))
expect_redis_running = os.getenv("DOGPILE_REDIS_PORT") is not None
class _TestRedisConn:
@classmethod
def _check_backend_available(cls, backend):
try:
backend.set_serialized("x", b"y")
assert backend.get_serialized("x") == b"y"
backend.delete("x")
except Exception:
if not expect_redis_running:
pytest.skip(
"redis is not running or "
"otherwise not functioning correctly"
)
else:
raise
class RedisTest(_TestRedisConn, _GenericBackendTestSuite):
backend = "dogpile.cache.redis"
config_args = {
"arguments": {
"host": REDIS_HOST,
"port": REDIS_PORT,
"db": 0,
"foo": "barf",
}
}
class RedisSerializerTest(_GenericSerializerTestSuite, RedisTest):
pass
class RedisDistributedMutexTest(_TestRedisConn, _GenericMutexTestSuite):
backend = "dogpile.cache.redis"
config_args = {
"arguments": {
"host": REDIS_HOST,
"port": REDIS_PORT,
"db": 0,
"distributed_lock": True,
}
}
class RedisAsyncCreationTest(_TestRedisConn, _GenericBackendFixture):
backend = "dogpile.cache.redis"
config_args = {
"arguments": {
"host": REDIS_HOST,
"port": REDIS_PORT,
"db": 0,
"distributed_lock": True,
# This is the important bit:
"thread_local_lock": False,
}
}
def test_distributed_async_locks(self):
pool = ThreadPoolExecutor(max_workers=1)
ev = Event()
# A simple example of how people may implement an async runner -
# plugged into a thread pool executor.
def asyncer(cache, key, creator, mutex):
def _call():
try:
value = creator()
cache.set(key, value)
finally:
# If a thread-local lock is used here, this will fail
# because generally the async calls run in a different
# thread (that's the point of async creators).
try:
mutex.release()
except Exception:
pass
else:
ev.set()
return pool.submit(_call)
reg = self._region(
region_args={"async_creation_runner": asyncer},
config_args={"expiration_time": 0.1},
)
@reg.cache_on_arguments()
def blah(k):
return k * 2
# First call adds to the cache without calling the async creator.
eq_(blah("asd"), "asdasd")
# Wait long enough to cause the cached value to get stale.
time.sleep(0.3)
# This will trigger the async runner and return the stale value.
eq_(blah("asd"), "asdasd")
# Wait for the the async runner to finish or timeout. If the mutex
# release errored, then the event won't be set and we'll timeout.
# On <= Python 3.1, wait returned nothing. So check is_set after.
ev.wait(timeout=1.0)
eq_(ev.is_set(), True)
@patch("redis.StrictRedis", autospec=True)
class RedisConnectionTest:
backend = "dogpile.cache.redis"
@classmethod
def setup_class(cls):
cls.backend_cls = _backend_loader.load(cls.backend)
try:
cls.backend_cls({})
except ImportError:
pytest.skip("Backend %s not installed" % cls.backend)
def _test_helper(self, mock_obj, expected_args, connection_args=None):
if connection_args is None:
connection_args = expected_args
self.backend_cls(connection_args)
mock_obj.assert_called_once_with(**expected_args)
def test_connect_with_defaults(self, MockStrictRedis):
# The defaults, used if keys are missing from the arguments dict.
arguments = {
"host": "localhost",
"port": 6379,
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_basics(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"port": 6379,
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_password(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"password": "some password",
"port": 6379,
"db": 0,
}
expected = arguments.copy()
expected.update(
{
"username": None,
}
)
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_username_and_password(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"username": "redis",
"password": "some password",
"port": 6379,
"db": 0,
}
self._test_helper(MockStrictRedis, arguments)
def test_connect_with_socket_timeout(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"port": 6379,
"socket_timeout": 0.5,
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_socket_connect_timeout(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"port": 6379,
"socket_timeout": 1.0,
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_socket_keepalive(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"port": 6379,
"socket_keepalive": True,
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_socket_keepalive_options(self, MockStrictRedis):
arguments = {
"host": "127.0.0.1",
"port": 6379,
"socket_keepalive": True,
# 4 = socket.TCP_KEEPIDLE
"socket_keepalive_options": {4, 10.0},
"db": 0,
}
expected = arguments.copy()
expected.update({"username": None, "password": None})
self._test_helper(MockStrictRedis, expected, arguments)
def test_connect_with_connection_pool(self, MockStrictRedis):
pool = Mock()
arguments = {"connection_pool": pool, "socket_timeout": 0.5}
expected_args = {"connection_pool": pool}
self._test_helper(
MockStrictRedis, expected_args, connection_args=arguments
)
def test_connect_with_url(self, MockStrictRedis):
arguments = {"url": "redis://redis:password@127.0.0.1:6379/0"}
self._test_helper(MockStrictRedis.from_url, arguments)
def test_extra_arbitrary_args(self, MockStrictRedis):
arguments = {
"url": "redis://redis:password@127.0.0.1:6379/0",
"connection_kwargs": {
"ssl": True,
"encoding": "utf-8",
"new_redis_arg": 50,
},
}
self._test_helper(
MockStrictRedis.from_url,
{
"url": "redis://redis:password@127.0.0.1:6379/0",
"ssl": True,
"encoding": "utf-8",
"new_redis_arg": 50,
},
arguments,
)
|