This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
forked from esnme/ultramemcache
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tests.py
347 lines (275 loc) · 9.92 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
# -*- coding: utf-8 -*-
"""
Copyright (c) 2011, Jonas Tarnstrom and ESN Social Software AB
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by ESN Social Software AB (www.esn.me).
4. Neither the name of the ESN Social Software AB nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ESN SOCIAL SOFTWARE AB ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from __future__ import with_statement
import time
import datetime
import logging
import unittest
import gevent
import random
from umemcached import Client
MEMCACHED_ADDRESS = "127.0.0.1:11211"
class Testumemcached(unittest.TestCase):
log = logging.getLogger('umemcached')
def testRandomData(self):
def random_bytes(size):
return "".join(chr(random.randrange(0, 256)) for i in xrange(size))
def random_str(size):
return "".join(chr(random.randrange(33, 64)) for i in xrange(size))
c = Client(MEMCACHED_ADDRESS);
c.connect();
count = 0
for x in xrange(0, 10):
key = random_str(random.randrange(1, 33))
value = random_bytes(random.randrange(1, 5000))
c.set(key, value, 0, 0, True)
v2 = c.get(key)[0]
count += 1
if len(value) != len(v2):
print "%d %d != %d" % (count, len(value), len(v2))
#self.assertEquals(value, v2)
def testBigDataFail(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
data = "31337" * 10000
for x in xrange(0, 10):
v = c.get("kaka" + str(x))
c.set("fsdafbdsakjfjdkfjadklsafdsafdsaffdsafdasfdsafdasfsdafdsafdsafasdas" + str(x), data, 604830, 17, True)
def testIncrDecrString(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("test", "hej")
self.assertRaises(Exception, c.incr, "test", 1)
self.assertRaises(Exception, c.decr, "test", 5)
def testSetExpiration(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("test", "1", 60, 2)
def testSegfault(self):
try:
c = Client()
assert False
except(TypeError):
pass
def testConnect(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
self.assertEquals(True, c.is_connected());
pass
def testDisconnect(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.disconnect();
self.assertEquals(False, c.is_connected());
pass
def testClose(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.close();
self.assertEquals(False, c.is_connected());
pass
def testConnectTwice(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.disconnect();
self.assertEquals(False, c.is_connected());
try:
c.connect();
assert False
except (RuntimeError):
pass
pass
def testConnectCloseQuery(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.disconnect();
try:
r = c.set("jonas", "kaka", 0, False)
assert False
except:
pass
def testOversizedKey(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
data = "A" * (1000 * 1000 * 2)
try:
r = c.set("test", data)
assert False
except(RuntimeError):
pass
def testGet(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key", "value")
self.assertEquals("value", c.get("key")[0])
self.assertEquals(None, c.get("key23123"))
pass
def testGets(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key", "value")
self.assertEquals("value", c.gets("key")[0])
self.assertEquals(None, c.gets("key23123"))
r = c.gets("key")
self.assertEquals(3, len(r))
pass
def testSet(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key", "my new value")
self.assertEquals("my new value", c.get("key")[0])
pass
def testGet_multi(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "value1")
c.set("key2", "value2")
c.set("key3", "value3")
r = c.get_multi(["key1", "key2", "key3", "key4"])
self.assertEquals(("value1", 0), r["key1"])
self.assertEquals(("value2", 0), r["key2"])
self.assertEquals(("value3", 0), r["key3"])
self.assertEquals(None, r.get("key4"))
pass
def testGets_multi(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "value1")
c.set("key2", "value2")
c.set("key3", "value3")
r = c.gets_multi(["key1", "key2", "key3", "key4"])
self.assertEquals(3, len(r["key1"]))
self.assertEquals(3, len(r["key2"]))
self.assertEquals(3, len(r["key3"]))
self.assertEquals("value1", r["key1"][0])
self.assertEquals("value2", r["key2"][0])
self.assertEquals("value3", r["key3"][0])
self.assertEquals(None, r.get("key4"))
pass
def testAdd(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "value1")
self.assertEquals("NOT_STORED", c.add("key1", "value"))
pass
def testReplace(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "value1")
self.assertEquals("STORED", c.replace("key1", "value"))
pass
pass
def testAppend(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "a")
self.assertEquals("STORED", c.append("key1", "b"))
self.assertEquals("ab", c.get("key1")[0])
pass
def testPrepend(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "a")
self.assertEquals("STORED", c.prepend("key1", "b"))
self.assertEquals("ba", c.get("key1")[0])
pass
def testDel(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "a")
c.delete("key1")
self.assertEquals(None, c.get("key1"))
pass
def testCas(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "a")
value, flags, cas = c.gets("key1")
self.assertEquals("STORED", c.cas("key1", "b", cas))
c.set("key1", "a")
self.assertEquals("EXISTS", c.cas("key1", "b", cas))
def testIncr(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "0")
c.incr("key1", 313370)
self.assertEquals("313370", c.get("key1")[0])
def testDecr(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
c.set("key1", "31337")
c.decr("key1", 31337)
self.assertEquals(0, long(c.get("key1")[0]))
def testVersion(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
v = c.version()
l = v.split('.');
self.assertEquals(3, len(l))
def testStats(self):
c = Client(MEMCACHED_ADDRESS);
c.connect();
d = c.stats()
self.assertTrue (d.has_key("uptime"))
self.assertTrue (d.has_key("bytes"))
def testConnectFails(self):
c = Client("130.244.1.1:31337");
try:
c.connect();
assert False
except:
pass
c.disconnect();
pass
def testConnectDNSOK(self):
c = Client("localhost:11211");
c.connect();
c.disconnect();
pass
def testConnectDNSFails(self):
c = Client("flensost:12111");
try:
c.connect();
assert False
except:
pass
c.disconnect();
pass
if __name__ == '__main__':
unittest.main()
"""
if __name__ == '__main__':
from guppy import hpy
hp = hpy()
hp.setrelheap()
while True:
unittest.main()
heap = hp.heapu()
print heap
"""