annotate Lib/test/test_queue.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 1c7c692de1ee
children
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
82931
1c7c692de1ee Clean up references to threads in test_queue.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75799
diff changeset
49 def tearDown(self):
1c7c692de1ee Clean up references to threads in test_queue.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75799
diff changeset
50 self.t = None
1c7c692de1ee Clean up references to threads in test_queue.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75799
diff changeset
51
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
52 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
53 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
54 self.t.start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
55 self.result = block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
56 # 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
57 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
58 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
59 block_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
60 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
61 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
62 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
63 trigger_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
64 return self.result
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
65
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
66 # 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
67 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
68 trigger_args, expected_exception_class):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
69 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
70 self.t.start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
71 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
72 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
73 block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
74 except expected_exception_class:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
75 raise
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
76 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
77 self.fail("expected exception of kind %r" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
78 expected_exception_class)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
79 finally:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
80 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
81 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
82 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
83 trigger_func)
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
84 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
85 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
86
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
87
75799
118f7bdd631b #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68257
diff changeset
88 class BaseQueueTestMixin(BlockingTestMixin):
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
89 def setUp(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
90 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
91 self.cumlock = threading.Lock()
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
92
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
93 def simple_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
94 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
95 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
96 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
97 self.assertFalse(q.full())
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
98 # 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
99 q.put(111)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
100 q.put(333)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
101 q.put(222)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
102 target_order = dict(Queue = [111, 333, 222],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
103 LifoQueue = [222, 333, 111],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
104 PriorityQueue = [111, 222, 333])
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
105 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
106 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
107 "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
108 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
109 q.put(i)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
110 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
111 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
112 last = 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
113 full = 3 * 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
114 q.put(last)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
115 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
116 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
117 self.assertTrue(q.full())
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
118 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
119 q.put(full, block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
120 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
121 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
122 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
123 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
124 q.put(full, timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
125 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
126 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
127 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
128 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
129 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
130 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
131 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
132 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
133 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
134 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
135 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
136 q.get(block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
137 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
138 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
139 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
140 try:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
141 q.get(timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
142 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
143 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
144 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
145 # Test a blocking get
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
146 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
147 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
148
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
149
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
150 def worker(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
151 while True:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
152 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
153 if x < 0:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
154 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
155 return
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
156 with self.cumlock:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
157 self.cum += x
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
158 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
159
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
160 def queue_join_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
161 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
162 for i in (0,1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
163 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
164 for i in range(100):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
165 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
166 q.join()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
167 self.assertEqual(self.cum, sum(range(100)),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
168 "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
169 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
170 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
171 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
172
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
173 def test_queue_task_done(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
174 # 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
175 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
176 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
177 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
178 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
179 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
180 else:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
181 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
182
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
183 def test_queue_join(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
184 # 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
185 # (done twice for insurance).
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
186 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
187 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
188 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
189 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
190 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
191 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
192 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
193 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
194 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
195
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
196 def test_simple_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
197 # 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
198 # 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
199 q = self.type2test(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
200 self.simple_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
201 self.simple_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
202
63180
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
203 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
204 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
205 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
206 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
207 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
208 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
209
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
210 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
211 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
212 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
213 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
214 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
215 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
216
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
217 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
218 q.get_nowait()
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
219 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
220 q.get_nowait()
af9341324d3d Add queue tests for empty, full, put_nowait, and get_nowait.
Brett Cannon <bcannon@gmail.com>
parents: 61073
diff changeset
221
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
222 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
223 # 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
224 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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 q.put_nowait(4)
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
234
75799
118f7bdd631b #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68257
diff changeset
235 class QueueTest(BaseQueueTestMixin, unittest.TestCase):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
236 type2test = queue.Queue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
237
75799
118f7bdd631b #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68257
diff changeset
238 class LifoQueueTest(BaseQueueTestMixin, unittest.TestCase):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
239 type2test = queue.LifoQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
240
75799
118f7bdd631b #14333: fix test_queue so it can be run via standard unittest test discovery.
R David Murray <rdmurray@bitdance.com>
parents: 68257
diff changeset
241 class PriorityQueueTest(BaseQueueTestMixin, unittest.TestCase):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
242 type2test = queue.PriorityQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
243
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
244
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
245
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
246 # 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
247 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
248 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
249
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
250 class FailingQueue(queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
251 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
252 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
253 self.fail_next_get = False
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
254 queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
255 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
256 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
257 self.fail_next_put = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
258 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
259 return queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
260 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
261 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
262 self.fail_next_get = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
263 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
264 return queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
265
82931
1c7c692de1ee Clean up references to threads in test_queue.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 75799
diff changeset
266 class FailingQueueTest(BlockingTestMixin, unittest.TestCase):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
267
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
268 def failing_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
269 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
270 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
271 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
272 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
273 # Test a failing non-blocking put.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
274 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
275 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
276 q.put("oops", block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
277 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
278 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
279 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
280 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
281 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
282 q.put("oops", timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
283 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
284 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
285 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
286 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
287 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
288 # Test a failing blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
289 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
290 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
291 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
292 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
293 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
294 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
295 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
296 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
297 q.put("last")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
298 # Test a failing timeout put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
299 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
300 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
301 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
302 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
303 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
304 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
305 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
306 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
307 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
308 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
309 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
310 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
311 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
312 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
313 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
314 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
315 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
316 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
317 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
318 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
319 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
320 q.put("first")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
321 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
322 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
323 q.get()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
324 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
325 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
326 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
327 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
328 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
329 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
330 q.get(timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
331 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
332 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
333 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
334 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
335 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
336 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
337 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
338 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
339 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
340 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
341 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
342 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
343 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
344 # put succeeded, but get failed.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
345 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
346 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
347 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
348
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
349 def test_failing_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
350 # 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
351 # Done twice to the same instance.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
352 q = FailingQueue(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
353 self.failing_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
354 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
355
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
356
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
357 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
358 support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest,
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
359 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
360
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
361
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
362 if __name__ == "__main__":
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
363 test_main()