forked from aio-libs/aiomcache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_client.py
557 lines (412 loc) · 15.1 KB
/
test_client.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import asyncio
from io import BytesIO
from sys import version_info
from unittest import mock
import pytest
from aiomemcached.client import Client
from aiomemcached.constants import DEFAULT_MAX_KEY_LENGTH, DEFAULT_MAX_VALUE_LENGTH
from aiomemcached.exceptions import ResponseException, ValidationException
async def run_func_with_mocked_execute_raw_cmd(
client, server_response: bytes, func, *args, **kwargs
):
if version_info.major <= 3 and version_info.minor <= 7:
# TODO py38+
# py37 say: object _io.BytesIO can't be used in 'await' expression
return
response = BytesIO()
response.write(server_response)
response.seek(0)
with mock.patch.object(client, "_execute_raw_cmd") as patched:
patched.return_value = response
await func(*args, **kwargs)
@pytest.mark.asyncio
async def test_close(client):
await client.close()
assert client._pool.size() == 0
@pytest.mark.asyncio
async def test_validate_key(client):
good_key = b"test:key:good_key"
client.validate_key(good_key)
good_key = bytes("test:key:中文", encoding="utf-8")
client.validate_key(good_key)
good_key = bytes("test:key:こんにちは", encoding="utf-8")
client.validate_key(good_key)
good_key = bytes("test:key:안녕하세요", encoding="utf-8")
client.validate_key(good_key)
good_key = bytes("test:key:!@#", encoding="utf-8")
client.validate_key(good_key)
bad_key = "this_is_string"
with pytest.raises(ValidationException):
client.validate_key(bad_key)
bad_key = b"test:key:have space"
with pytest.raises(ValidationException):
client.validate_key(bad_key)
bad_key = b"test:key:have_newline\r"
with pytest.raises(ValidationException):
client.validate_key(bad_key)
bad_key = b"test:key:have_newline\n"
with pytest.raises(ValidationException):
client.validate_key(bad_key)
bad_key = b"test:key:have_newline\nin_center"
with pytest.raises(ValidationException):
client.validate_key(bad_key)
bad_key = b"test:key:too_long" + bytes(250)
with pytest.raises(ValidationException):
client.validate_key(bad_key)
@pytest.mark.asyncio
async def test_uri_parser(client):
host = "localhost"
port = 11211
result = client.uri_parser("memcached://localhost:11211")
assert result[0] == host
assert result[1] == port
result = client.uri_parser("memcached://localhost")
assert result[0] == host
assert result[1] == port
result = client.uri_parser("memcached://localhost:aaa")
assert result[0] == host
assert result[1] == port
with pytest.raises(ValidationException):
_ = client.uri_parser("BADmemcached://localhost")
_ = Client("memcached://localhost:11211")
@pytest.mark.asyncio
async def test_get_set(client):
key, value = b"test:key:get_set", b"1"
default = b"default"
flags = 11
exptime = 1
# get set
await client.set(key, value)
result, _ = await client.get(key)
assert result == value
result, _ = await client.get(b"not:" + key, default=default)
assert result == default
result, _ = await client.get(b"not:" + key)
assert result is None
# set param expire ---
await client.set(key, value, exptime=exptime)
result, _ = await client.get(key)
assert result == value
await asyncio.sleep(1.1)
result, _ = await client.get(key)
assert result is None
# set flags
await client.set(key, value, flags=flags)
result, info = await client.get(key)
assert result == value
assert info.get("flags") == flags
# set param errors ---
with pytest.raises(ValidationException):
await client.set(key, value, exptime=-1)
with pytest.raises(ValidationException):
await client.set(key, value, flags=-1)
# storage command error :TODO NOT_STORED, EXISTS, NOT_FOUND ?
async def func():
with pytest.raises(ResponseException):
await client.set(key, value)
await run_func_with_mocked_execute_raw_cmd(client, b"SERVER_ERROR\r\n", func)
# retrieval command error ---
async def func_r_server(*args, **kwargs):
with pytest.raises(ResponseException):
await client.get(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client, b"SERVER_ERROR\r\n", func_r_server, key
)
async def func_r_dup(*args, **kwargs):
with pytest.raises(ResponseException):
await client.get(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client,
b"VALUE %b 0 1\r\n1\r\nVALUE %b 0 1\r\n1\r\n" % (key, key),
func_r_dup,
key,
)
async def func_r_data_include_new_line(*args, **kwargs):
await client.get(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client,
b"VALUE %b 0 10\r\n_new\n_line\r\n" % key,
func_r_data_include_new_line,
key,
)
async def func_r_data_len(*args, **kwargs):
with pytest.raises(ResponseException):
await client.get(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client, b"VALUE %b 0 1\r\n12\r\n" % key, func_r_data_len, key
)
async def func_r_too_many(*args, **kwargs):
with pytest.raises(ResponseException):
await client.get(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client,
b"VALUE %b 0 1\r\n1\r\nVALUE %b 0 1\r\n1\r\n" % (key, key + b"other"),
func_r_too_many,
key,
)
@pytest.mark.asyncio
async def test_gets_set_cas(client):
key, value_1, value_2 = b"test:key:gets_set_cas", b"1", b"2"
default = b"default"
await client.set(key, value_1)
# basic function ---
result_value, result_info = await client.gets(key)
assert result_value == value_1
assert isinstance(result_info.get("cas"), int)
result_value, result_info = await client.gets(b"not:" + key, default=default)
assert result_value == default
assert len(result_info) == 0
result_value, result_info = await client.gets(b"not:" + key)
assert result_value is None
assert len(result_info) == 0
# cas ---
await client.set(key, value_1)
result_value, info = await client.gets(key)
cas = info.get("cas")
stored = await client.cas(key, value_2, cas + 1)
assert stored is False
result_value, result_info = await client.gets(key)
assert result_value == value_1
assert result_info.get("cas") == cas
stored = await client.cas(key, value_2, cas)
assert stored is True
result_value, result_info = await client.gets(key)
assert result_value == value_2
assert isinstance(result_info.get("cas"), int)
@pytest.mark.asyncio
async def test_get_many_set(client):
key_1, value_1 = b"test:key:get_many_set_1", b"1"
key_2, value_2 = b"test:key:get_many_set_2", b"2"
await client.set(key_1, value_1)
await client.set(key_2, value_2)
keys = [key_1, key_2]
result, _ = await client.get_many(keys)
result[key_1] = value_1
result[key_2] = value_2
keys = [b"not" + key_1, key_2]
result, _ = await client.get_many(keys)
assert key_1 not in result
keys = []
result, _ = await client.get_many(keys)
assert len(result) == 0
@pytest.mark.asyncio
async def test_gets_many_set(client):
key_1, value_1 = b"test:key:gets_many_set:1", b"1"
key_2, value_2 = b"test:key:gets_many_set:2", b"2"
await client.set(key_1, value_1)
await client.set(key_2, value_2)
keys = [key_1, key_2]
result_value, result_cas = await client.gets_many(keys)
assert result_value[key_1] == value_1
assert result_value[key_2] == value_2
assert isinstance(result_cas[key_1].get("flags"), int)
assert isinstance(result_cas[key_2].get("cas"), int)
keys = []
result, _ = await client.gets_many(keys)
assert len(result) == 0
@pytest.mark.asyncio
async def test_multi_get(client):
key1, value1 = b"test:key:multi_get:1", b"1"
key2, value2 = b"test:key:multi_get:2", b"2"
await client.set(key1, value1)
await client.set(key2, value2)
test_value = await client.multi_get(key1, key2)
assert test_value == (value1, value2)
test_value = await client.multi_get(b"not" + key1, key2)
assert test_value == (None, value2)
test_value = await client.multi_get()
assert test_value == ()
@pytest.mark.asyncio
async def test_add(client):
key, value = b"test:key:add", b"1"
await client.set(key, value)
test_value = await client.add(key, b"2")
assert not test_value
test_value = await client.add(b"not:" + key, b"2")
assert test_value
result, _ = await client.get(b"not:" + key)
assert result == b"2"
@pytest.mark.asyncio
async def test_replace(client):
key, value = b"test:key:replace", b"1"
await client.set(key, value)
test_value = await client.replace(key, b"2")
assert test_value
# make sure value exists
result, _ = await client.get(key)
assert result == b"2"
test_value = await client.replace(b"not:" + key, b"3")
assert not test_value
# make sure value exists
result, _ = await client.get(b"not:" + key)
assert result is None
@pytest.mark.asyncio
async def test_append(client):
key, value = b"test:key:append", b"1"
await client.set(key, value)
test_value = await client.append(key, b"2")
assert test_value
# make sure value exists
result, _ = await client.get(key)
assert result == b"12"
test_value = await client.append(b"not:" + key, b"3")
assert not test_value
# make sure value exists
result, _ = await client.get(b"not:" + key)
assert result is None
@pytest.mark.asyncio
async def test_prepend(client):
key, value = b"test:key:prepend", b"1"
await client.set(key, value)
test_value = await client.prepend(key, b"2")
assert test_value
# make sure value exists
result, _ = await client.get(key)
assert result == b"21"
test_value = await client.prepend(b"not:" + key, b"3")
assert not test_value
# make sure value exists
result, _ = await client.get(b"not:" + key)
assert result is None
@pytest.mark.asyncio
async def test_delete(client):
key, value = b"test:key:delete", b"value"
await client.set(key, value)
# make sure value exists
result, _ = await client.get(key)
assert result == value
is_deleted = await client.delete(key)
assert is_deleted
# make sure value does not exists
result, _ = await client.get(key)
assert result is None
# delete key not exists
is_deleted = await client.delete(b"not:key")
assert not is_deleted
async def func(*args, **kwargs):
with pytest.raises(ResponseException):
await client.delete(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(client, b"SERVER_ERROR\r\n", func, key)
@pytest.mark.asyncio
async def test_incr_decr(client):
# incr ---
key, value = b"test:key:incr:1", b"1"
await client.set(key, value)
test_value = await client.incr(key, 2)
assert test_value == 3
# make sure value exists
result, _ = await client.get(key)
assert result == b"3"
# incr error ---
key, value = b"test:key:incr:2", b"string"
await client.set(key, value)
with pytest.raises(ResponseException):
await client.incr(key, 2)
with pytest.raises(ValidationException):
await client.incr(key, 3.14)
# decr ---
key, value = b"test:key:decr:1", b"17"
await client.set(key, value)
result = await client.decr(key, 2)
assert result == 15
result, _ = await client.get(key)
assert result == b"15"
result = await client.decr(key, 1000)
assert result == 0
# decr ---
key, value = b"test:key:decr:2", b"string"
await client.set(key, value)
with pytest.raises(ResponseException):
await client.decr(key, 2)
with pytest.raises(ValidationException):
await client.decr(key, 3.14)
# common --
key, value = b"test:key:incr_decr:1", b"1"
await client.set(key, value)
await client.incr(key, increment=1)
await client.decr(key, decrement=1)
with pytest.raises(ValidationException):
await client.incr(key, value=-1)
# NOT_FOUND
async def func_1(*args, **kwargs):
assert await client.incr(*args, **kwargs) is None
await run_func_with_mocked_execute_raw_cmd(
client, b"NOT_FOUND\r\n", func_1, b"not:" + key
)
async def func_2(*args, **kwargs):
with pytest.raises(ResponseException):
await client.incr(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client, b"SERVER_ERROR\r\n", func_2, b"not:" + key
)
@pytest.mark.asyncio
async def test_touch(client):
key, value = b"test:key:touch:1", b"17"
await client.set(key, value)
touched = await client.touch(key, 1)
assert touched
result, _ = await client.get(key)
assert result == value
await asyncio.sleep(1.1)
result, _ = await client.get(key)
assert result is None
# NOT_FOUND
touched = await client.touch(b"not:" + key, 1)
assert not touched
async def func(*args, **kwargs):
with pytest.raises(ResponseException):
await client.touch(*args, **kwargs)
await run_func_with_mocked_execute_raw_cmd(
client, b"SERVER_ERROR\r\n", func, b"not:" + key, 1
)
@pytest.mark.asyncio
async def test_stats(client):
stats = await client.stats()
assert b"pid" in stats
async def func():
await client.stats()
await run_func_with_mocked_execute_raw_cmd(
client, b"STAT a\r\nSTAT a b\r\nSTAT a b c\r\nEND\r\n", func
)
# bad response
async def func_bad_response():
with pytest.raises(ResponseException):
await client.stats()
await run_func_with_mocked_execute_raw_cmd(
client, b"BAD_RESPONSE\r\nEND\r\n", func_bad_response
)
@pytest.mark.asyncio
async def test_version(client):
version = await client.version()
stats = await client.stats()
assert version == stats[b"version"]
async def func():
with pytest.raises(ResponseException):
await client.version()
await run_func_with_mocked_execute_raw_cmd(client, b"NOT_VERSION\r\n", func)
@pytest.mark.asyncio
async def test_flush_all(client):
key, value = b"test:key:flush_all", b"flush_all_value"
await client.set(key, value)
# make sure value exists
result, _ = await client.get(key)
assert result == value
# flush data
await client.flush_all()
# make sure value does not exists
result, _ = await client.get(key)
assert result is None
async def func():
with pytest.raises(ResponseException):
await client.flush_all()
await run_func_with_mocked_execute_raw_cmd(client, b"NOT_OK\r\n", func)
@pytest.mark.asyncio
async def test_out_of_limit(client):
def generate_bytes(length):
return b"".join([b"0" for _ in range(length)])
key = generate_bytes(DEFAULT_MAX_KEY_LENGTH + 1)
value = generate_bytes(DEFAULT_MAX_VALUE_LENGTH + 1)
with pytest.raises(ValidationException):
await client.get(key)
with pytest.raises(ValidationException):
await client.set(key=b"key", value=value)