This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_basics.py
402 lines (329 loc) · 14.1 KB
/
test_basics.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
#! /usr/bin/env python
# coding=utf-8
from pgclient import *
from twisted.trial import unittest
import uuid
class TestBasics(unittest.TestCase):
TEST_HKEY = 'test_hkey_1'
TEST_SKEY = 'test_skey_1'
TEST_VALUE = 'test_value_1'
@inlineCallbacks
def setUp(self):
self.c = Pegasus(['127.0.0.1:34601', '127.0.0.1:34602', '127.0.0.1:34603'], 'temp')
ret = yield self.c.init()
self.assertTrue(ret)
def tearDown(self):
self.c.close()
@inlineCallbacks
def test_set_ok(self):
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE)
self.assertEqual(ret, error_types.ERR_OK.value)
@inlineCallbacks
def test_set_timeout(self):
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE*100000, 0, 10)
self.assertEqual(ret, error_types.ERR_TIMEOUT.value)
@inlineCallbacks
def test_remove_ok(self):
(ret, ign) = yield self.c.remove(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.exist(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OBJECT_NOT_FOUND.value)
@inlineCallbacks
def test_exist_ok(self):
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.exist(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OK.value)
@inlineCallbacks
def test_exist_none(self):
(ret, ign) = yield self.c.remove(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.exist(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OBJECT_NOT_FOUND.value)
@inlineCallbacks
def test_get_ok(self):
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.get(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(v, self.TEST_VALUE)
@inlineCallbacks
def test_get_none(self):
(ret, ign) = yield self.c.remove(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.get(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OBJECT_NOT_FOUND.value)
@inlineCallbacks
def test_ttl_forever(self):
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.ttl(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(v, -1)
@inlineCallbacks
def test_ttl_N(self):
ttl = 60
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE, ttl)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, v) = yield self.c.ttl(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(v, ttl)
@inlineCallbacks
def test_ttl_N_with_phase(self):
ttl = 10
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE, ttl)
self.assertEqual(ret, error_types.ERR_OK.value)
period = 2
d = defer.Deferred()
reactor.callLater(period, d.callback, 'ok')
yield d
(rc, v) = yield self.c.ttl(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(v, ttl - period)
@inlineCallbacks
def test_ttl_expired(self):
ttl = 1
(ret, ign) = yield self.c.set(self.TEST_HKEY, self.TEST_SKEY, self.TEST_VALUE, ttl)
self.assertEqual(ret, error_types.ERR_OK.value)
period = 1.5
d = defer.Deferred()
reactor.callLater(period, d.callback, 'ok')
yield d
(rc, v) = yield self.c.ttl(self.TEST_HKEY, self.TEST_SKEY)
self.assertEqual(rc, error_types.ERR_OBJECT_NOT_FOUND.value)
@inlineCallbacks
def test_multi_set_ok(self):
count = 50
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
@inlineCallbacks
def test_multi_get_ok(self):
count = 50
ks = {self.TEST_SKEY + str(x) for x in range(count)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, get_kvs) = yield self.c.multi_get(self.TEST_HKEY, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(len(get_kvs), len(kvs))
self.assertEqual(get_kvs, kvs)
@inlineCallbacks
def test_multi_get_1by1_ok(self):
count = 5
ks = {self.TEST_SKEY + str(x) for x in range(count)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
get_count = 0
while ks:
(rc, get_kvs) = yield self.c.multi_get(self.TEST_HKEY, ks, 1)
if rc == error_types.ERR_INCOMPLETE_DATA.value\
or rc == error_types.ERR_OK.value:
for (k, v) in get_kvs.items():
get_count += 1
self.assertIn(k, ks)
ks.remove(k)
self.assertIn(k, kvs)
self.assertEqual(v, kvs[k])
self.assertEqual(get_count, len(kvs))
@inlineCallbacks
def test_multi_del_ok(self):
count = 50
ks = {self.TEST_SKEY + str(x) for x in range(count)}
(rc, del_count) = yield self.c.multi_del(self.TEST_HKEY, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(del_count, len(ks))
@inlineCallbacks
def test_multi_get_part_ok(self):
count = 50
ks = {self.TEST_SKEY + str(x) for x in range(count/2)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, get_kvs) = yield self.c.multi_get(self.TEST_HKEY, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(len(get_kvs), len(ks))
for (k, v) in get_kvs.items():
self.assertIn(k, ks)
self.assertIn(k, kvs)
self.assertEqual(v, kvs[k])
@inlineCallbacks
def test_multi_get_more_ok(self):
count = 50
ks = {self.TEST_SKEY + str(x) for x in range(count)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count/2)}
(rc, del_count) = yield self.c.multi_del(self.TEST_HKEY, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(del_count, len(ks))
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, get_kvs) = yield self.c.multi_get(self.TEST_HKEY, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(len(get_kvs), len(kvs))
for (k, v) in get_kvs.items():
self.assertIn(k, ks)
self.assertIn(k, kvs)
self.assertEqual(v, kvs[k])
@inlineCallbacks
def test_sort_key_count_ok(self):
rand_key = uuid.uuid1().hex
(rc, count) = yield self.c.sort_key_count(self.TEST_HKEY + rand_key)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(count, 0)
@inlineCallbacks
def test_get_sort_keys_none(self):
rand_key = uuid.uuid1().hex
(rc, get_ks) = yield self.c.get_sort_keys(self.TEST_HKEY + rand_key)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(len(get_ks), 0)
@inlineCallbacks
def test_get_sort_keys_ok(self):
count = 50
rand_key = uuid.uuid1().hex
ks = {self.TEST_SKEY + str(x) for x in range(count)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
(rc, del_count) = yield self.c.multi_del(self.TEST_HKEY + rand_key, ks)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(del_count, len(ks))
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY + rand_key, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
(rc, get_ks) = yield self.c.get_sort_keys(self.TEST_HKEY + rand_key)
self.assertEqual(rc, error_types.ERR_OK.value)
self.assertEqual(len(get_ks), count)
@inlineCallbacks
def test_scan_none(self):
rand_key = uuid.uuid1().hex
o = ScanOptions()
s = self.c.get_scanner(self.TEST_HKEY + rand_key, b'\x00\x00', b'\xFF\xFF', o)
ret = yield s.get_next()
self.assertEqual(ret, None)
s.close()
@inlineCallbacks
def test_scan_all(self):
count = 50
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
rand_key = uuid.uuid1().hex
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY + rand_key, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
o = ScanOptions()
s = self.c.get_scanner(self.TEST_HKEY + rand_key, b'\x00\x00', b'\xFF\xFF', o)
get_count = 0
last_sk = None
while True:
hk_sk_v = yield s.get_next()
if hk_sk_v is None:
break
else:
get_count += 1
hk = hk_sk_v[0][0]
sk = hk_sk_v[0][1]
v = hk_sk_v[1]
self.assertNotEqual(sk, None)
if last_sk is not None:
self.assertLess(last_sk, sk)
last_sk = sk
self.assertEqual(hk, self.TEST_HKEY + rand_key)
self.assertIn(sk, kvs)
self.assertEqual(v, kvs[sk])
s.close()
self.assertEqual(count, get_count)
@inlineCallbacks
def test_scan_part(self):
count = 50
self.assertLess(2, count)
count_len = len(str(count))
kvs = {self.TEST_SKEY + str(x).zfill(count_len): self.TEST_VALUE + str(x) for x in range(count)}
sub_kvs = {self.TEST_SKEY + str(x).zfill(count_len): self.TEST_VALUE + str(x) for x in range(1, count-1)}
rand_key = uuid.uuid1().hex
(ret, ign) = yield self.c.multi_set(self.TEST_HKEY + rand_key, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
o = ScanOptions()
s = self.c.get_scanner(self.TEST_HKEY + rand_key,
self.TEST_SKEY + str(1).zfill(count_len),
self.TEST_SKEY + str(count-1).zfill(count_len),
o)
get_count = 0
last_sk = None
while True:
hk_sk_v = yield s.get_next()
if hk_sk_v is None:
break
else:
get_count += 1
hk = hk_sk_v[0][0]
sk = hk_sk_v[0][1]
v = hk_sk_v[1]
self.assertNotEqual(sk, None)
if last_sk is not None:
self.assertLess(last_sk, sk)
last_sk = sk
self.assertEqual(hk, self.TEST_HKEY + rand_key)
self.assertIn(sk, sub_kvs)
self.assertEqual(v, sub_kvs[sk])
s.close()
self.assertEqual(len(sub_kvs), get_count)
@inlineCallbacks
def test_unordered_scan_none(self):
split_count = 5
o = ScanOptions()
scanners = self.c.get_unordered_scanners(split_count, o)
self.assertEqual(len(scanners), split_count)
for scanner in scanners:
while True:
hk_sk_v = yield scanner.get_next()
# self.assertEqual(hk_sk_v, None) # TODO there maybe some remain data
if hk_sk_v is None:
break
scanner.close()
@inlineCallbacks
def test_unordered_scan_all(self):
hkey_count = 20
count = 50
rand_key = uuid.uuid1().hex
hks = {self.TEST_HKEY + rand_key + '_' + str(i) for i in range(hkey_count)}
kvs = {self.TEST_SKEY + str(x): self.TEST_VALUE + str(x) for x in range(count)}
for hk in hks:
(ret, ign) = yield self.c.multi_set(hk, kvs)
self.assertEqual(ret, error_types.ERR_OK.value)
split_count = 5
o = ScanOptions()
scanners = self.c.get_unordered_scanners(split_count, o)
self.assertEqual(len(scanners), split_count)
get_hk_count = 0
for scanner in scanners:
last_hk = None
last_sk = None
hk_found = False
get_count = 0
while True:
hk_sk_v = yield scanner.get_next()
if hk_sk_v is None:
break
hk = hk_sk_v[0][0]
sk = hk_sk_v[0][1]
v = hk_sk_v[1]
self.assertNotEqual(hk, None)
if last_hk is None:
last_hk = hk
get_hk_count += 1
self.assertNotEqual(sk, None)
if hk == last_hk:
get_count += 1
if last_sk is not None:
self.assertLess(last_sk, sk)
else:
get_hk_count += 1
if hk_found:
self.assertEqual(count, get_count)
get_count = 1
last_hk = hk
last_sk = sk
if hk in hks:
hk_found = True
self.assertIn(sk, kvs)
self.assertEqual(v, kvs[sk])
else:
hk_found = False
scanner.close()
self.assertLessEqual(hkey_count, get_hk_count)