annotate Lib/test/test_queue.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents c349f9fbc704
children dba99828d91c 6768b2350c57
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
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
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)
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
103 self.assert_(not q.empty(), "Queue should not be empty")
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
104 self.assert_(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
105 q.put("last")
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
106 self.assert_(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
107 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
108 q.put("full", block=0)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
109 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
110 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
111 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
112 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
113 q.put("full", timeout=0.01)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
114 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
115 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
116 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
117 # 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
118 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
119 self.do_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
120 # Empty it
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
121 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
122 q.get()
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
123 self.assert_(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
124 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
125 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
126 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
127 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
128 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
129 try:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
130 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
131 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
132 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
133 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
134 # 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
135 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
136 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
137
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
138
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
139 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
140 while True:
45116
b2de97eb1f67 Fix a conversion mistake that caused test_queue to fail intermittently.
Georg Brandl <georg@python.org>
parents: 45111
diff changeset
141 x = q.get()
b2de97eb1f67 Fix a conversion mistake that caused test_queue to fail intermittently.
Georg Brandl <georg@python.org>
parents: 45111
diff changeset
142 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
143 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
144 return
45121
24969405e1aa Use context manager for a lock.
Brett Cannon <bcannon@gmail.com>
parents: 45116
diff changeset
145 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
146 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
147 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
148
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
149 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
150 self.cum = 0
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
151 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
152 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
153 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
154 q.put(i)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
155 q.join()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
156 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
157 "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
158 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
159 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
160 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
161
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
162 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
163 # 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
164 q = self.type2test()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
165 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
166 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
167 except ValueError:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
168 pass
33203
eab21fbf9a70 Stab at SF 1010777: test_queue fails occasionally
Tim Peters <tim.peters@gmail.com>
parents: 33202
diff changeset
169 else:
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
170 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
171
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
172 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
173 # 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
174 # (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
175 q = self.type2test()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
176 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
177 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
178 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
179 q.task_done()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
180 except ValueError:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
181 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
182 else:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
183 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
184
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
185 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
186 # 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
187 # 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
188 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
189 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
190 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
191
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
192
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
193 class QueueTest(BaseQueueTest):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
194 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
195
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
196 class LifoQueueTest(BaseQueueTest):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
197 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
198
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
199 class PriorityQueueTest(BaseQueueTest):
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
200 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
201
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
202
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
203
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
204 # 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
205 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
206 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
207
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
208 class FailingQueue(Queue.Queue):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
209 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
210 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
211 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
212 Queue.Queue.__init__(self, *args)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
213 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
214 if self.fail_next_put:
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 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
217 return Queue.Queue._put(self, item)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
218 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
219 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
220 self.fail_next_get = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
221 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
222 return Queue.Queue._get(self)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
223
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
224 class FailingQueueTest(unittest.TestCase, BlockingTestMixin):
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 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
227 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
228 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
229 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
230 q.put(i)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
231 # 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
232 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
233 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
234 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
235 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
236 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
237 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
238 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
239 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
240 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
241 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
242 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
243 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
244 q.put("last")
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
245 self.assert_(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
246 # 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
247 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
248 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
249 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
250 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
251 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
252 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
253 # 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
254 # 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
255 q.put("last")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
256 # 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
257 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
258 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
259 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
260 FailingQueueException)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
261 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
262 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
263 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
264 # 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
265 # 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
266 q.put("last")
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
267 self.assert_(q.full(), "Queue should be full")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
268 q.get()
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
269 self.assert_(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
270 q.put("last")
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
271 self.assert_(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
272 # 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
273 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
274 # Empty it
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
275 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
276 q.get()
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
277 self.assert_(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
278 q.put("first")
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
279 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
280 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
281 q.get()
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
282 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
283 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
284 pass
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
285 self.assert_(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
286 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
287 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
288 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
289 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
290 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
291 pass
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
292 self.assert_(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
293 q.get()
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
294 self.assert_(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
295 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
296 try:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
297 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
298 FailingQueueException)
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
299 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
300 except FailingQueueException:
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
301 pass
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
302 # put succeeded, but get failed.
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
303 self.assert_(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
304 q.get()
45205
d39e1a0e0965 Revert accidental changes to test_queue in r60605.
Georg Brandl <georg@python.org>
parents: 45181
diff changeset
305 self.assert_(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
306
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
307 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
308 # 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
309 # 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
310 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
311 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
312 self.failing_queue_test(q)
37205
49622d65c29d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37201
diff changeset
313
37207
96e4d53128c9 Don't decrement below zero. And add more tests.
Raymond Hettinger <python@rcn.com>
parents: 37205
diff changeset
314
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
315 def test_main():
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
316 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
317 FailingQueueTest)
37198
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 33203
diff changeset
318
bed3f52f0dc6 SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents: 33203
diff changeset
319
45111
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
320 if __name__ == "__main__":
6d75be2c3d3e Rewrite test_queue as unittest. Written for GHOP by Ian Seyer.
Georg Brandl <georg@python.org>
parents: 44714
diff changeset
321 test_main()