forked from basho/riak-python-client
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_pool.py
More file actions
363 lines (301 loc) · 10.5 KB
/
Copy pathtest_pool.py
File metadata and controls
363 lines (301 loc) · 10.5 KB
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
# Copyright 2010-present Basho Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from random import SystemRandom
from threading import currentThread, Thread
from time import sleep
from riak import RiakError
from riak.tests import RUN_POOL
from riak.tests.comparison import Comparison
from riak.transports.pool import BadResource, Pool
from queue import Queue
class SimplePool(Pool):
def __init__(self):
self.count = 0
Pool.__init__(self)
def create_resource(self):
self.count += 1
return [self.count]
def destroy_resource(self, resource):
del resource[:]
class EmptyListPool(Pool):
def create_resource(self):
return []
@unittest.skipUnless(RUN_POOL, "RUN_POOL is 0")
class PoolTest(unittest.TestCase, Comparison):
def test_can_raise_bad_resource(self):
ex_msg = "exception-message!"
with self.assertRaises(BadResource) as cm:
raise BadResource(ex_msg)
ex = cm.exception
self.assertEqual(ex.args[0], ex_msg)
def test_bad_resource_inner_exception(self):
ex_msg = "exception-message!"
ex = RiakError(ex_msg)
with self.assertRaises(BadResource) as cm:
raise BadResource(ex)
br_ex = cm.exception
self.assertEqual(br_ex.args[0], ex)
def test_yields_new_object_when_empty(self):
"""
The pool should create new resources as needed.
"""
pool = SimplePool()
with pool.transaction() as element:
self.assertEqual([1], element)
def test_yields_same_object_in_serial_access(self):
"""
The pool should reuse resources that already exist, when used
serially.
"""
pool = SimplePool()
with pool.transaction() as element:
self.assertEqual([1], element)
element.append(2)
with pool.transaction() as element2:
self.assertEqual(1, len(pool.resources))
self.assertEqual([1, 2], element2)
self.assertEqual(1, len(pool.resources))
def test_reentrance(self):
"""
The pool should be re-entrant, that is, yield new resources
while one is already claimed in the same code path.
"""
pool = SimplePool()
with pool.transaction() as first:
self.assertEqual([1], first)
with pool.transaction() as second:
self.assertEqual([2], second)
with pool.transaction() as third:
self.assertEqual([3], third)
def test_unlocks_when_exception_raised(self):
"""
The pool should unlock all resources that were previously
claimed when an exception occurs.
"""
pool = SimplePool()
try:
with pool.transaction():
with pool.transaction():
raise RuntimeError
except Exception:
self.assertEqual(2, len(pool.resources))
for e in pool.resources:
self.assertFalse(e.claimed)
def test_removes_bad_resource(self):
"""
The pool should remove resources that are considered bad by
user code throwing a BadResource exception.
"""
pool = SimplePool()
with pool.transaction() as resource:
self.assertEqual([1], resource)
resource.append(2)
try:
with pool.transaction():
raise BadResource("bad resource")
except BadResource:
self.assertEqual(0, len(pool.resources))
with pool.transaction() as goodie:
self.assertEqual([2], goodie)
def test_filter_skips_unmatching_resources(self):
"""
The _filter parameter should cause the pool to yield the first
unclaimed resource that passes the filter.
"""
def filtereven(numlist):
return numlist[0] % 2 == 0
pool = SimplePool()
with pool.transaction():
with pool.transaction():
pass
with pool.transaction(_filter=filtereven) as f:
self.assertEqual([2], f)
def test_requires_filter_to_be_callable(self):
"""
The _filter parameter should be required to be a callable, or
None.
"""
badfilter = "foo"
pool = SimplePool()
with self.assertRaises(TypeError):
with pool.transaction(_filter=badfilter):
pass
def test_yields_default_when_empty(self):
"""
The pool should yield the given default when no existing
resources are free.
"""
pool = SimplePool()
with pool.transaction(default="default") as x:
self.assertEqual("default", x)
def test_manual_release(self):
"""
The pool should allow resources to be acquired and released
manually, without giving them out twice.
"""
pool = SimplePool()
a = pool.acquire()
self.assertEqual([1], a.object)
with pool.transaction() as b:
self.assertEqual([2], b)
with pool.transaction() as c:
self.assertEqual([2], c)
pool.release(a)
with pool.transaction() as d:
self.assertEqual([1], d)
e = pool.acquire()
with pool.transaction() as f:
self.assertEqual([2], f)
e.release()
with pool.transaction() as g:
self.assertEqual([1], g)
def test_thread_safety(self):
"""
The pool should allocate n objects for n concurrent operations.
"""
n = 10
pool = EmptyListPool()
readyq = Queue()
finishq = Queue()
threads = []
def _run():
with pool.transaction() as resource:
readyq.put(1)
resource.append(currentThread())
finishq.get(True)
finishq.task_done()
for i in range(n):
th = Thread(target=_run)
threads.append(th)
th.start()
for i in range(n):
readyq.get()
readyq.task_done()
for i in range(n):
finishq.put(1)
for thr in threads:
thr.join()
self.assertEqual(n, len(pool.resources))
for resource in pool.resources:
self.assertFalse(resource.claimed)
self.assertEqual(1, len(resource.object))
self.assertIn(resource.object[0], threads)
def test_iteration(self):
"""
Iteration over the pool resources, even when some are claimed,
should eventually touch all resources (excluding ones created
during iteration).
"""
for i in range(25):
started = Queue()
n = 1000
threads = []
touched = []
pool = EmptyListPool()
rand = SystemRandom()
def _run():
psleep = rand.uniform(0.05, 0.1)
with pool.transaction() as a:
started.put(1)
started.join()
a.append(rand.uniform(0, 1))
sleep(psleep)
for i in range(n):
th = Thread(target=_run)
threads.append(th)
th.start()
for i in range(n):
started.get()
started.task_done()
for resource in pool:
touched.append(resource)
for thr in threads:
thr.join()
self.assertItemsEqual(pool.resources, touched)
def test_clear(self):
"""
Clearing the pool should remove all resources known at the
time of the call.
"""
n = 10
startq = Queue()
finishq = Queue()
rand = SystemRandom()
threads = []
pusher = None
pool = SimplePool()
def worker_run():
with pool.transaction():
startq.put(1)
startq.join()
sleep(rand.uniform(0, 0.5))
finishq.get()
finishq.task_done()
def pusher_run():
for i in range(n):
finishq.put(1)
sleep(rand.uniform(0, 0.1))
finishq.join()
# Allocate 10 resources in the pool by spinning up 10 threads
for i in range(n):
th = Thread(target=worker_run)
threads.append(th)
th.start()
# Pull everything off the queue, allowing the workers to run
for i in range(n):
startq.get()
startq.task_done()
# Start the pusher that will allow them to proceed and exit
pusher = Thread(target=pusher_run)
threads.append(pusher)
pusher.start()
# Clear the pool
pool.clear()
# Wait for all threads to complete
for t in threads:
t.join()
# Make sure that the pool resources are gone
self.assertEqual(0, len(pool.resources))
def test_stress(self):
"""
Runs a large number of threads doing operations with resources
checked out, ensuring properties of the pool.
"""
rand = SystemRandom()
n = rand.randint(1, 400)
passes = rand.randint(1, 20)
rounds = rand.randint(1, 200)
breaker = rand.uniform(0, 1)
pool = EmptyListPool()
def _run():
for i in range(rounds):
with pool.transaction() as a:
self.assertEqual([], a)
a.append(currentThread())
self.assertEqual([currentThread()], a)
for p in range(passes):
self.assertEqual([currentThread()], a)
if rand.uniform(0, 1) > breaker:
break
a.remove(currentThread())
threads = []
for i in range(n):
th = Thread(target=_run)
threads.append(th)
th.start()
for th in threads:
th.join()
if __name__ == "__main__":
unittest.main()