-
Notifications
You must be signed in to change notification settings - Fork 157
/
test_lock.py
279 lines (210 loc) · 9.83 KB
/
test_lock.py
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
271
272
273
274
275
276
277
278
279
import asyncio
import pytest
from aiocache.lock import OptimisticLock, OptimisticLockError, RedLock
from aiocache.serializers import StringSerializer
from ..utils import KEY_LOCK, Keys
@pytest.fixture
def lock(cache):
return RedLock(cache, Keys.KEY, 20)
def build_key(key, namespace=None):
return "custom_key"
def build_key_bytes(key, namespace=None):
return b"custom_key"
@pytest.fixture
def custom_redis_cache(mocker, redis_cache, build_key=build_key):
mocker.patch.object(redis_cache, "build_key", new=build_key)
yield redis_cache
@pytest.fixture
def custom_memory_cache(mocker, memory_cache, build_key=build_key):
mocker.patch.object(memory_cache, "build_key", new=build_key)
yield memory_cache
@pytest.fixture
def custom_memcached_cache(mocker, memcached_cache, build_key=build_key_bytes):
mocker.patch.object(memcached_cache, "build_key", new=build_key)
yield memcached_cache
class TestRedLock:
async def test_acquire(self, cache, lock):
cache.serializer = StringSerializer()
async with lock:
assert await cache.get(KEY_LOCK) == lock._value
async def test_release_does_nothing_when_no_lock(self, lock):
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_acquire_release(self, cache, lock):
async with lock:
pass
assert await cache.get(KEY_LOCK) is None
async def test_locking_dogpile(self, mocker, cache):
mocker.spy(cache, "get")
mocker.spy(cache, "set")
mocker.spy(cache, "_add")
async def dummy():
res = await cache.get(Keys.KEY)
assert res is None
async with RedLock(cache, Keys.KEY, lease=5):
res = await cache.get(Keys.KEY)
if res is not None:
return
await asyncio.sleep(0.1)
await cache.set(Keys.KEY, "value")
await asyncio.gather(dummy(), dummy(), dummy(), dummy())
assert cache._add.call_count == 4
assert cache.get.call_count == 8
assert cache.set.call_count == 1, cache.set.call_args_list
async def test_locking_dogpile_lease_expiration(self, cache):
async def dummy() -> None:
res = await cache.get(Keys.KEY)
assert res is None
# Lease should expire before cache is set, so res is still None.
async with RedLock(cache, Keys.KEY, lease=1):
res = await cache.get(Keys.KEY)
assert res is None
await asyncio.sleep(1.1)
await cache.set(Keys.KEY, "value")
await asyncio.gather(dummy(), dummy(), dummy(), dummy())
async def test_locking_dogpile_propagates_exceptions(self, cache):
async def dummy():
async with RedLock(cache, Keys.KEY, lease=1):
raise ValueError()
with pytest.raises(ValueError):
await dummy()
class TestMemoryRedLock:
@pytest.fixture
def lock(self, memory_cache):
return RedLock(memory_cache, Keys.KEY, 20)
async def test_acquire_key_builder(self, custom_memory_cache, lock):
async with lock:
assert await custom_memory_cache.get(KEY_LOCK) == lock._value
async def test_acquire_release_key_builder(self, custom_memory_cache, lock):
async with lock:
assert await custom_memory_cache.get(KEY_LOCK) is not None
assert await custom_memory_cache.get(KEY_LOCK) is None
async def test_release_wrong_token_fails(self, lock):
await lock.__aenter__()
lock._value = "random"
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_release_wrong_client_fails(self, memory_cache, lock):
wrong_lock = RedLock(memory_cache, Keys.KEY, 20)
await lock.__aenter__()
assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_float_lease(self, memory_cache):
lock = RedLock(memory_cache, Keys.KEY, 0.1)
await lock.__aenter__()
await asyncio.sleep(0.2)
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
@pytest.mark.redis
class TestRedisRedLock:
@pytest.fixture
def lock(self, redis_cache):
return RedLock(redis_cache, Keys.KEY, 20)
async def test_acquire_key_builder(self, custom_redis_cache, lock):
custom_redis_cache.serializer = StringSerializer()
async with lock:
assert await custom_redis_cache.get(KEY_LOCK) == lock._value
async def test_acquire_release_key_builder(self, custom_redis_cache, lock):
custom_redis_cache.serializer = StringSerializer()
async with lock:
assert await custom_redis_cache.get(KEY_LOCK) is not None
assert await custom_redis_cache.get(KEY_LOCK) is None
async def test_release_wrong_token_fails(self, lock):
await lock.__aenter__()
lock._value = "random"
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_release_wrong_client_fails(self, redis_cache, lock):
wrong_lock = RedLock(redis_cache, Keys.KEY, 20)
await lock.__aenter__()
assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_float_lease(self, redis_cache):
lock = RedLock(redis_cache, Keys.KEY, 0.1)
await lock.__aenter__()
await asyncio.sleep(0.2)
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
@pytest.mark.memcached
class TestMemcachedRedLock:
@pytest.fixture
def lock(self, memcached_cache):
return RedLock(memcached_cache, Keys.KEY, 20)
async def test_acquire_key_builder(self, custom_memcached_cache, lock):
custom_memcached_cache.serializer = StringSerializer()
async with lock:
assert await custom_memcached_cache.get(KEY_LOCK) == lock._value
async def test_acquire_release_key_builder(self, custom_memcached_cache, lock):
custom_memcached_cache.serializer = StringSerializer()
async with lock:
assert await custom_memcached_cache.get(KEY_LOCK) is not None
assert await custom_memcached_cache.get(KEY_LOCK) is None
async def test_release_wrong_token_succeeds_meh(self, lock):
await lock.__aenter__()
lock._value = "random"
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_release_wrong_client_succeeds_meh(self, memcached_cache, lock):
wrong_lock = RedLock(memcached_cache, Keys.KEY, 20)
await lock.__aenter__()
assert await wrong_lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_float_lease(self, memcached_cache):
lock = RedLock(memcached_cache, Keys.KEY, 0.1)
with pytest.raises(TypeError):
await lock.__aenter__()
class TestOptimisticLock:
@pytest.fixture
def lock(self, cache):
return OptimisticLock(cache, Keys.KEY)
async def test_acquire(self, cache, lock):
await cache.set(Keys.KEY, "value")
async with lock:
assert lock._token == await cache._gets(cache.build_key(Keys.KEY))
async def test_release_does_nothing(self, lock):
assert await lock.__aexit__("exc_type", "exc_value", "traceback") is None
async def test_check_and_set_not_existing_never_fails(self, cache, lock):
async with lock as locked:
await cache.set(Keys.KEY, "conflicting_value")
await locked.cas("value")
assert await cache.get(Keys.KEY) == "value"
async def test_check_and_set(self, cache, lock):
await cache.set(Keys.KEY, "previous_value")
async with lock as locked:
await locked.cas("value")
assert await cache.get(Keys.KEY) == "value"
async def test_check_and_set_fail(self, cache, lock):
await cache.set(Keys.KEY, "previous_value")
with pytest.raises(OptimisticLockError):
async with lock as locked:
await cache.set(Keys.KEY, "conflicting_value")
await locked.cas("value")
async def test_check_and_set_with_int_ttl(self, cache, lock):
await cache.set(Keys.KEY, "previous_value")
async with lock as locked:
await locked.cas("value", ttl=1)
await asyncio.sleep(1)
assert await cache.get(Keys.KEY) is None
class TestMemoryOptimisticLock:
@pytest.fixture
def lock(self, memory_cache):
return OptimisticLock(memory_cache, Keys.KEY)
async def test_acquire_key_builder(self, custom_memory_cache, lock):
await custom_memory_cache.set(Keys.KEY, "value")
async with lock:
assert await custom_memory_cache.get(KEY_LOCK) == lock._token
await custom_memory_cache.delete(Keys.KEY, "value")
async def test_check_and_set_with_float_ttl(self, memory_cache, lock):
await memory_cache.set(Keys.KEY, "previous_value")
async with lock as locked:
await locked.cas("value", ttl=0.1)
await asyncio.sleep(1)
assert await memory_cache.get(Keys.KEY) is None
@pytest.mark.redis
class TestRedisOptimisticLock:
@pytest.fixture
def lock(self, redis_cache):
return OptimisticLock(redis_cache, Keys.KEY)
async def test_acquire_key_builder(self, custom_redis_cache, lock):
custom_redis_cache.serializer = StringSerializer()
await custom_redis_cache.set(Keys.KEY, "value")
async with lock:
assert await custom_redis_cache.get(KEY_LOCK) == lock._token
await custom_redis_cache.delete(Keys.KEY, "value")
async def test_check_and_set_with_float_ttl(self, redis_cache, lock):
await redis_cache.set(Keys.KEY, "previous_value")
async with lock as locked:
await locked.cas("value", ttl=0.1)
await asyncio.sleep(1)
assert await redis_cache.get(Keys.KEY) is None