annotate Lib/test/test_queue.py @ 58765:2ba7677bfa95 legacy-trunk

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