annotate Lib/test/test_queue.py @ 32527:0da660e05e69 legacy-trunk

"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD isn't always set to an integer value when it's defined.
author Tim Peters <tim.peters@gmail.com>
date Wed, 07 Jul 2004 20:42:07 +0000
parents 02d48ed447a8
children 1d0f4c0357d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
1 # Some simple Queue module tests, plus some failure conditions
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
2 # to ensure the Queue locks remain stable
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
3 import Queue
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
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
7
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 23160
diff changeset
8 from test.test_support import verify, TestFailed, verbose
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
9
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
10 queue_size = 5
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
11
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
12 # Execute a function that blocks, and in a seperate thread, a function that
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
13 # triggers the release. Returns the result of the blocking function.
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
14 class _TriggerThread(threading.Thread):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
15 def __init__(self, fn, args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
16 self.fn = fn
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
17 self.args = args
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
18 self.startedEvent = threading.Event()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
19 threading.Thread.__init__(self)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
20 def run(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
21 time.sleep(.1)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
22 self.startedEvent.set()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
23 self.fn(*self.args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
24
26250
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
25 def _doBlockingTest(block_func, block_args, trigger_func, trigger_args):
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
26 t = _TriggerThread(trigger_func, trigger_args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
27 t.start()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
28 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
29 return block_func(*block_args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
30 finally:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
31 # If we unblocked before our thread made the call, we failed!
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
32 if not t.startedEvent.isSet():
26250
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
33 raise TestFailed("blocking function '%r' appeared not to block" %
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
34 block_func)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
35 t.join(1) # make sure the thread terminates
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
36 if t.isAlive():
26250
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
37 raise TestFailed("trigger function '%r' appeared to not return" %
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
38 trigger_func)
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
39
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
40 # 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
41 class FailingQueueException(Exception):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
42 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
43
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
44 class FailingQueue(Queue.Queue):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
45 def __init__(self, *args):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
46 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
47 self.fail_next_get = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
48 Queue.Queue.__init__(self, *args)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
49 def _put(self, item):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
50 if self.fail_next_put:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
51 self.fail_next_put = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
52 raise FailingQueueException, "You Lose"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
53 return Queue.Queue._put(self, item)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
54 def _get(self):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
55 if self.fail_next_get:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
56 self.fail_next_get = False
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
57 raise FailingQueueException, "You Lose"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
58 return Queue.Queue._get(self)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
59
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
60 def FailingQueueTest(q):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
61 if not q.empty():
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
62 raise RuntimeError, "Call this function with an empty queue"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
63 for i in range(queue_size-1):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
64 q.put(i)
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
65 # Test a failing non-blocking put.
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
66 q.fail_next_put = True
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
67 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
68 q.put("oops", block=0)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
69 raise TestFailed("The queue didn't fail when it should have")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
70 except FailingQueueException:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
71 pass
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
72 q.fail_next_put = True
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
73 try:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
74 q.put("oops", timeout=0.1)
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
75 raise TestFailed("The queue didn't fail when it should have")
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
76 except FailingQueueException:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
77 pass
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
78 q.put("last")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
79 verify(q.full(), "Queue should be full")
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
80 # Test a failing blocking put
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
81 q.fail_next_put = True
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
82 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
83 _doBlockingTest( q.put, ("full",), q.get, ())
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
84 raise TestFailed("The queue didn't fail when it should have")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
85 except FailingQueueException:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
86 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
87 # Check the Queue isn't damaged.
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
88 # put failed, but get succeeded - re-add
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
89 q.put("last")
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
90 # Test a failing timeout put
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
91 q.fail_next_put = True
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
92 try:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
93 _doBlockingTest( q.put, ("full", True, 0.2), q.get, ())
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
94 raise TestFailed("The queue didn't fail when it should have")
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
95 except FailingQueueException:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
96 pass
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
97 # Check the Queue isn't damaged.
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
98 # put failed, but get succeeded - re-add
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
99 q.put("last")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
100 verify(q.full(), "Queue should be full")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
101 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
102 verify(not q.full(), "Queue should not be full")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
103 q.put("last")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
104 verify(q.full(), "Queue should be full")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
105 # Test a blocking put
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
106 _doBlockingTest( q.put, ("full",), q.get, ())
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
107 # Empty it
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
108 for i in range(queue_size):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
109 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
110 verify(q.empty(), "Queue should be empty")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
111 q.put("first")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
112 q.fail_next_get = True
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
113 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
114 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
115 raise TestFailed("The queue didn't fail when it should have")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
116 except FailingQueueException:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
117 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
118 verify(not q.empty(), "Queue should not be empty")
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
119 q.fail_next_get = True
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
120 try:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
121 q.get(timeout=0.1)
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
122 raise TestFailed("The queue didn't fail when it should have")
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
123 except FailingQueueException:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
124 pass
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
125 verify(not q.empty(), "Queue should not be empty")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
126 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
127 verify(q.empty(), "Queue should be empty")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
128 q.fail_next_get = True
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
129 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
130 _doBlockingTest( q.get, (), q.put, ('empty',))
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
131 raise TestFailed("The queue didn't fail when it should have")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
132 except FailingQueueException:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
133 pass
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
134 # put succeeded, but get failed.
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
135 verify(not q.empty(), "Queue should not be empty")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
136 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
137 verify(q.empty(), "Queue should be empty")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
138
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
139 def SimpleQueueTest(q):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
140 if not q.empty():
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
141 raise RuntimeError, "Call this function with an empty queue"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
142 # I guess we better check things actually queue correctly a little :)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
143 q.put(111)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
144 q.put(222)
26250
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
145 verify(q.get() == 111 and q.get() == 222,
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
146 "Didn't seem to queue the correct data!")
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
147 for i in range(queue_size-1):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
148 q.put(i)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
149 verify(not q.full(), "Queue should not be full")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
150 q.put("last")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
151 verify(q.full(), "Queue should be full")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
152 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
153 q.put("full", block=0)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
154 raise TestFailed("Didn't appear to block with a full queue")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
155 except Queue.Full:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
156 pass
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
157 try:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
158 q.put("full", timeout=0.1)
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
159 raise TestFailed("Didn't appear to time-out with a full queue")
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
160 except Queue.Full:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
161 pass
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
162 # Test a blocking put
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
163 _doBlockingTest( q.put, ("full",), q.get, ())
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
164 _doBlockingTest( q.put, ("full", True, 0.2), q.get, ())
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
165 # Empty it
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
166 for i in range(queue_size):
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
167 q.get()
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
168 verify(q.empty(), "Queue should be empty")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
169 try:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
170 q.get(block=0)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
171 raise TestFailed("Didn't appear to block with an empty queue")
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
172 except Queue.Empty:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
173 pass
25796
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
174 try:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
175 q.get(timeout=0.1)
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
176 raise TestFailed("Didn't appear to time-out with an empty queue")
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
177 except Queue.Empty:
3eafbde56b21 Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
178 pass
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
179 # Test a blocking get
26250
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
180 _doBlockingTest(q.get, (), q.put, ('empty',))
02d48ed447a8 Style guide reformats. I saw this test fail on a very heavily loaded
Tim Peters <tim.peters@gmail.com>
parents: 25796
diff changeset
181 _doBlockingTest(q.get, (True, 0.2), q.put, ('empty',))
23160
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
182
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
183 def test():
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
184 q=Queue.Queue(queue_size)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
185 # Do it a couple of times on the same queue
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
186 SimpleQueueTest(q)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
187 SimpleQueueTest(q)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
188 if verbose:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
189 print "Simple Queue tests seemed to work"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
190 q = FailingQueue(queue_size)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
191 FailingQueueTest(q)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
192 FailingQueueTest(q)
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
193 if verbose:
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
194 print "Failing Queue tests seemed to work"
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
195
c548e726b3bc Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
196 test()