annotate Lib/test/test_queue.py @ 64166:d0e9febfa00a 3.1

Merged revisions 84172 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines Restore GIL in nis_cat in case of error. ........
author Martin v. Löwis <martin@v.loewis.de>
date Thu, 19 Aug 2010 09:03:03 +0000
parents 0bb232078b8c
children 2beb25369289 b93cfafcdcad
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 sys
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
5 import threading
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
6 import time
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
7 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
8 from test import support
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
9
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
10 QUEUE_SIZE = 5
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
11
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
12 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
13 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
14
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
15 # 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
16 class _TriggerThread(threading.Thread):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
17 def __init__(self, fn, args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
18 self.fn = fn
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
19 self.args = args
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
20 self.startedEvent = threading.Event()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
21 threading.Thread.__init__(self)
33202
1d0f4c0357d0 Semantic-neutral format and comment changes.
Tim Peters <tim.peters@gmail.com>
parents: 26250
diff changeset
22
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
23 def run(self):
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
24 # 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
25 # 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
26 # 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
27 # 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
28 # 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
29 # 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
30 # 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
31 # 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
32 time.sleep(0.1)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
33 self.startedEvent.set()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
34 self.fn(*self.args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
35
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
36
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
37 # 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
38 # 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
39 # 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
40 # 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
41 # 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
42 # 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
43 # 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
44 # 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
45 # 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
46 # instead.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
47
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
48 class BlockingTestMixin:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
49
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
50 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
51 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
52 self.t.start()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
53 self.result = block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
54 # 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
55 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
56 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
57 block_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
58 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
59 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
60 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
61 trigger_func)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
62 return self.result
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
63
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
64 # 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
65 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
66 trigger_args, expected_exception_class):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
67 self.t = _TriggerThread(trigger_func, trigger_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
68 self.t.start()
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 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
71 block_func(*block_args)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
72 except expected_exception_class:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
73 raise
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
74 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
75 self.fail("expected exception of kind %r" %
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
76 expected_exception_class)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
77 finally:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
78 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
79 if self.t.is_alive():
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
80 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
81 trigger_func)
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
82 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
83 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
84
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
85
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
86 class BaseQueueTest(unittest.TestCase, BlockingTestMixin):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
87 def setUp(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
88 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
89 self.cumlock = threading.Lock()
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
90
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
91 def simple_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
92 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
93 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
94 # 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
95 q.put(111)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
96 q.put(333)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
97 q.put(222)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
98 target_order = dict(Queue = [111, 333, 222],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
99 LifoQueue = [222, 333, 111],
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
100 PriorityQueue = [111, 222, 333])
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
101 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
102 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
103 "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
104 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
105 q.put(i)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
106 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
107 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
108 last = 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
109 full = 3 * 2 * QUEUE_SIZE
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
110 q.put(last)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
111 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
112 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
113 q.put(full, block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
114 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
115 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
116 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
117 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
118 q.put(full, timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
119 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
120 except queue.Full:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
121 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
122 # Test a blocking put
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,), q.get, ())
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
124 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
125 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
126 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
127 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
128 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
129 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
130 q.get(block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
131 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
132 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
133 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
134 try:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
135 q.get(timeout=0.01)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
136 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
137 except queue.Empty:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
138 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
139 # Test a blocking get
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
140 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
141 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
142
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
143
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
144 def worker(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
145 while True:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
146 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
147 if x < 0:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
148 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
149 return
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
150 with self.cumlock:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
151 self.cum += x
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
152 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
153
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
154 def queue_join_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
155 self.cum = 0
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
156 for i in (0,1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
157 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
158 for i in range(100):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
159 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
160 q.join()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
161 self.assertEquals(self.cum, sum(range(100)),
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
162 "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
163 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
164 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
165 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
166
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
167 def test_queue_task_done(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
168 # 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
169 q = self.type2test()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
170 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
171 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
172 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
173 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
174 else:
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
175 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
176
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
177 def test_queue_join(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
178 # 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
179 # (done twice for insurance).
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
180 q = self.type2test()
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 self.queue_join_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
183 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
184 q.task_done()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
185 except ValueError:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
186 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
187 else:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
188 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
189
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
190 def test_simple_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
191 # 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
192 # 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
193 q = self.type2test(QUEUE_SIZE)
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 self.simple_queue_test(q)
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
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
198 class QueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
199 type2test = queue.Queue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
200
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
201 class LifoQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
202 type2test = queue.LifoQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
203
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
204 class PriorityQueueTest(BaseQueueTest):
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
205 type2test = queue.PriorityQueue
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
206
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
207
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
208
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
209 # 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
210 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
211 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
212
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
213 class FailingQueue(queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
214 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
215 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
216 self.fail_next_get = False
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
217 queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
218 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
219 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
220 self.fail_next_put = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
221 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
222 return queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
223 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
224 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
225 self.fail_next_get = False
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 41642
diff changeset
226 raise FailingQueueException("You Lose")
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46243
diff changeset
227 return queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
228
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
229 class FailingQueueTest(unittest.TestCase, BlockingTestMixin):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
230
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
231 def failing_queue_test(self, q):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
232 if q.qsize():
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
233 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
234 for i in range(QUEUE_SIZE-1):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
235 q.put(i)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
236 # Test a failing non-blocking put.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
237 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
238 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
239 q.put("oops", block=0)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
240 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
241 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
242 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
243 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
244 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
245 q.put("oops", timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
246 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
247 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
248 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
249 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
250 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
251 # Test a failing blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
252 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
253 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
254 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
255 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
256 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
257 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
258 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
259 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
260 q.put("last")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
261 # Test a failing timeout put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
262 q.fail_next_put = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
263 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
264 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
265 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
266 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
267 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
268 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
269 # Check the Queue isn't damaged.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
270 # put failed, but get succeeded - re-add
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
271 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
272 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
273 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
274 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
275 q.put("last")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
276 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
277 # Test a blocking put
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
278 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
279 # Empty it
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
280 for i in range(QUEUE_SIZE):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
281 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
282 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
283 q.put("first")
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
284 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
285 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
286 q.get()
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
287 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
288 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
289 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
290 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
291 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
292 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
293 q.get(timeout=0.1)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
294 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
295 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
296 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
297 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
298 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
299 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
300 q.fail_next_get = True
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
301 try:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
302 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
303 FailingQueueException)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
304 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
305 except FailingQueueException:
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
306 pass
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
307 # put succeeded, but get failed.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
308 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
309 q.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47615
diff changeset
310 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
311
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
312 def test_failing_queue(self):
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
313 # 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
314 # Done twice to the same instance.
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
315 q = FailingQueue(QUEUE_SIZE)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
316 self.failing_queue_test(q)
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
317 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
318
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
319
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
320 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
321 support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest,
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
322 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
323
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33203
diff changeset
324
45177
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
325 if __name__ == "__main__":
01d1a4ffd5b3 Merge r60528, r60534 and r60539 from trunk.
Georg Brandl <georg@python.org>
parents: 44742
diff changeset
326 test_main()