Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_queue.py @ 25796:3eafbde56b21 legacy-trunk
Patch #572628: Optional timeouts for put and get.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Tue, 15 Oct 2002 15:11:13 +0000 |
| parents | aa0d2d51fbde |
| children | 02d48ed447a8 |
| 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 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
25 def _doBlockingTest( block_func, block_args, trigger_func, trigger_args): |
|
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(): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
33 raise TestFailed("blocking function '%r' appeared not to block" % (block_func,)) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
34 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
|
35 if t.isAlive(): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
36 raise TestFailed("trigger function '%r' appeared to not return" % (trigger_func,)) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
37 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
38 # 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
|
39 class FailingQueueException(Exception): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
40 pass |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
41 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
42 class FailingQueue(Queue.Queue): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
43 def __init__(self, *args): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
44 self.fail_next_put = False |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
45 self.fail_next_get = False |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
46 Queue.Queue.__init__(self, *args) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
47 def _put(self, item): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
48 if self.fail_next_put: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
49 self.fail_next_put = False |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
50 raise FailingQueueException, "You Lose" |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
51 return Queue.Queue._put(self, item) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
52 def _get(self): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
53 if self.fail_next_get: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
54 self.fail_next_get = False |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
55 raise FailingQueueException, "You Lose" |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
56 return Queue.Queue._get(self) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
57 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
58 def FailingQueueTest(q): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
59 if not q.empty(): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
60 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
|
61 for i in range(queue_size-1): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
62 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
|
63 # Test a failing non-blocking put. |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
64 q.fail_next_put = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
65 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
66 q.put("oops", block=0) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
67 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
|
68 except FailingQueueException: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
69 pass |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
70 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
|
71 try: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
72 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
|
73 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
|
74 except FailingQueueException: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
75 pass |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
76 q.put("last") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
77 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
|
78 # Test a failing blocking put |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
79 q.fail_next_put = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
80 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
81 _doBlockingTest( q.put, ("full",), q.get, ()) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
82 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
|
83 except FailingQueueException: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
84 pass |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
85 # Check the Queue isn't damaged. |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
86 # put failed, but get succeeded - re-add |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
87 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
|
88 # 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
|
89 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
|
90 try: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
91 _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
|
92 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
|
93 except FailingQueueException: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
94 pass |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
95 # 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
|
96 # 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
|
97 q.put("last") |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
98 verify(q.full(), "Queue should be full") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
99 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
100 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
|
101 q.put("last") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
102 verify(q.full(), "Queue should be full") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
103 # Test a blocking put |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
104 _doBlockingTest( q.put, ("full",), q.get, ()) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
105 # Empty it |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
106 for i in range(queue_size): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
107 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
108 verify(q.empty(), "Queue should be empty") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
109 q.put("first") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
110 q.fail_next_get = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
111 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
112 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
113 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
|
114 except FailingQueueException: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
115 pass |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
116 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
|
117 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
|
118 try: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
119 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
|
120 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
|
121 except FailingQueueException: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
122 pass |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
123 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
|
124 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
125 verify(q.empty(), "Queue should be empty") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
126 q.fail_next_get = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
127 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
128 _doBlockingTest( q.get, (), q.put, ('empty',)) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
129 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
|
130 except FailingQueueException: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
131 pass |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
132 # put succeeded, but get failed. |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
133 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
|
134 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
135 verify(q.empty(), "Queue should be empty") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
136 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
137 def SimpleQueueTest(q): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
138 if not q.empty(): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
139 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
|
140 # 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
|
141 q.put(111) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
142 q.put(222) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
143 verify(q.get()==111 and q.get()==222, "Didn't seem to queue the correct data!") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
144 for i in range(queue_size-1): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
145 q.put(i) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
146 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
|
147 q.put("last") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
148 verify(q.full(), "Queue should be full") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
149 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
150 q.put("full", block=0) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
151 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
|
152 except Queue.Full: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
153 pass |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
154 try: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
155 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
|
156 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
|
157 except Queue.Full: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
158 pass |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
159 # Test a blocking put |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
160 _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
|
161 _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
|
162 # Empty it |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
163 for i in range(queue_size): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
164 q.get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
165 verify(q.empty(), "Queue should be empty") |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
166 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
167 q.get(block=0) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
168 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
|
169 except Queue.Empty: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
170 pass |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
171 try: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
172 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
|
173 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
|
174 except Queue.Empty: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
175 pass |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
176 # Test a blocking get |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
177 _doBlockingTest( q.get, (), q.put, ('empty',)) |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24364
diff
changeset
|
178 _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
|
179 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
180 def test(): |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
181 q=Queue.Queue(queue_size) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
182 # 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
|
183 SimpleQueueTest(q) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
184 SimpleQueueTest(q) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
185 if verbose: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
186 print "Simple Queue tests seemed to work" |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
187 q = FailingQueue(queue_size) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
188 FailingQueueTest(q) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
189 FailingQueueTest(q) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
190 if verbose: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
191 print "Failing Queue tests seemed to work" |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
192 |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff
changeset
|
193 test() |
