-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_txMemcached.py
122 lines (102 loc) · 3.73 KB
/
test_txMemcached.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
from nose.twistedtools import reactor, deferred
from twisted.internet import defer
import unittest
from txMemcached import MemcacheProtocol
from mock import Mock
class MockTransport(object):
def __init__(self):
self.buffer = []
def write(self, d):
self.buffer.append(d)
def value(self):
return ''.join(self.buffer)
class TextInterleavedProtocol(unittest.TestCase):
def setUp(self):
self.protocol = MemcacheProtocol()
self.protocol.transport = Mock()
@deferred(timeout=1.0)
def test_connection(self):
d = self.protocol.connectedDeferred
self.protocol.connectionMade()
return d
@deferred(timeout=1.0)
def test_set_simple(self):
m = self.protocol.transport
d = self.protocol.set("a", "value")
self.protocol.lineReceived('STORED')
def wrote(key):
assert key == "a"
d.addCallback(wrote)
return d
@deferred(timeout=1.0)
def test_get_simple(self):
m = self.protocol.transport
d = self.protocol.get("a")
self.protocol.lineReceived('VALUE a 0 5')
self.protocol.rawDataReceived("abcde\r\n")
self.protocol.lineReceived('END')
def got((key, value)):
assert key == "a"
assert value == "abcde"
d.addCallback(got)
return d
@deferred(timeout=1.0)
def DONTtest_set_multiple(self):
m = self.protocol.transport = MockTransport()
d1 = self.protocol.set("a", "1")
reactor.callLater(0, self.protocol.lineReceived, "STORED")
assert self.protocol.lock.locked
def wrote_1(key):
assert key == "a"
assert m.value() == '\r\n'.join(["set a 0 0 1", "1", ""])
m.buffer = []
assert self.protocol.lock.locked
reactor.callLater(0, self.protocol.lineReceived, "STORED")
d1.addCallback(wrote_1)
d2 = self.protocol.set("b", "2")
final_d = defer.Deferred()
def wrote_2(key):
assert key == "b"
assert m.value() == '\r\n'.join(["set b 0 0 1", "2", ""])
assert self.protocol.lock.locked
reactor.callLater(0, final_d.callback, None)
d2.addCallback(wrote_2)
def final(_):
assert not self.protocol.lock.locked
final_d.addCallback(final)
return final_d
@deferred(timeout=1.0)
def DONTtest_get_multiple(self):
m = self.protocol.transport = MockTransport()
d1 = self.protocol.get("a")
assert self.protocol.lock.locked
def send(k, v):
self.protocol.lineReceived('VALUE %s 0 %d' % (k, len(v)))
self.protocol.rawDataReceived("%s\r\n" % v)
self.protocol.lineReceived('END')
reactor.callLater(0, send, "a", "123")
def got_a((k, v)):
assert k == 'a'
assert v == '123'
assert self.protocol.lock.locked
assert m.value() == 'get a\r\n'
assert [k for (_, k) in self.protocol.requests] == ['b']
m.buffer = []
reactor.callLater(0, send, "b", "456")
d1.addCallback(got_a)
d2 = self.protocol.get("b")
final_d = defer.Deferred()
def got_b((k, v)):
assert k == 'b'
assert v == '456'
assert self.protocol.lock.locked
assert m.value() == 'get b\r\n'
m.buffer = []
assert [k for (_, k) in self.protocol.requests] == []
reactor.callLater(0, final_d.callback, None)
d2.addCallback(got_b)
def final(_):
assert not self.protocol.lock.locked
final_d.addCallback(final)
assert [k for (_, k) in self.protocol.requests] == ['b', 'a']
return final_d