This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
398 lines (323 loc) · 14.9 KB
/
tests.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
import struct
from twisted.trial import unittest
from twisted.test import proto_helpers
from twisted.internet import task
from pmemcached.storages.memory import Storage
from pmemcached.storages import getStorage
from pmemcached.storages.memory import Storage as MemoryStorage
from pmemcached.server import MemcachedFactory
class ServerTests(unittest.TestCase):
HEADER_STRUCT = '!BBHBBHLLQ'
HEADER_SIZE = 24
MAGIC = {
'request': 0x80,
'response': 0x81
}
# All structures will be appended to HEADER_STRUCT
COMMANDS = {
'get': {'command': 0x00, 'struct': '%ds'},
'set': {'command': 0x01, 'struct': 'LL%ds%ds'},
'add': {'command': 0x02, 'struct': 'LL%ds%ds'},
'replace': {'command': 0x03, 'struct': 'LL%ds%ds'},
'delete': {'command': 0x04, 'struct': '%ds'},
'incr': {'command': 0x05, 'struct': 'QQL%ds'},
'decr': {'command': 0x06, 'struct': 'QQL%ds'},
'flush': {'command': 0x08, 'struct': 'I'},
'stat': {'command': 0x10},
'auth_negotiation': {'command': 0x20},
'auth_request': {'command': 0x21, 'struct': '%ds%ds'}
}
STATUS = {
'success': 0x00,
'key_not_found': 0x01,
'key_exists': 0x02,
'auth_error': 0x08,
'unknown_command': 0x81
}
FLAGS = {
'pickle': 1 << 0,
'integer': 1 << 1,
'long': 1 << 2,
'compressed': 1 << 3
}
def setUp(self):
self.storage = Storage()
factory = MemcachedFactory(self.storage)
self.protocol = factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransport()
self.clock = task.Clock()
self.storage.callLater = self.clock.callLater
self.protocol.makeConnection(self.tr)
def tearDown(self):
self.protocol.transport.loseConnection()
def testGetInvalidKey(self):
key = 'foobarbazdoesnotexist'
expected = '\x81\x00\x00\x15\x00\x00\x00\x01\x00\x00\x00\t\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Not found'
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected)
def testGet(self):
key = 'foo'
value = 'bar'
expected = '\x81\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'+ \
'\x81\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00bar'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected)
def testGetExpiredKey(self):
key = 'foo'
value = 'bar'
expected = '\x81\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'+ \
'\x81\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00bar'
expected_not_found = '\x81\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\t' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Not found'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected)
self.clock.advance(1)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected_not_found)
def testOverwritedExpireTime(self):
"""
This will test if storage wont create 2 callLaters for the same key. It have to
aways delete old one if it is not expired and create a new one.
"""
key = 'foo'
value = 'bar'
expected = '\x81\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'+ \
'\x81\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00bar'
expected_not_found = '\x81\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\t' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Not found'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected)
time = 1500
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.clock.advance(1)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), '\x81\x00\x00\x00\x04\x00\x00\x00\x00' + \
'\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00' + \
'\x00\x00\x00bar')
# Can't have more than one because the backend should overwrite the first
# one
self.assertEqual(len(self.clock.calls), 1)
self.assertEqual(self.clock.calls[0].getTime(), 1.5)
def testSet(self):
key = 'foo'
value = 'bar'
expected = '\x81\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected)
def testAdd(self):
key = 'foo'
value = 'bar'
expected_add_success = '\x81\x02\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
expected_add_fail ='\x81\x02\x00\x00\x00\x00\x00\x02\x00\x00\x00\x14' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Data exists for key.'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['add']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['add']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected_add_success)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['add']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['add']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected_add_fail)
def testReplace(self):
key = 'foo'
value = 'bar'
expected_replace = '\x81\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
expected_get = '\x81\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x07' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00baz'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.tr.clear()
value = 'baz'
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['replace']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['replace']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected_replace)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected_get)
def testReplaceInvalidKey(self):
key = 'foo'
value = 'bar'
expected = '\x81\x03\x00\x00\x00\x00\x00\x01\x00\x00\x00\t\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Not found'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['replace']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['replace']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected)
def testDelete(self):
key = 'foo'
value = 'bar'
expected_set = '\x81\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
expected_delete = '\x81\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
expected_delete_fail = '\x81\x04\x00\x00\x00\x00\x00\x01\x00\x00\x00\t' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Not found'
expected_get_should_not_exists = '\x81\x00\x00\x03\x00\x00\x00\x01' + \
'\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
'Not found'
flags = 0
time = 1000
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['set']['struct'] % (len(key), len(value)),
self.MAGIC['request'],
self.COMMANDS['set']['command'],
len(key),
8, 0, 0, len(key) + len(value) + 8, 0, 0, flags, time, key, value))
self.assertEqual(self.tr.value(), expected_set)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['delete']['struct'] % len(key),
self.MAGIC['request'],
self.COMMANDS['delete']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected_delete)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['delete']['struct'] % len(key),
self.MAGIC['request'],
self.COMMANDS['delete']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected_delete_fail)
self.tr.clear()
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
self.COMMANDS['get']['command'],
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected_get_should_not_exists)
def testUnknownCommand(self):
key = 'foo'
expected = '\x81\x91\x00\x00\x00\x00\x00\x81\x00\x00\x00\x0F\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Unknown command'
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['get']['struct'] % (len(key)),
self.MAGIC['request'],
0x91,
len(key), 0, 0, 0, len(key), 0, 0, key))
self.assertEqual(self.tr.value(), expected)
def testInvalidHeader(self):
self.protocol.dataReceived('foobar')
self.assertEqual(self.tr.value(), '')
def testInvalidMagicCode(self):
self.protocol.dataReceived(
'\x82\x91\x00\x00\x00\x00\x00\x81\x00\x00\x00\x0F\x00\x00' + \
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(self.tr.value(), '')
def testIncrement(self):
key = 'foo'
value = 1
raise unittest.SkipTest("Skipping increment for now")
self.protocol.dataReceived(struct.pack(self.HEADER_STRUCT + \
self.COMMANDS['incr']['struct'] % len(key),
self.MAGIC['request'],
self.COMMANDS['incr']['command'],
len(key),
20, 0, 0, len(key) + 20, 0, 0, value, 0, 1000, key))
self.assertEqual(1, self.tr.value())
def testDecrement(self):
raise unittest.SkipTest('stub')
class BaseTests(unittest.TestCase):
def testGetValidStorage(self):
self.assertTrue(isinstance(getStorage('memcached'), MemoryStorage))
def testGetInvalidValidStorage(self):
self.assertTrue(isinstance(getStorage('foo'), MemoryStorage))