annotate Lib/test/test_queue.py @ 74006:88198b93ff2f

Issue #13560: Add PyUnicode_EncodeLocale() * Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not available * Document my last changes in Misc/NEWS
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 17 Dec 2011 04:13:41 +0100
parents 3c9232a684ae
children 118f7bdd631b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
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.
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
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
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
5 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
6 from test import support
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59615
diff changeset
7 threading = 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
44675
627f2e2273b9 Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
Raymond Hettinger <python@rcn.com>
parents: 43014
diff changeset
11 def qfull(q):
627f2e2273b9 Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
Raymond Hettinger <python@rcn.com>
parents: 43014
diff changeset
12 return q.maxsize > 0 and q.qsize() == q.maxsize
627f2e2273b9 Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
Raymond Hettinger <python@rcn.com>
parents: 43014
diff changeset
13
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
14 # 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
15 class _TriggerThread(threading.Thread):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
16 def __init__(self, fn, args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
17 self.fn = fn
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
18 self.args = args
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
19 self.startedEvent = threading.Event()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
20 threading.Thread.__init__(self)
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
21
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
22 def run(self):
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
23 # 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
24 # 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
25 # 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
26 # 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
27 # 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
28 # 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
29 # 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
30 # 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
31 time.sleep(0.1)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
32 self.startedEvent.set()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
33 self.fn(*self.args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
34
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
35
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
36 # Execute a function that blocks, and in a separate thread, a function that
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
37 # triggers the release. Returns the result of the blocking function. Caution:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
38 # block_func must guarantee to block until trigger_func is called, and
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
39 # trigger_func must guarantee to change queue state so that block_func can make
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
40 # enough progress to return. In particular, a block_func that just raises an
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
41 # exception regardless of whether trigger_func is called will lead to
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
42 # timing-dependent sporadic failures, and one of those went rarely seen but
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
43 # undiagnosed for years. Now block_func must be unexceptional. If block_func
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
44 # is supposed to raise an exception, call do_exceptional_blocking_test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
45 # instead.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
46
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
47 class BlockingTestMixin:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
48
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
49 def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
50 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
51 self.t.start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
52 self.result = block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
53 # If block_func returned before our thread made the call, we failed!
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
54 if not self.t.startedEvent.is_set():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
55 self.fail("blocking function '%r' appeared not to block" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
56 block_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
57 self.t.join(10) # make sure the thread terminates
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
58 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
59 self.fail("trigger function '%r' appeared to not return" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
60 trigger_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
61 return self.result
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
62
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
63 # Call this instead if block_func is supposed to raise an exception.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
64 def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
65 trigger_args, expected_exception_class):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
66 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
67 self.t.start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
68 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
69 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
70 block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
71 except expected_exception_class:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
72 raise
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
73 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
74 self.fail("expected exception of kind %r" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
75 expected_exception_class)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
76 finally:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
77 self.t.join(10) # make sure the thread terminates
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
78 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
79 self.fail("trigger function '%r' appeared to not return" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
80 trigger_func)
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
81 if not self.t.startedEvent.is_set():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
82 self.fail("trigger thread ended but event never set")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
83
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
84
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
85 class BaseQueueTest(unittest.TestCase, BlockingTestMixin):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
86 def setUp(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
87 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
88 self.cumlock = threading.Lock()
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
89
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
90 def simple_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
91 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
92 raise RuntimeError("Call this function with an empty queue")
63180
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
93 self.assertTrue(q.empty())
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
94 self.assertFalse(q.full())
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
95 # I guess we better check things actually queue correctly a little :)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
96 q.put(111)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
97 q.put(333)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
98 q.put(222)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
99 target_order = dict(Queue = [111, 333, 222],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
100 LifoQueue = [222, 333, 111],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
101 PriorityQueue = [111, 222, 333])
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
102 actual_order = [q.get(), q.get(), q.get()]
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
103 self.assertEqual(actual_order, target_order[q.__class__.__name__],
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
104 "Didn't seem to queue the correct data!")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
105 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
106 q.put(i)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
107 self.assertTrue(q.qsize(), "Queue should not be empty")
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
108 self.assertTrue(not qfull(q), "Queue should not be full")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
109 last = 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
110 full = 3 * 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
111 q.put(last)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
112 self.assertTrue(qfull(q), "Queue should be full")
63180
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
113 self.assertFalse(q.empty())
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
114 self.assertTrue(q.full())
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
115 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
116 q.put(full, block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
117 self.fail("Didn't appear to block with a full queue")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
118 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
119 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
120 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
121 q.put(full, timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
122 self.fail("Didn't appear to time-out with a full queue")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
123 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
124 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
125 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
126 self.do_blocking_test(q.put, (full,), q.get, ())
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
127 self.do_blocking_test(q.put, (full, True, 10), q.get, ())
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
128 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
129 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
130 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
131 self.assertTrue(not q.qsize(), "Queue should be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
132 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
133 q.get(block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
134 self.fail("Didn't appear to block with an empty queue")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
135 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
136 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
137 try:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
138 q.get(timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
139 self.fail("Didn't appear to time-out with an empty queue")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
140 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
141 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
142 # Test a blocking get
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
143 self.do_blocking_test(q.get, (), q.put, ('empty',))
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
144 self.do_blocking_test(q.get, (True, 10), q.put, ('empty',))
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
145
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
146
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
147 def worker(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
148 while True:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
149 x = q.get()
46243
ab4f8c46c3d9 Prevent test_queue from leaking: one worker thread was not stopped.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45177
diff changeset
150 if x < 0:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
151 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
152 return
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
153 with self.cumlock:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
154 self.cum += x
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
155 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
156
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
157 def queue_join_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
158 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
159 for i in (0,1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
160 threading.Thread(target=self.worker, args=(q,)).start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
161 for i in range(100):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
162 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
163 q.join()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
164 self.assertEqual(self.cum, sum(range(100)),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
165 "q.join() did not block until all tasks were done")
46243
ab4f8c46c3d9 Prevent test_queue from leaking: one worker thread was not stopped.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45177
diff changeset
166 for i in (0,1):
ab4f8c46c3d9 Prevent test_queue from leaking: one worker thread was not stopped.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45177
diff changeset
167 q.put(-1) # instruct the threads to close
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
168 q.join() # verify that you can join twice
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
169
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
170 def test_queue_task_done(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
171 # Test to make sure a queue task completed successfully.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
172 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
173 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
174 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
175 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
176 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
177 else:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
178 self.fail("Did not detect task count going negative")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
179
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
180 def test_queue_join(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
181 # Test that a queue join()s successfully, and before anything else
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
182 # (done twice for insurance).
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
183 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
184 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
185 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
186 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
187 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
188 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
189 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
190 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
191 self.fail("Did not detect task count going negative")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
192
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
193 def test_simple_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
194 # Do it a couple of times on the same queue.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
195 # Done twice to make sure works with same instance reused.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
196 q = self.type2test(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
197 self.simple_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
198 self.simple_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
199
63180
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
200 def test_negative_timeout_raises_exception(self):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
201 q = self.type2test(QUEUE_SIZE)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
202 with self.assertRaises(ValueError):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
203 q.put(1, timeout=-1)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
204 with self.assertRaises(ValueError):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
205 q.get(1, timeout=-1)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
206
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
207 def test_nowait(self):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
208 q = self.type2test(QUEUE_SIZE)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
209 for i in range(QUEUE_SIZE):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
210 q.put_nowait(1)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
211 with self.assertRaises(queue.Full):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
212 q.put_nowait(1)
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
213
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
214 for i in range(QUEUE_SIZE):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
215 q.get_nowait()
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
216 with self.assertRaises(queue.Empty):
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
217 q.get_nowait()
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
218
65831
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
219 def test_shrinking_queue(self):
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
220 # issue 10110
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
221 q = self.type2test(3)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
222 q.put(1)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
223 q.put(2)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
224 q.put(3)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
225 with self.assertRaises(queue.Full):
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
226 q.put_nowait(4)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
227 self.assertEqual(q.qsize(), 3)
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
228 q.maxsize = 2 # shrink the queue
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
229 with self.assertRaises(queue.Full):
bc6cce4ab3c6 Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
Raymond Hettinger <python@rcn.com>
parents: 63180
diff changeset
230 q.put_nowait(4)
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
231
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
232 class QueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
233 type2test = queue.Queue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
234
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
235 class LifoQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
236 type2test = queue.LifoQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
237
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
238 class PriorityQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
239 type2test = queue.PriorityQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
240
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
241
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
242
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
243 # 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
244 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
245 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
246
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
247 class FailingQueue(queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
248 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
249 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
250 self.fail_next_get = False
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
251 queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
252 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
253 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
254 self.fail_next_put = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
255 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
256 return queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
257 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
258 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
259 self.fail_next_get = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
260 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
261 return queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
262
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
263 class FailingQueueTest(unittest.TestCase, BlockingTestMixin):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
264
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
265 def failing_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
266 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
267 raise RuntimeError("Call this function with an empty queue")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
268 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
269 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
270 # Test a failing non-blocking put.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
271 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
272 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
273 q.put("oops", block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
274 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
275 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
276 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
277 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
278 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
279 q.put("oops", timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
280 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
281 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
282 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
283 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
284 self.assertTrue(qfull(q), "Queue should be full")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
285 # Test a failing blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
286 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
287 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
288 self.do_blocking_test(q.put, ("full",), q.get, ())
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
289 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
290 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
291 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
292 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
293 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
294 q.put("last")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
295 # Test a failing timeout put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
296 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
297 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
298 self.do_exceptional_blocking_test(q.put, ("full", True, 10), q.get, (),
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
299 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
300 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
301 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
302 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
303 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
304 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
305 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
306 self.assertTrue(qfull(q), "Queue should be full")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
307 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
308 self.assertTrue(not qfull(q), "Queue should not be full")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
309 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
310 self.assertTrue(qfull(q), "Queue should be full")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
311 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
312 self.do_blocking_test(q.put, ("full",), q.get, ())
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
313 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
314 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
315 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
316 self.assertTrue(not q.qsize(), "Queue should be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
317 q.put("first")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
318 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
319 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
320 q.get()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
321 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
322 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
323 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
324 self.assertTrue(q.qsize(), "Queue should not be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
325 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
326 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
327 q.get(timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
328 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
329 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
330 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
331 self.assertTrue(q.qsize(), "Queue should not be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
332 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
333 self.assertTrue(not q.qsize(), "Queue should be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
334 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
335 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
336 self.do_exceptional_blocking_test(q.get, (), q.put, ('empty',),
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
337 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
338 self.fail("The queue didn't fail when it should have")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
339 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
340 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
341 # put succeeded, but get failed.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
342 self.assertTrue(q.qsize(), "Queue should not be empty")
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
343 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
344 self.assertTrue(not q.qsize(), "Queue should be empty")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
345
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
346 def test_failing_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
347 # Test to make sure a queue is functioning correctly.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
348 # Done twice to the same instance.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
349 q = FailingQueue(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
350 self.failing_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
351 self.failing_queue_test(q)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
352
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
353
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
354 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
355 support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest,
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
356 FailingQueueTest)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
357
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
358
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
359 if __name__ == "__main__":
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
360 test_main()