annotate Lib/test/test_queue.py @ 62799:dcc2e8bf64cf

Fix: Issue9091 Minor documentation clarification.
author Senthil Kumaran <orsenthil@gmail.com>
date Mon, 05 Jul 2010 11:41:42 +0000
parents 493bb4585e38
children af9341324d3d
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")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
93 # 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
94 q.put(111)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
95 q.put(333)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
96 q.put(222)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
97 target_order = dict(Queue = [111, 333, 222],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
98 LifoQueue = [222, 333, 111],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
99 PriorityQueue = [111, 222, 333])
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
100 actual_order = [q.get(), q.get(), q.get()]
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
101 self.assertEquals(actual_order, target_order[q.__class__.__name__],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
102 "Didn't seem to queue the correct data!")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
103 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
104 q.put(i)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
105 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
106 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
107 last = 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
108 full = 3 * 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
109 q.put(last)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
110 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
111 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
112 q.put(full, block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
113 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
114 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
115 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
116 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
117 q.put(full, timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
118 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
119 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
120 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
121 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
122 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
123 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
124 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
125 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
126 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
127 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
128 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
129 q.get(block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
130 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
131 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
132 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
133 try:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
134 q.get(timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
135 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
136 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
137 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
138 # Test a blocking get
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
139 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
140 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
141
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
142
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
143 def worker(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
144 while True:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
145 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
146 if x < 0:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
147 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
148 return
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
149 with self.cumlock:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
150 self.cum += x
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
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
153 def queue_join_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
154 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
155 for i in (0,1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
156 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
157 for i in range(100):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
158 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
159 q.join()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
160 self.assertEquals(self.cum, sum(range(100)),
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
161 "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
162 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
163 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
164 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
165
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
166 def test_queue_task_done(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
167 # 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
168 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
169 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
170 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
171 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
172 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
173 else:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
174 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
175
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
176 def test_queue_join(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
177 # 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
178 # (done twice for insurance).
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
179 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
180 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
181 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
182 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
183 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
184 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
185 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
186 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
187 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
188
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
189 def test_simple_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
190 # 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
191 # 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
192 q = self.type2test(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
193 self.simple_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
194 self.simple_queue_test(q)
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
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
197 class QueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
198 type2test = queue.Queue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
199
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
200 class LifoQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
201 type2test = queue.LifoQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
202
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
203 class PriorityQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
204 type2test = queue.PriorityQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
205
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
206
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
207
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
208 # 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
209 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
210 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
211
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
212 class FailingQueue(queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
213 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
214 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
215 self.fail_next_get = False
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
216 queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
217 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
218 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
219 self.fail_next_put = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
220 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
221 return queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
222 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
223 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
224 self.fail_next_get = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
225 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
226 return queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
227
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
228 class FailingQueueTest(unittest.TestCase, BlockingTestMixin):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
229
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
230 def failing_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
231 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
232 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
233 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
234 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
235 # Test a failing non-blocking put.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
236 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
237 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
238 q.put("oops", block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
239 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
240 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
241 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
242 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
243 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
244 q.put("oops", timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
245 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
246 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
247 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
248 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
249 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
250 # Test a failing blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
251 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
252 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
253 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
254 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
255 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
256 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
257 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
258 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
259 q.put("last")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
260 # Test a failing timeout put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
261 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
262 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
263 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
264 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
265 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
266 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
267 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
268 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
269 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
270 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
271 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
272 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
273 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
274 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
275 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
276 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
277 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
278 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
279 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
280 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
281 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
282 q.put("first")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
283 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
284 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
285 q.get()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
286 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
287 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
288 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
289 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
290 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
291 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
292 q.get(timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
293 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
294 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
295 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
296 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
297 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
298 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
299 q.fail_next_get = 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.get, (), q.put, ('empty',),
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 # put succeeded, but get failed.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
307 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
308 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
309 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
310
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
311 def test_failing_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
312 # 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
313 # Done twice to the same instance.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
314 q = FailingQueue(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
315 self.failing_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
316 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
317
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
318
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
319 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
320 support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest,
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
321 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
322
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
323
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
324 if __name__ == "__main__":
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
325 test_main()