-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_cond.py
317 lines (240 loc) · 10.1 KB
/
tests_cond.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
""" Testcases borrowed from git://github.com/foxx/python-redislock.git """
import redis
import logging
import multiprocessing
import time
import sys
import traceback
from Queue import Empty, Full
from pprint import pprint as p
from pprint import pformat as pf
from gredislock import GRedisLock
###############################################################
# CONFIGURATION
###############################################################
# Worker config
WORKERS = 5
WORKERS_DUPLICATE = 5
# Logging config
DEBUG_LEVEL = logging.INFO
# Others
THREAD_SYNC_WAIT = 2
LOCK_TIMEOUT = 5
RESULT_TIMEOUT = 20
MQUEUE_TIMEOUT = 1
COLLECT_FPS = 0.01
#### DO NOT EDIT BELOW THIS LINE #####
def configure_logging():
FORMAT = "[%(asctime)-15s] [%(levelname)s] [%(process)d/%(processName)s] %(message)s"
logging.basicConfig(format=FORMAT, level=DEBUG_LEVEL)
def display_traceback(msg=""):
msg = "%s; %s" % ( msg, get_traceback())
logging.error(msg)
def get_traceback():
exc_info = sys.exc_info()
msg = '\n'.join(traceback.format_exception(*exc_info))
return msg
configure_logging()
class LockTest(object):
"""Simulate workers trying to acquire a lock at the same time.
Locks are given based on the worker_id.
This example will attempt to create 5 sets of 5 workers, which
means each worker_id will be spawned 5 times.
The end result is that only 5 of the workers should acquire the lock."""
def __init__(self):
# temp assign
expected_total = ( WORKERS * WORKERS_DUPLICATE )
self.redis = redis.StrictRedis(host='localhost', port=6379, db=9)
self.mqueue = multiprocessing.Queue(expected_total)
self.threads = []
self.lock = None
def run_worker(self, worker_id, mqueue, instance_id, cqueue):
"""Entry point for worker threads"""
logging.debug("started thread")
try:
# create lock instance
w = self.lock(self.redis, str(worker_id))
while True:
# wait for command
#logging.debug("waiting for cmd")
cmd, start_ts, args, kwargs = cqueue.get()
logging.debug("received cmd: %s" % ( (cmd, start_ts, args, kwargs), ))
# re-produce race condition (ensures they all start at the same time)
while time.time() < start_ts:
time.sleep(0.01)
logging.debug("start_ts reached")
# execute command
if cmd == 'acquire':
result = w.acquire(*args, **kwargs)
elif cmd == 'release':
result = w.release(*args, **kwargs)
else:
assert False, "no such cmd %s" % ( cmd, )
mqueue.put({
'worker_id' : worker_id,
'result' : result,
#'release' : r_result,
'instance_id' : instance_id
})
except:
display_traceback()
def start_threads(self):
"""Starts up the threads"""
for worker_id in range(WORKERS):
for instance_id in range(WORKERS_DUPLICATE):
worker_name = "worker-%s-%s" % ( worker_id, instance_id, )
cqueue = multiprocessing.Queue(1)
t = multiprocessing.Process(target=self.run_worker, name=worker_name, args=(worker_id, self.mqueue, instance_id, cqueue))
self.threads.append((t, cqueue))
t.start()
def stop_threads(self):
"""Hard stop all threads"""
# ensure all threads are hard killed
for t, cqueue in self.threads:
logging.debug("terminating %s" % ( t.name, ))
t.terminate()
def thread_action(self, action, *args, **kwargs):
# sanity checks
assert action in ['acquire', 'release']
# push to threads
start_ts = time.time() + THREAD_SYNC_WAIT
for t, cqueue in self.threads:
cqueue.put((action, start_ts, args, kwargs))
expected_total = ( WORKERS * WORKERS_DUPLICATE )
results = []
start_ts = time.time()
while True:
# sanity checks
assert len(results) <= expected_total, "wtf"
# temp assign
elapsed = ( time.time() - start_ts )
if elapsed > RESULT_TIMEOUT:
raise Exception, "collect_results() timed out"
# looks like we got all our results
if len(results) == expected_total:
break
try:
res = self.mqueue.get(block=True, timeout=MQUEUE_TIMEOUT)
results.append(res)
except Empty:
pass
time.sleep(COLLECT_FPS)
return results
def test_release(self):
"""Attempt to release the lock on each thread.
Collect the lock results back from each thread, and check
if the locking semantics worked as expected"""
# fetch results of release from all threads
result = self.thread_action(action='release')
logging.debug("Lock release() result:")
logging.debug(pf(result))
instance_ids = []
success = True
# find duplicate instances
for worker_id in range(WORKERS):
worker_results = filter(lambda x: x.get('worker_id') == worker_id, result)
locks = filter(lambda x: x.get('result') == True, worker_results)
lock_instance_ids = map(lambda x: str(x.get('instance_id')), locks)
instance_ids.append(lock_instance_ids)
lock_instance_ids = ",".join(lock_instance_ids)
if len(locks) == 1:
logging.info("worker_id[%s] result: PASSED (lock released by instance %s)" % ( worker_id, lock_instance_ids, ))
else:
logging.error("worker_id[%s] result: FAILED (lock released by instances %s)" % ( worker_id, lock_instance_ids, ))
success = False
return instance_ids, success
def test_acquire(self, timeout):
"""Attempt to acquire the lock on each thread.
Collect the lock results back from each thread, and check
if the locking semantics worked as expected"""
# fetch results of acquire from all threads
result = self.thread_action(action='acquire', blocking=False, timeout=timeout)
logging.debug("Lock acquire() result:")
logging.debug(pf(result))
instance_ids = []
success = True
# find duplicate instances
for worker_id in range(WORKERS):
worker_results = filter(lambda x: x.get('worker_id') == worker_id, result)
locks = filter(lambda x: x.get('result') == True, worker_results)
lock_instance_ids = map(lambda x: str(x.get('instance_id')), locks)
instance_ids.append(lock_instance_ids)
lock_instance_ids = ",".join(lock_instance_ids)
if len(locks) == 1:
logging.info("worker_id[%s] result: PASSED (lock acquired by instance %s)" % ( worker_id, lock_instance_ids, ))
else:
logging.error("worker_id[%s] result: FAILED (lock acquired by instances %s)" % ( worker_id, lock_instance_ids, ))
success = False
return instance_ids, success
def run(self, lock):
"""Start the test"""
# clear redis
self.redis.flushall()
logging.info("Redis flushall() executed")
# determine lock type
self.lock = lock
logging.info("Testing lock type: %s" % ( lock, ))
# listen for results
try:
# start threads
logging.info("Starting threads")
self.start_threads()
# Attempt to acquire the first set of locks
logging.info("------------------------------------------------------")
logging.info("TEST 1: Initial lock acquire() on 5s timeout")
ids_acquire, result = self.test_acquire(timeout=5)
if result:
logging.info("PASSED")
else:
logging.debug("FAILED")
# Attempt to release the locks immediately
logging.info("------------------------------------------------------")
logging.info("TEST 2: Initial lock release() on 2s timeout")
ids_release, result = self.test_release()
if result:
logging.info("PASSED")
else:
logging.debug("FAILED")
logging.info("------------------------------------------------------")
logging.info("TEST 3: Compare instance IDs between acquire() and release()")
print ids_acquire, ids_release
if ids_acquire == ids_release:
logging.info("PASSED")
else:
logging.error("FAILED")
# clear redis
self.redis.flushall()
logging.info("Redis flushall() executed")
# Attempt to acquire the first set of locks
logging.info("------------------------------------------------------")
logging.info("TEST 4: Initial lock acquire() on 5s timeout")
ids_acquire, result = self.test_acquire(timeout=5)
if result:
logging.info("PASSED")
else:
logging.debug("FAILED")
time.sleep(5)
# attempt to acquire based on timeout
logging.info("------------------------------------------------------")
logging.info("TEST 5: Attempt lock acquire() after timeout")
ids_acquire, result = self.test_acquire(timeout=5)
if result:
logging.info("PASSED")
else:
logging.debug("FAILED")
return
# wait until the timeout expired
logging.info("waiting for lock timeout to expire")
time.sleep(LOCK_TIMEOUT + 2)
# second
logging.info("running acquire after expire test")
self.test_acquire()
except:
# soft handle traceback
display_traceback()
finally:
self.stop_threads()
def run_tests():
t = LockTest()
t.run(GRedisLock)
run_tests()