-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
746 lines (561 loc) · 20.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# -*- coding: utf-8 -*-
import os
import sys
from json import dumps
from itertools import chain
from time import time, sleep
from math import sqrt
from datetime import datetime, timedelta
from contextlib import contextmanager
from unittest import TestCase, SkipTest
from logging import getLogger, StreamHandler, INFO, CRITICAL
from threading import Event, Thread, current_thread
from psycopg2cffi.pool import ThreadedConnectionPool
from psycopg2cffi.extensions import cursor
from psycopg2cffi.extras import NamedTupleCursor
from psycopg2cffi import ProgrammingError
from pq import (
PQ,
Queue,
)
from pq.tasks import Queue as TaskQueue
from pq.utils import logger
# Set up logging such that we can quickly enable logging for a
# particular queue instance (via the `logging` flag).
getLogger('pq').setLevel(INFO)
getLogger('pq').addHandler(StreamHandler())
# Make rescheduling test suite less verbose
getLogger('pq.tasks').setLevel(CRITICAL)
def mean(values):
return sum(values) / float(len(values))
def stdev(values, c):
n = len(values)
ss = sum((x - c) ** 2 for x in values)
ss -= sum((x - c) for x in values) ** 2 / n
return sqrt(ss / (n - 1))
class LoggingCursor(cursor):
logger = getLogger('sql')
def execute(self, sql, args=None):
self.logger.info(self.mogrify(sql, args))
try:
cursor.execute(self, sql, args)
except Exception as exc:
self.logger.error("%s: %s" % (exc.__class__.__name__, exc))
raise
class BaseTestCase(TestCase):
base_concurrency = 1
queue_class = Queue
CURSOR_FACTORY = None
@classmethod
def setUpClass(cls):
c = cls.base_concurrency * 4
pool = cls.pool = ThreadedConnectionPool(
c, c, "dbname=%s user=%s port=%s host=%s password=%s" % (
os.environ.get('PQ_TEST_DB', 'pq_test'),
os.environ.get('PQ_TEST_USER', 'postgres'),
os.environ.get('PQ_TEST_DB_PORT', '5432'),
os.environ.get('PQ_TEST_DB_HOST', 'localhost'),
os.environ.get('PQ_TEST_DB_PASS', '')),
cursor_factory=cls.CURSOR_FACTORY
)
cls.pq = PQ(
pool=pool, table="queue", queue_class=cls.queue_class,
)
def setup_filter(record):
return False
logger.addFilter(setup_filter)
try:
cls.pq.create()
except ProgrammingError as exc:
if exc.pgcode != '42P07':
raise
finally:
logger.removeFilter(setup_filter)
@classmethod
def tearDownClass(cls):
cls.pq.close()
def setUp(self):
self.queues = []
self.start = time()
def tearDown(self):
for queue in self.queues:
queue.clear()
def make_one(self, name):
queue = self.pq[name]
self.queues.append(queue)
queue.clear()
return queue
class CursorTest(BaseTestCase):
CURSOR_FACTORY = NamedTupleCursor
def test_get(self):
queue = self.make_one("test")
queue.put({'foo': 'bar'})
job = queue.get()
self.assertEqual(job.data, {'foo': 'bar'})
def test_returning_connections(self):
try:
with self.pq['test'] as cursor:
self.assertTrue(self.pool._used)
raise Exception('test')
except:
pass
self.assertFalse(self.pool._used)
class QueueTest(BaseTestCase):
base_concurrency = 4
@contextmanager
def assertExecutionTime(self, condition, start=None):
yield
seconds = time() - (start or self.start)
self.assertTrue(condition(seconds), seconds)
def test_put_and_get(self):
queue = self.make_one("test")
queue.put({'foo': 'bar'})
self.assertEqual(queue.get().data, {'foo': 'bar'})
def test_put_and_get_pickle(self):
queue = self.make_one("test/pickle")
dt = datetime.utcnow()
queue.put({'time': dt, 'text': '…æøå!'})
self.assertEqual(queue.get().data, {'time': dt, 'text': '…æøå!'})
def test_put_expected_at(self):
queue = self.make_one("test")
queue.put(1, None, "1s")
queue.put(2, None, "2s")
queue.put(3, None, "3s")
queue.put(6)
queue.put(4, None, timedelta(seconds=4))
queue.put(5, None, timedelta(seconds=5) + datetime.utcnow())
t = time()
self.assertEqual(len(queue), 6)
for i, job in enumerate(queue):
if job is None:
break
self.assertEqual(i + 1, job.data)
d = time() - t
self.assertTrue(d < 1)
is_null = job.expected_at is None
if job.data == 6:
self.assertTrue(is_null)
else:
self.assertFalse(is_null)
# We expect five plus the empty.
self.assertEqual(i, 6)
def test_put_schedule_at_without_blocking(self):
queue = self.make_one("test_schedule_at_non_blocking")
queue.put({'baz': 'fob'}, timedelta(seconds=6))
def get(block=True): return queue.get(block, 5)
self.assertEqual(queue.get(False, 5), None)
def test_put_schedule_at(self):
queue = self.make_one("test_schedule_at_blocking")
queue.put({'bar': 'foo'})
queue.put({'baz': 'fob'}, "6s")
queue.put({'foo': 'bar'}, timedelta(seconds=2))
queue.put({'boo': 'baz'}, timedelta(seconds=4) + datetime.utcnow())
# We use a timeout of five seconds for this test.
def get(block=True): return queue.get(block, 5)
# First item is immediately available.
self.assertEqual(get(False).data, {'bar': 'foo'})
with self.assertExecutionTime(lambda seconds: 2 < seconds < 3):
self.assertEqual(get().data, {'foo': 'bar'})
with self.assertExecutionTime(lambda seconds: 4 < seconds < 5):
self.assertEqual(get().data, {'boo': 'baz'})
with self.assertExecutionTime(lambda seconds: 6 < seconds < 7):
self.assertEqual(get().data, {'baz': 'fob'})
# This ensures the timeout has been reset
with self.assertExecutionTime(
lambda seconds: queue.timeout < seconds < queue.timeout + 1,
time(),
):
self.assertEqual(get(), None)
def test_get_and_set(self):
queue = self.make_one("test")
queue.put({'foo': 'bar'})
job = queue.get()
self.assertEqual(job.data, {'foo': 'bar'})
job.data = {'foo': 'boo'}
with queue._transaction() as cursor:
cursor.execute(
"SELECT data FROM %s WHERE id = %s",
(queue.table, job.id)
)
data = cursor.fetchone()[0]
self.assertEqual(job.data, data)
self.assertIn('size=%d' % len(dumps(data)), repr(job))
def test_get_not_empty(self):
queue = self.make_one("test")
queue.put({'foo': 'bar'})
with self.assertExecutionTime(lambda seconds: 0 < seconds < 0.1):
self.assertEqual(queue.get().data, {'foo': 'bar'})
def test_get_empty_blocking_timeout(self):
queue = self.make_one("test_blocking_timeout")
with self.assertExecutionTime(lambda seconds: seconds > 2):
self.assertEqual(queue.get(timeout=2), None)
def test_get_empty_non_blocking_timeout(self):
queue = self.make_one("test")
with self.assertExecutionTime(lambda seconds: seconds > 0.1):
self.assertEqual(queue.get(), None)
def test_get_empty_thread_puts(self):
queue = self.make_one("test")
def target():
sleep(0.2)
queue.put({'foo': 'bar'})
thread = Thread(target=target)
thread.start()
with self.assertExecutionTime(lambda seconds: seconds < 1):
self.assertEqual(queue.get().data, {'foo': 'bar'})
thread.join()
def test_get_empty_thread_puts_after_timeout(self):
queue = self.make_one("test")
def target():
sleep(1.2)
queue.put({'foo': 'bar'})
thread = Thread(target=target)
thread.start()
with self.assertExecutionTime(lambda seconds: seconds > 0.1):
self.assertEqual(queue.get(), None)
thread.join()
def test_get_empty_non_blocking_thread_puts(self):
queue = self.make_one("test")
def target():
sleep(0.1)
queue.put({'foo': 'bar'})
thread = Thread(target=target)
thread.start()
t = time()
self.assertEqual(queue.get(False), None, time() - t)
sleep(0.2)
self.assertEqual(queue.get(False).data, {'foo': 'bar'})
thread.join()
def test_iter(self):
queue = self.make_one("test")
data = {'foo': 'bar'}
queue.put(data)
def target():
for i in range(5):
sleep(0.2)
queue.put(data)
thread = Thread(target=target)
thread.start()
dt = []
iterator = enumerate(queue)
try:
while True:
t = time()
i, job = next(iterator)
self.assertEqual(data, job.data, (i, time() - t))
dt.append(time() - t)
if i == 5:
break
finally:
thread.join()
self.assertGreater(0.1, dt[0])
self.assertGreater(dt[1], 0.1)
def test_length(self):
queue = self.make_one("test")
self.assertEqual(len(queue), 0)
queue.put({'foo': 'bar'})
self.assertEqual(len(queue), 1)
queue.put({'foo': 'bar'}, schedule_at='1s')
self.assertEqual(
len(queue),
1,
'Length should still be 1 with job scheduled for the future.',
)
queue.put({'foo': 'bar'})
self.assertEqual(len(queue), 2)
queue.get()
self.assertEqual(len(queue), 1)
sleep(1)
self.assertEqual(
len(queue),
2,
'Length should be back to 2 in the future.',
)
def test_benchmark(self, items=1000):
if not os.environ.get("BENCHMARK"):
raise SkipTest
event = Event()
queue = self.make_one("benchmark")
iterations = 1000
threads = []
# Queue.put
def put(iterations=iterations):
threads.append(current_thread())
event.wait()
for i in range(iterations):
queue.put({})
for i in range(self.base_concurrency):
Thread(target=put).start()
while len(threads) < self.base_concurrency:
sleep(0.01)
t = time()
event.set()
while threads:
threads.pop().join()
elapsed = time() - t
put_throughput = iterations * self.base_concurrency / elapsed
event.clear()
# Queue.get
def get(iterations=iterations):
threads.append(current_thread())
event.wait()
iterator = iter(queue)
iterator.timeout = 0.1
for item in iterator:
if item is None:
break
for i in range(self.base_concurrency):
Thread(target=get).start()
while len(threads) < self.base_concurrency:
sleep(0.1)
t = time()
event.set()
while threads:
threads.pop().join()
elapsed = time() - t
get_throughput = iterations * self.base_concurrency / elapsed
event.clear()
p_times = {}
# Queue.__iter__
def producer():
thread = current_thread()
times = p_times[thread] = []
active[0].append(thread)
event.wait()
i = 0
while True:
t = time()
queue.put({})
times.append(time() - t)
i += 1
if i > items / self.base_concurrency:
break
c_times = {}
def consumer(consumed):
thread = current_thread()
times = c_times[thread] = []
active[1].append(thread)
event.wait()
iterator = iter(queue)
iterator.timeout = 0.1
while True:
t = time()
try:
item = next(iterator)
except StopIteration:
break
if item is None:
break
times.append(time() - t)
consumed.append(item)
# Use a trial run to warm up JIT (when using PyPy).
warmup = '__pypy__' in sys.builtin_module_names
c = self.base_concurrency
for x in range(1 + int(warmup)):
cs = [[] for i in range(c * 2)]
threads = (
[Thread(target=producer) for i in range(c)],
[Thread(target=consumer, args=(cs[i], )) for i in range(c)],
)
active = ([], [])
for thread in chain(*threads):
thread.start()
while map(len, active) != map(len, threads):
sleep(0.1)
event.set()
t = time()
for thread in chain(*threads):
thread.join()
event.clear()
elapsed = time() - t
consumed = sum(map(len, cs))
c_times = [1000000 * v for v in chain(*c_times.values())]
p_times = [1000000 * v for v in chain(*p_times.values())]
get_latency = mean(c_times)
put_latency = mean(p_times)
get_stdev = stdev(c_times, get_latency)
put_stdev = stdev(p_times, put_latency)
sys.stderr.write("complete.\n>>> stats: %s ... " % (
"\n ".join(
"%s: %s" % item for item in (
("threads ", c),
("consumed ", consumed),
("remaining ", len(queue)),
("throughput ", "%d items/s" % (consumed / elapsed)),
("get ", "%d items/s" % (get_throughput)),
("put ", "%d items/s" % (put_throughput)),
("get (mean avg)", "%d μs/item" % (get_latency)),
("put (mean avg)", "%d μs/item" % (put_latency)),
("get (stdev) ", "%d μs/item" % (get_stdev)),
("put (stdev) ", "%d μs/item" % (put_stdev)),
))))
def test_producer_consumer_threaded(self):
queue = self.make_one("test")
def producer():
ident = current_thread().ident
for i in range(100):
queue.put((ident, i))
consumed = []
def consumer():
while True:
for d in queue:
if d is None:
return
consumed.append(tuple(d.data))
c = self.base_concurrency
producers = [Thread(target=producer) for i in range(c * 2)]
consumers = [Thread(target=consumer) for i in range(c)]
for t in producers:
t.start()
for t in consumers:
t.start()
for t in producers:
t.join()
for t in consumers:
t.join()
self.assertEqual(len(consumed), len(set(consumed)))
def test_context_manager_put(self):
queue = self.make_one("test")
with queue:
queue.put({'foo': 'bar'})
job = queue.get()
self.assertEqual(job.data, {'foo': 'bar'})
def test_context_manager_get_and_set(self):
queue = self.make_one("test")
queue.put({'foo': 'bar'})
with queue:
job = queue.get()
self.assertEqual(job.data, {'foo': 'bar'})
job.data = {'foo': 'boo'}
with queue as cursor:
cursor.execute(
"SELECT data FROM %s WHERE id = %s",
(queue.table, job.id)
)
data = cursor.fetchone()[0]
self.assertEqual(job.data, data)
self.assertIn('size=%d' % len(dumps(data)), repr(job))
def test_context_manager_exception(self):
queue = self.make_one("test")
def test():
with queue:
queue.put({'foo': 'bar'})
raise ValueError()
self.assertRaises(ValueError, test)
self.assertEqual(queue.get(), None)
def test_queue_name_with_space(self):
queue = self.make_one("test two")
queue.put({'foo': 'bar'})
with self.assertExecutionTime(lambda seconds: 0 < seconds < 0.1):
self.assertEqual(queue.get().data, {'foo': 'bar'})
def test_long_queue_name(self):
queue = self.make_one("a" * 64)
queue.put({'foo': 'bar'})
with self.assertExecutionTime(lambda seconds: 0 < seconds < 0.1, time()):
self.assertEqual(queue.get().data, {'foo': 'bar'})
# The queue is now empty. We expect a timeout when pulling an item
# in blocking mode.
with self.assertExecutionTime(lambda seconds: 0 < seconds < 0.3, time()):
self.assertEqual(queue.get(timeout=0.25), None)
queue.put({'foo': 'bar'})
with self.assertExecutionTime(lambda seconds: 0 < seconds < 0.1, time()):
self.assertEqual(queue.get(block=False).data, {'foo': 'bar'})
class TaskTest(BaseTestCase):
queue_class = TaskQueue
@staticmethod
def job(increment):
global test_value
test_value += increment
return True
def test_task(self):
queue = self.make_one("jobs")
global test_value
test_value = 0
# Decorate the static method with our queue task...
job_handler = queue.task()(self.__class__.job)
self.assertEqual(job_handler._path, 'tests.TaskTest.job')
self.assertIn(job_handler._path, queue.handler_registry)
# Should work without the registry cache...
queue.handler_registry.pop(job_handler._path)
job_handler(12)
self.assertEqual(test_value, 0)
self.assertEqual(len(queue), 1)
self.assertTrue(queue.perform(queue.get()))
self.assertEqual(test_value, 12)
del test_value
def test_task_arguments(self):
queue = self.make_one("jobs")
@queue.task(None, expected_at='1s')
def job_handler(value):
return value
job_handler('test')
job = queue.get()
self.assertFalse(job is None)
self.assertFalse(job.expected_at is None)
def test_task_override_job_arguments(self):
queue = self.make_one("jobs")
@queue.task(expected_at='1s')
def job_handler(value):
return value
schedule_at = datetime.utcnow() - timedelta(seconds=5)
expected_at = datetime.utcnow() + timedelta(seconds=6)
job_handler(
'test',
_schedule_at=schedule_at,
_expected_at=expected_at,
)
job = queue.get()
self.assertFalse(job is None)
self.assertEqual(job.expected_at, expected_at)
self.assertEqual(job.schedule_at, schedule_at)
def test_work(self):
queue = self.make_one("jobs")
global test_value
test_value = 1
@queue.task()
def job_handler(increment):
global test_value
test_value += increment
return True
job_handler(26)
self.assertEqual(test_value, 1)
queue.work(True)
self.assertEqual(test_value, 27)
del test_value
def test_removed_job(self):
queue = self.make_one("jobs")
@queue.task()
def job_handler(value):
return value
del queue.handler_registry[job_handler._path]
job_handler(42)
queue.work(True)
def test_worker_no_breaking_exception(self):
queue = self.make_one("jobs")
global test_value
test_value = 3
@queue.task()
def job_handler(increment):
if increment < 0:
raise Exception()
global test_value
test_value += increment
return True
job_handler(-100)
job_handler(2)
self.assertEqual(test_value, 3)
queue.work(True)
self.assertEqual(test_value, 5)
del test_value
def test_worker_reschedule_failing_job(self):
queue = self.make_one("jobs")
global test_value
test_value = 0
@queue.task(max_retries=2, retry_in=None)
def job_handler(increment):
global test_value
test_value += increment
raise Exception()
job_handler(1)
queue.work(True)
self.assertEqual(test_value, 3)
del test_value