annotate Lib/test/test_queue.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 340053208864
children 6e21215acb1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46897
76681a6306a8 Added stub for the Queue module to be renamed in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45205
diff changeset
1 # Some simple queue module tests, plus some failure conditions
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
2 # to ensure the Queue locks remain stable.
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
3 import Queue
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
4 import time
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
5 import unittest
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
6 from test import test_support
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58817
diff changeset
7 threading = test_support.import_module('threading')
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
8
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
9 QUEUE_SIZE = 5
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
10
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
11 # A thread to run a function that unclogs a blocked Queue.
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
12 class _TriggerThread(threading.Thread):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
13 def __init__(self, fn, args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
14 self.fn = fn
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
15 self.args = args
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
16 self.startedEvent = threading.Event()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
17 threading.Thread.__init__(self)
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
18
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
19 def run(self):
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
20 # The sleep isn't necessary, but is intended to give the blocking
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
21 # function in the main thread a chance at actually blocking before
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
22 # we unclog it. But if the sleep is longer than the timeout-based
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
23 # tests wait in their blocking functions, those tests will fail.
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
24 # So we give them much longer timeout values compared to the
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
25 # sleep here (I aimed at 10 seconds for blocking functions --
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
26 # they should never actually wait that long - they should make
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
27 # progress as soon as we call self.fn()).
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
28 time.sleep(0.1)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
29 self.startedEvent.set()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
30 self.fn(*self.args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
31
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
32
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
33 # Execute a function that blocks, and in a separate thread, a function that
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
34 # triggers the release. Returns the result of the blocking function. Caution:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
35 # block_func must guarantee to block until trigger_func is called, and
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
36 # trigger_func must guarantee to change queue state so that block_func can make
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
37 # enough progress to return. In particular, a block_func that just raises an
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
38 # exception regardless of whether trigger_func is called will lead to
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
39 # timing-dependent sporadic failures, and one of those went rarely seen but
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
40 # undiagnosed for years. Now block_func must be unexceptional. If block_func
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
41 # is supposed to raise an exception, call do_exceptional_blocking_test()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
42 # instead.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
43
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
44 class BlockingTestMixin:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
45
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
46 def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
47 self.t = _TriggerThread(trigger_func, trigger_args)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
48 self.t.start()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
49 self.result = block_func(*block_args)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
50 # If block_func returned before our thread made the call, we failed!
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47248
diff changeset
51 if not self.t.startedEvent.is_set():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
52 self.fail("blocking function '%r' appeared not to block" %
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
53 block_func)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
54 self.t.join(10) # make sure the thread terminates
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47248
diff changeset
55 if self.t.is_alive():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
56 self.fail("trigger function '%r' appeared to not return" %
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
57 trigger_func)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
58 return self.result
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
59
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
60 # Call this instead if block_func is supposed to raise an exception.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
61 def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
62 trigger_args, expected_exception_class):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
63 self.t = _TriggerThread(trigger_func, trigger_args)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
64 self.t.start()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
65 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
66 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
67 block_func(*block_args)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
68 except expected_exception_class:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
69 raise
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
70 else:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
71 self.fail("expected exception of kind %r" %
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
72 expected_exception_class)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
73 finally:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
74 self.t.join(10) # make sure the thread terminates
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47248
diff changeset
75 if self.t.is_alive():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
76 self.fail("trigger function '%r' appeared to not return" %
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
77 trigger_func)
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47248
diff changeset
78 if not self.t.startedEvent.is_set():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
79 self.fail("trigger thread ended but event never set")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
80
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
81
75801
340053208864 #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
82 class BaseQueueTest(BlockingTestMixin):
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
83 def setUp(self):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
84 self.cum = 0
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
85 self.cumlock = threading.Lock()
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
86
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
87 def simple_queue_test(self, q):
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
88 if not q.empty():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
89 raise RuntimeError, "Call this function with an empty queue"
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
90 # I guess we better check things actually queue correctly a little :)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
91 q.put(111)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
92 q.put(333)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
93 q.put(222)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
94 target_order = dict(Queue = [111, 333, 222],
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
95 LifoQueue = [222, 333, 111],
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
96 PriorityQueue = [111, 222, 333])
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
97 actual_order = [q.get(), q.get(), q.get()]
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61046
diff changeset
98 self.assertEqual(actual_order, target_order[q.__class__.__name__],
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61046
diff changeset
99 "Didn't seem to queue the correct data!")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
100 for i in range(QUEUE_SIZE-1):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
101 q.put(i)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
102 self.assertTrue(not q.empty(), "Queue should not be empty")
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
103 self.assertTrue(not q.full(), "Queue should not be full")
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
104 last = 2 * QUEUE_SIZE
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
105 full = 3 * 2 * QUEUE_SIZE
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
106 q.put(last)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
107 self.assertTrue(q.full(), "Queue should be full")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
108 try:
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
109 q.put(full, block=0)
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
110 self.fail("Didn't appear to block with a full queue")
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
111 except Queue.Full:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
112 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
113 try:
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
114 q.put(full, timeout=0.01)
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
115 self.fail("Didn't appear to time-out with a full queue")
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
116 except Queue.Full:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
117 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
118 # Test a blocking put
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
119 self.do_blocking_test(q.put, (full,), q.get, ())
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47598
diff changeset
120 self.do_blocking_test(q.put, (full, True, 10), q.get, ())
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
121 # Empty it
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
122 for i in range(QUEUE_SIZE):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
123 q.get()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
124 self.assertTrue(q.empty(), "Queue should be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
125 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
126 q.get(block=0)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
127 self.fail("Didn't appear to block with an empty queue")
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
128 except Queue.Empty:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
129 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
130 try:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
131 q.get(timeout=0.01)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
132 self.fail("Didn't appear to time-out with an empty queue")
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
133 except Queue.Empty:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
134 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
135 # Test a blocking get
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
136 self.do_blocking_test(q.get, (), q.put, ('empty',))
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
137 self.do_blocking_test(q.get, (True, 10), q.put, ('empty',))
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
138
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
139
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
140 def worker(self, q):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
141 while True:
45116
b2de97eb1f67 Fix a conversion mistake that caused test_queue to fail intermittently.
Georg Brandl <georg@python.org>
parents: 45111
diff changeset
142 x = q.get()
b2de97eb1f67 Fix a conversion mistake that caused test_queue to fail intermittently.
Georg Brandl <georg@python.org>
parents: 45111
diff changeset
143 if x is None:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
144 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
145 return
45121
24969405e1aa Use context manager for a lock.
Brett Cannon <bcannon@gmail.com>
parents: 45116
diff changeset
146 with self.cumlock:
45116
b2de97eb1f67 Fix a conversion mistake that caused test_queue to fail intermittently.
Georg Brandl <georg@python.org>
parents: 45111
diff changeset
147 self.cum += x
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
148 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
149
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
150 def queue_join_test(self, q):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
151 self.cum = 0
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
152 for i in (0,1):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
153 threading.Thread(target=self.worker, args=(q,)).start()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
154 for i in xrange(100):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
155 q.put(i)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
156 q.join()
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61046
diff changeset
157 self.assertEqual(self.cum, sum(range(100)),
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61046
diff changeset
158 "q.join() did not block until all tasks were done")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
159 for i in (0,1):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
160 q.put(None) # instruct the threads to close
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
161 q.join() # verify that you can join twice
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
162
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
163 def test_queue_task_done(self):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
164 # Test to make sure a queue task completed successfully.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
165 q = self.type2test()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
166 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
167 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
168 except ValueError:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
169 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
170 else:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
171 self.fail("Did not detect task count going negative")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
172
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
173 def test_queue_join(self):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
174 # Test that a queue join()s successfully, and before anything else
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
175 # (done twice for insurance).
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
176 q = self.type2test()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
177 self.queue_join_test(q)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
178 self.queue_join_test(q)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
179 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
180 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
181 except ValueError:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
182 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
183 else:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
184 self.fail("Did not detect task count going negative")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
185
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
186 def test_simple_queue(self):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
187 # Do it a couple of times on the same queue.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
188 # Done twice to make sure works with same instance reused.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
189 q = self.type2test(QUEUE_SIZE)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
190 self.simple_queue_test(q)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
191 self.simple_queue_test(q)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
192
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
193
75801
340053208864 #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
194 class QueueTest(BaseQueueTest, unittest.TestCase):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
195 type2test = Queue.Queue
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
196
75801
340053208864 #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
197 class LifoQueueTest(BaseQueueTest, unittest.TestCase):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
198 type2test = Queue.LifoQueue
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
199
75801
340053208864 #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
200 class PriorityQueueTest(BaseQueueTest, unittest.TestCase):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
201 type2test = Queue.PriorityQueue
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
202
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
203
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
204
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
205 # A Queue subclass that can provoke failure at a moment's notice :)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
206 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
207 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
208
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
209 class FailingQueue(Queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
210 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
211 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
212 self.fail_next_get = False
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
213 Queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
214 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
215 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
216 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
217 raise FailingQueueException, "You Lose"
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
218 return Queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
219 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
220 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
221 self.fail_next_get = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
222 raise FailingQueueException, "You Lose"
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
223 return Queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
224
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
225 class FailingQueueTest(unittest.TestCase, BlockingTestMixin):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
226
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
227 def failing_queue_test(self, q):
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
228 if not q.empty():
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
229 raise RuntimeError, "Call this function with an empty queue"
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
230 for i in range(QUEUE_SIZE-1):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
231 q.put(i)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
232 # Test a failing non-blocking put.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
233 q.fail_next_put = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
234 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
235 q.put("oops", block=0)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
236 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
237 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
238 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
239 q.fail_next_put = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
240 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
241 q.put("oops", timeout=0.1)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
242 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
243 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
244 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
245 q.put("last")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
246 self.assertTrue(q.full(), "Queue should be full")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
247 # Test a failing blocking put
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
248 q.fail_next_put = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
249 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
250 self.do_blocking_test(q.put, ("full",), q.get, ())
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
251 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
252 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
253 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
254 # Check the Queue isn't damaged.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
255 # put failed, but get succeeded - re-add
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
256 q.put("last")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
257 # Test a failing timeout put
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
258 q.fail_next_put = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
259 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
260 self.do_exceptional_blocking_test(q.put, ("full", True, 10), q.get, (),
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
261 FailingQueueException)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
262 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
263 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
264 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
265 # Check the Queue isn't damaged.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
266 # put failed, but get succeeded - re-add
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
267 q.put("last")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
268 self.assertTrue(q.full(), "Queue should be full")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
269 q.get()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
270 self.assertTrue(not q.full(), "Queue should not be full")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
271 q.put("last")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
272 self.assertTrue(q.full(), "Queue should be full")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
273 # Test a blocking put
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
274 self.do_blocking_test(q.put, ("full",), q.get, ())
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
275 # Empty it
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
276 for i in range(QUEUE_SIZE):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
277 q.get()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
278 self.assertTrue(q.empty(), "Queue should be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
279 q.put("first")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
280 q.fail_next_get = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
281 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
282 q.get()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
283 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
284 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
285 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
286 self.assertTrue(not q.empty(), "Queue should not be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
287 q.fail_next_get = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
288 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
289 q.get(timeout=0.1)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
290 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
291 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
292 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
293 self.assertTrue(not q.empty(), "Queue should not be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
294 q.get()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
295 self.assertTrue(q.empty(), "Queue should be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
296 q.fail_next_get = True
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
297 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
298 self.do_exceptional_blocking_test(q.get, (), q.put, ('empty',),
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
299 FailingQueueException)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
300 self.fail("The queue didn't fail when it should have")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
301 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
302 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
303 # put succeeded, but get failed.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
304 self.assertTrue(not q.empty(), "Queue should not be empty")
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
305 q.get()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47598
diff changeset
306 self.assertTrue(q.empty(), "Queue should be empty")
37198
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 33203
diff changeset
307
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
308 def test_failing_queue(self):
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
309 # Test to make sure a queue is functioning correctly.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
310 # Done twice to the same instance.
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
311 q = FailingQueue(QUEUE_SIZE)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
312 self.failing_queue_test(q)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
313 self.failing_queue_test(q)
37205
49622d65c29d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37201
diff changeset
314
37207
96e4d53128c9 Don't decrement below zero. And add more tests.
Raymond Hettinger <python@rcn.com>
parents: 37205
diff changeset
315
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
316 def test_main():
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
317 test_support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest,
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
318 FailingQueueTest)
37198
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 33203
diff changeset
319
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 33203
diff changeset
320
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
321 if __name__ == "__main__":
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
322 test_main()