forked from youknowone/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_test_func_asyncio.py
465 lines (359 loc) · 10.5 KB
/
_test_func_asyncio.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import asyncio
import time
import sys
import shelve
import aiomcache
import diskcache
import ring
import pytest
from tests.test_func_sync import StorageDict
@pytest.fixture()
def storage_dict():
storage = StorageDict()
return storage, ring.dict
@pytest.fixture()
def aiomcache_client():
client = aiomcache.Client('127.0.0.1', 11211)
return client, ring.func.asyncio.aiomcache
@pytest.fixture()
def aioredis_pool():
if sys.version_info >= (3, 5):
import aioredis
pool_coroutine = aioredis.create_redis_pool(
('localhost', 6379), minsize=2, maxsize=2)
return pool_coroutine, ring.aioredis
else:
pytest.skip()
@pytest.fixture(params=[
pytest.lazy_fixture('storage_dict'),
pytest.lazy_fixture('aiomcache_client'),
pytest.lazy_fixture('aioredis_pool'),
])
def storage_and_ring(request):
return request.param
@pytest.fixture()
def storage_shelve():
storage = shelve.open('/tmp/ring-test/shelvea')
return storage, ring.shelve
@pytest.fixture()
def storage_disk(request):
client = diskcache.Cache('/tmp/ring-test/diskcache')
return client, ring.disk
@pytest.fixture(params=[
pytest.lazy_fixture('storage_shelve'),
pytest.lazy_fixture('storage_disk'),
])
def synchronous_storage_and_ring(request):
return request.param
@pytest.mark.asyncio
@asyncio.coroutine
def test_singleton_proxy():
@asyncio.coroutine
def client():
return object()
assert ((yield from client())) is not ((yield from client()))
proxy = ring.func.asyncio.SingletonCoroutineProxy(client())
assert ((yield from proxy)) is ((yield from proxy))
@pytest.mark.asyncio
@asyncio.coroutine
def test_vanilla_function(aiomcache_client):
storage, storage_ring = aiomcache_client
with pytest.raises(TypeError):
@storage_ring(storage)
def vanilla_function():
pass
@pytest.mark.asyncio
@asyncio.coroutine
def test_common(storage_and_ring):
storage, storage_ring = storage_and_ring
base = [0]
@storage_ring(storage, 'ring-test !@#', 5)
@asyncio.coroutine
def f(a, b):
return str(base[0] + a * 100 + b).encode()
# `f` is a callable with argument `a` and `b`
# test f is correct
if asyncio.iscoroutine(storage):
s1 = yield from f.storage.backend
with pytest.raises(RuntimeError):
yield from storage
s2 = yield from f.storage.backend
assert s1 is s2
else:
assert f.storage.backend is storage
assert f.key(a=0, b=0) # f takes a, b
assert base[0] is not None # f has attr base for test
assert ((yield from f.execute(a=1, b=2))) != ((yield from f.execute(a=1, b=3))) # f is not singular
assert ((yield from f.execute(a=2, b=2))) != ((yield from f.execute(a=1, b=2))) # f is not singular
r = yield from f.execute(0, 0)
base[0] += 1
assert r != ((yield from f.execute(0, 0))) # base has side effect
yield from f.delete(1, 2) # delete sample cache
# test: parametrized key build
assert f.key(1, 2) == f.key(1, b=2) == f.key(a=1, b=2)
assert f.key(1, 2) != f.key(1, 3)
# set base
base[0] = 10000
# test: 'get' 'execute' 'delete' 'get_or_update'
rn = yield from f.get(1, 2)
assert rn is None, (rn, f.key(1, 2)) # not cached yet
r1 = yield from f.execute(1, 2) # run without cache
r2 = yield from f(1, 2)
assert r1 == r2, (r1, r2) # create and return cache
assert ((yield from f.get(1, 2))) == ((yield from f(a=1, b=2))) # cached now
yield from f.delete(b=2, a=1) # delete cache
assert ((yield from f.get(1, 2))) is None # of course get fails
assert r1 == ((yield from f.get_or_update(1, 2))) # this is equivalent to call the func
# reset base
base[0] = 20000
# test: actually cached or not
r3 = yield from f.execute(1, 2)
assert r1 != r3 # base has side effect
assert r1 == ((yield from f(1, 2))) # still cached
assert r3 != ((yield from f(1, 2)))
# test: 'update'
assert r3 == ((yield from f.update(1, 2))) # immediate update
yield from f.touch(1, 2) # just a running test
yield from f.touch(0, 0) # illegal access
yield from f.set(b'RANDOMVALUE', 1, 2)
rset = yield from f.get(1, 2)
assert rset == b'RANDOMVALUE'
yield from f.delete(1, 2) # finallize
@pytest.mark.asyncio
@asyncio.coroutine
def test_complicated_key(storage_and_ring):
storage, storage_ring = storage_and_ring
@storage_ring(storage)
@asyncio.coroutine
def complicated(a, *args, b, **kw):
return b'42'
# set
v1 = yield from complicated(0, 1, 2, 3, b=4, c=5, d=6)
v2 = yield from complicated.get(0, 1, 2, 3, b=4, c=5, d=6)
assert v1 == v2, (v1, v2)
@pytest.mark.asyncio
@asyncio.coroutine
def test_func_dict():
cache = {}
@ring.dict(cache)
@asyncio.coroutine
def f1(a, b):
return a * 100 + b
r = yield from f1.has(1, 2)
assert r is False
r = yield from f1(1, 2)
assert r == 102
r = yield from f1(1, 2)
assert r == 102
r = yield from f1.has(1, 2)
assert r is True
with pytest.raises(AttributeError):
yield from f1.touch(1, 2)
cache = {}
@ring.dict(cache, expire=1)
@asyncio.coroutine
def f2(a, b):
return a * 100 + b
yield from f2(1, 2)
yield from f2(1, 2)
yield from f2.touch(1, 2)
f2._rope.storage.now = lambda: time.time() + 100 # expirable duration
assert ((yield from f2.get(1, 2))) is None
@pytest.mark.asyncio
@asyncio.coroutine
def test_many(aiomcache_client):
client, _ = aiomcache_client
@ring.aiomcache(client)
@asyncio.coroutine
def f(a):
return 't{}'.format(a).encode()
r = yield from f.execute_many(
(1,),
{'a': 2},
)
assert r == [b't1', b't2']
with pytest.raises(TypeError):
yield from f.execute_many(
[1],
)
@pytest.mark.asyncio
@asyncio.coroutine
def test_aiomcache(aiomcache_client):
client, _ = aiomcache_client
@ring.aiomcache(client)
@asyncio.coroutine
def f(a):
return 't{}'.format(a).encode()
yield from f.delete(1)
yield from f(1)
yield from f.touch(1)
r = yield from f.get_many(
(1,),
{'a': 2},
)
assert r == [b't1', None]
with pytest.raises(AttributeError):
yield from f.has(1)
with pytest.raises(NotImplementedError):
yield from f.update_many()
with pytest.raises(NotImplementedError):
yield from f.delete_many()
with pytest.raises(AttributeError):
yield from f.has_many()
with pytest.raises(AttributeError):
yield from f.touch_many()
@pytest.mark.parametrize('expire', [
1,
None,
])
@pytest.mark.asyncio
@asyncio.coroutine
def test_aioredis(aioredis_pool, expire):
client, _ = aioredis_pool
@ring.aioredis(client, expire=expire)
@asyncio.coroutine
def f(a):
return 't{}'.format(a).encode()
yield from f.delete(1)
yield from f.delete(2)
yield from f.delete(3)
r = yield from f.get(1)
assert r is None
r = yield from f.has(1)
assert r is False
yield from f(1)
r = yield from f.has(1)
assert r is True
if expire is None:
with pytest.raises(TypeError):
yield from f.touch(1)
else:
yield from f.touch(1)
# _many
r = yield from f.get_many(
(1,),
{'a': 2},
)
assert r == [b't1', None]
r = yield from f.update_many(
(1,),
{'a': 3},
)
assert r == [b't1', b't3']
yield from f.set_many((
(1,),
(2,),
), (
b'foo',
b'bar',
))
r = yield from f.get_many(
{'a': 1},
(2,),
(3,),
)
assert r == [b'foo', b'bar', b't3']
yield from f.delete(2)
r = yield from f.get_or_update_many(
(1,),
(2,),
(3,),
)
assert r == [b'foo', b't2', b't3']
with pytest.raises(AttributeError):
yield from f.delete_many()
with pytest.raises(AttributeError):
yield from f.has_many()
with pytest.raises(AttributeError):
yield from f.touch_many()
@pytest.mark.asyncio
@asyncio.coroutine
def test_aioredis_hash(aioredis_pool):
client, _ = aioredis_pool
@ring.aioredis_hash(client, 'test-hashkey')
@asyncio.coroutine
def f(a):
return 't{}'.format(a).encode()
# delete previous test
yield from f.delete(1)
yield from f.delete(2)
yield from f.delete(3)
r = yield from f.get(1)
assert r is None
yield from f(1)
r = yield from f.has(1)
assert r is True
r = yield from f.get(1)
assert r == b't1'
r = yield from f.get_many(
(1,),
{'a': 2},
)
assert r == [b't1', None]
r = yield from f.update_many(
(1,),
{'a': 3},
)
assert r == [b't1', b't3']
yield from f.set_many((
(1,),
(2,),
), (
b'foo',
b'bar',
))
r = yield from f.get_many(
{'a': 1},
(2,),
(3,),
)
assert r == [b'foo', b'bar', b't3']
yield from f.delete(2)
r = yield from f.get_or_update_many(
(1,),
(2,),
(3,),
)
assert r == [b'foo', b't2', b't3']
@pytest.mark.asyncio
@asyncio.coroutine
def test_func_method(storage_dict):
storage, _ = storage_dict
class A(object):
def __ring_key__(self):
return 'A'
@ring.dict(storage)
@asyncio.coroutine
def method(self, a, b):
return base + a * 100 + b
@ring.dict(storage)
@classmethod
@asyncio.coroutine
def cmethod(cls, a, b):
return base + a * 200 + b
obj = A()
base = 10000
yield from obj.method.delete(1, 2)
assert ((yield from obj.method(1, 2))) == 10102
yield from obj.cmethod.delete(1, 2)
assert ((yield from obj.cmethod(1, 2))) == 10202
yield from A.cmethod.delete(1, 2)
assert ((yield from A.cmethod(1, 2))) == 10202
assert obj.cmethod.key(3, 4) == A.cmethod.key(3, 4)
@pytest.mark.asyncio
@asyncio.coroutine
def test_forced_sync(synchronous_storage_and_ring):
storage, storage_ring = synchronous_storage_and_ring
with pytest.raises(TypeError):
@storage_ring(storage)
@asyncio.coroutine
def g():
return 1
@storage_ring(storage, force_asyncio=True)
@asyncio.coroutine
def f(a):
return a
yield from f.delete('a')
assert None is ((yield from f.get('a')))
assert 'a' is ((yield from f('a')))
assert 'a' is ((yield from f.get('a')))