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
|
import uuid
import pytest
from kazoo.testing import KazooTestCase
from kazoo.tests.util import CI_ZK_VERSION
class KazooQueueTests(KazooTestCase):
def _makeOne(self):
path = "/" + uuid.uuid4().hex
return self.client.Queue(path)
def test_queue_validation(self):
queue = self._makeOne()
with pytest.raises(TypeError):
queue.put({})
with pytest.raises(TypeError):
queue.put(b"one", b"100")
with pytest.raises(TypeError):
queue.put(b"one", 10.0)
with pytest.raises(ValueError):
queue.put(b"one", -100)
with pytest.raises(ValueError):
queue.put(b"one", 100000)
def test_empty_queue(self):
queue = self._makeOne()
assert len(queue) == 0
assert queue.get() is None
assert len(queue) == 0
def test_queue(self):
queue = self._makeOne()
queue.put(b"one")
queue.put(b"two")
queue.put(b"three")
assert len(queue) == 3
assert queue.get() == b"one"
assert queue.get() == b"two"
assert queue.get() == b"three"
assert len(queue) == 0
def test_priority(self):
queue = self._makeOne()
queue.put(b"four", priority=101)
queue.put(b"one", priority=0)
queue.put(b"two", priority=0)
queue.put(b"three", priority=10)
assert queue.get() == b"one"
assert queue.get() == b"two"
assert queue.get() == b"three"
assert queue.get() == b"four"
class KazooLockingQueueTests(KazooTestCase):
def setUp(self):
KazooTestCase.setUp(self)
skip = False
if CI_ZK_VERSION and CI_ZK_VERSION < (3, 4):
skip = True
elif CI_ZK_VERSION and CI_ZK_VERSION >= (3, 4):
skip = False
else:
ver = self.client.server_version()
if ver[1] < 4:
skip = True
if skip:
pytest.skip("Must use Zookeeper 3.4 or above")
def _makeOne(self):
path = "/" + uuid.uuid4().hex
return self.client.LockingQueue(path)
def test_queue_validation(self):
queue = self._makeOne()
with pytest.raises(TypeError):
queue.put({})
with pytest.raises(TypeError):
queue.put(b"one", b"100")
with pytest.raises(TypeError):
queue.put(b"one", 10.0)
with pytest.raises(ValueError):
queue.put(b"one", -100)
with pytest.raises(ValueError):
queue.put(b"one", 100000)
with pytest.raises(TypeError):
queue.put_all({})
with pytest.raises(TypeError):
queue.put_all([{}])
with pytest.raises(TypeError):
queue.put_all([b"one"], b"100")
with pytest.raises(TypeError):
queue.put_all([b"one"], 10.0)
with pytest.raises(ValueError):
queue.put_all([b"one"], -100)
with pytest.raises(ValueError):
queue.put_all([b"one"], 100000)
def test_empty_queue(self):
queue = self._makeOne()
assert len(queue) == 0
assert queue.get(0) is None
assert len(queue) == 0
def test_queue(self):
queue = self._makeOne()
queue.put(b"one")
queue.put_all([b"two", b"three"])
assert len(queue) == 3
assert not queue.consume()
assert not queue.holds_lock()
assert queue.get(1) == b"one"
assert queue.holds_lock()
# Without consuming, should return the same element
assert queue.get(1) == b"one"
assert queue.consume()
assert not queue.holds_lock()
assert queue.get(1) == b"two"
assert queue.holds_lock()
assert queue.consume()
assert not queue.holds_lock()
assert queue.get(1) == b"three"
assert queue.holds_lock()
assert queue.consume()
assert not queue.holds_lock()
assert not queue.consume()
assert len(queue) == 0
def test_consume(self):
queue = self._makeOne()
queue.put(b"one")
assert not queue.consume()
queue.get(0.1)
assert queue.consume()
assert not queue.consume()
def test_release(self):
queue = self._makeOne()
queue.put(b"one")
assert queue.get(1) == b"one"
assert queue.holds_lock()
assert queue.release()
assert not queue.holds_lock()
assert queue.get(1) == b"one"
assert queue.consume()
assert not queue.release()
assert len(queue) == 0
def test_holds_lock(self):
queue = self._makeOne()
assert not queue.holds_lock()
queue.put(b"one")
queue.get(0.1)
assert queue.holds_lock()
queue.consume()
assert not queue.holds_lock()
def test_priority(self):
queue = self._makeOne()
queue.put(b"four", priority=101)
queue.put(b"one", priority=0)
queue.put(b"two", priority=0)
queue.put(b"three", priority=10)
assert queue.get(1) == b"one"
assert queue.consume()
assert queue.get(1) == b"two"
assert queue.consume()
assert queue.get(1) == b"three"
assert queue.consume()
assert queue.get(1) == b"four"
assert queue.consume()
def test_concurrent_execution(self):
queue = self._makeOne()
value1 = []
value2 = []
value3 = []
event1 = self.client.handler.event_object()
event2 = self.client.handler.event_object()
event3 = self.client.handler.event_object()
def get_concurrently(value, event):
q = self.client.LockingQueue(queue.path)
value.append(q.get(0.1))
event.set()
self.client.handler.spawn(get_concurrently, value1, event1)
self.client.handler.spawn(get_concurrently, value2, event2)
self.client.handler.spawn(get_concurrently, value3, event3)
queue.put(b"one")
event1.wait(0.2)
event2.wait(0.2)
event3.wait(0.2)
result = value1 + value2 + value3
assert result.count(b"one") == 1
assert result.count(None) == 2
|