Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 1 | # Some simple queue module tests, plus some failure conditions |
Tim Peters | afe5297 | 2004-08-20 02:37:25 +0000 | [diff] [blame] | 2 | # to ensure the Queue locks remain stable. |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 3 | import queue |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 4 | import time |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 7 | threading = support.import_module('threading') |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 8 | |
Tim Peters | afe5297 | 2004-08-20 02:37:25 +0000 | [diff] [blame] | 9 | QUEUE_SIZE = 5 |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 11 | def qfull(q): |
| 12 | return q.maxsize > 0 and q.qsize() == q.maxsize |
| 13 | |
Tim Peters | afe5297 | 2004-08-20 02:37:25 +0000 | [diff] [blame] | 14 | # A thread to run a function that unclogs a blocked Queue. |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 15 | class _TriggerThread(threading.Thread): |
| 16 | def __init__(self, fn, args): |
| 17 | self.fn = fn |
| 18 | self.args = args |
| 19 | self.startedEvent = threading.Event() |
| 20 | threading.Thread.__init__(self) |
Tim Peters | afe5297 | 2004-08-20 02:37:25 +0000 | [diff] [blame] | 21 | |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 22 | def run(self): |
Tim Peters | 8d7626c | 2004-08-20 03:27:12 +0000 | [diff] [blame] | 23 | # The sleep isn't necessary, but is intended to give the blocking |
| 24 | # function in the main thread a chance at actually blocking before |
| 25 | # we unclog it. But if the sleep is longer than the timeout-based |
| 26 | # tests wait in their blocking functions, those tests will fail. |
| 27 | # So we give them much longer timeout values compared to the |
| 28 | # sleep here (I aimed at 10 seconds for blocking functions -- |
| 29 | # they should never actually wait that long - they should make |
| 30 | # progress as soon as we call self.fn()). |
| 31 | time.sleep(0.1) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 32 | self.startedEvent.set() |
| 33 | self.fn(*self.args) |
| 34 | |
Tim Peters | 8d7626c | 2004-08-20 03:27:12 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 36 | # Execute a function that blocks, and in a separate thread, a function that |
| 37 | # triggers the release. Returns the result of the blocking function. Caution: |
| 38 | # block_func must guarantee to block until trigger_func is called, and |
| 39 | # trigger_func must guarantee to change queue state so that block_func can make |
| 40 | # enough progress to return. In particular, a block_func that just raises an |
| 41 | # exception regardless of whether trigger_func is called will lead to |
| 42 | # timing-dependent sporadic failures, and one of those went rarely seen but |
| 43 | # undiagnosed for years. Now block_func must be unexceptional. If block_func |
| 44 | # is supposed to raise an exception, call do_exceptional_blocking_test() |
| 45 | # instead. |
| 46 | |
| 47 | class BlockingTestMixin: |
| 48 | |
Ezio Melotti | 656c808 | 2013-03-23 23:35:06 +0200 | [diff] [blame] | 49 | def tearDown(self): |
| 50 | self.t = None |
| 51 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 52 | def do_blocking_test(self, block_func, block_args, trigger_func, trigger_args): |
| 53 | self.t = _TriggerThread(trigger_func, trigger_args) |
| 54 | self.t.start() |
| 55 | self.result = block_func(*block_args) |
| 56 | # If block_func returned before our thread made the call, we failed! |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 57 | if not self.t.startedEvent.is_set(): |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 58 | self.fail("blocking function '%r' appeared not to block" % |
| 59 | block_func) |
| 60 | self.t.join(10) # make sure the thread terminates |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 61 | if self.t.is_alive(): |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 62 | self.fail("trigger function '%r' appeared to not return" % |
| 63 | trigger_func) |
| 64 | return self.result |
| 65 | |
| 66 | # Call this instead if block_func is supposed to raise an exception. |
| 67 | def do_exceptional_blocking_test(self,block_func, block_args, trigger_func, |
| 68 | trigger_args, expected_exception_class): |
| 69 | self.t = _TriggerThread(trigger_func, trigger_args) |
| 70 | self.t.start() |
Tim Peters | 8d7626c | 2004-08-20 03:27:12 +0000 | [diff] [blame] | 71 | try: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 72 | try: |
| 73 | block_func(*block_args) |
| 74 | except expected_exception_class: |
| 75 | raise |
| 76 | else: |
| 77 | self.fail("expected exception of kind %r" % |
| 78 | expected_exception_class) |
| 79 | finally: |
| 80 | self.t.join(10) # make sure the thread terminates |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 81 | if self.t.is_alive(): |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 82 | self.fail("trigger function '%r' appeared to not return" % |
| 83 | trigger_func) |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 84 | if not self.t.startedEvent.is_set(): |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 85 | self.fail("trigger thread ended but event never set") |
| 86 | |
| 87 | |
R David Murray | c6bfce9 | 2012-03-17 16:38:39 -0400 | [diff] [blame] | 88 | class BaseQueueTestMixin(BlockingTestMixin): |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 89 | def setUp(self): |
| 90 | self.cum = 0 |
| 91 | self.cumlock = threading.Lock() |
| 92 | |
| 93 | def simple_queue_test(self, q): |
| 94 | if q.qsize(): |
| 95 | raise RuntimeError("Call this function with an empty queue") |
Brett Cannon | 671153d | 2010-07-23 16:56:21 +0000 | [diff] [blame] | 96 | self.assertTrue(q.empty()) |
| 97 | self.assertFalse(q.full()) |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 98 | # I guess we better check things actually queue correctly a little :) |
| 99 | q.put(111) |
| 100 | q.put(333) |
| 101 | q.put(222) |
| 102 | target_order = dict(Queue = [111, 333, 222], |
| 103 | LifoQueue = [222, 333, 111], |
| 104 | PriorityQueue = [111, 222, 333]) |
| 105 | actual_order = [q.get(), q.get(), q.get()] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 106 | self.assertEqual(actual_order, target_order[q.__class__.__name__], |
| 107 | "Didn't seem to queue the correct data!") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 108 | for i in range(QUEUE_SIZE-1): |
| 109 | q.put(i) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertTrue(q.qsize(), "Queue should not be empty") |
| 111 | self.assertTrue(not qfull(q), "Queue should not be full") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 112 | last = 2 * QUEUE_SIZE |
| 113 | full = 3 * 2 * QUEUE_SIZE |
| 114 | q.put(last) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 115 | self.assertTrue(qfull(q), "Queue should be full") |
Brett Cannon | 671153d | 2010-07-23 16:56:21 +0000 | [diff] [blame] | 116 | self.assertFalse(q.empty()) |
| 117 | self.assertTrue(q.full()) |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 118 | try: |
| 119 | q.put(full, block=0) |
| 120 | self.fail("Didn't appear to block with a full queue") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 121 | except queue.Full: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 122 | pass |
| 123 | try: |
| 124 | q.put(full, timeout=0.01) |
| 125 | self.fail("Didn't appear to time-out with a full queue") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 126 | except queue.Full: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 127 | pass |
| 128 | # Test a blocking put |
| 129 | self.do_blocking_test(q.put, (full,), q.get, ()) |
| 130 | self.do_blocking_test(q.put, (full, True, 10), q.get, ()) |
| 131 | # Empty it |
| 132 | for i in range(QUEUE_SIZE): |
| 133 | q.get() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 134 | self.assertTrue(not q.qsize(), "Queue should be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 135 | try: |
| 136 | q.get(block=0) |
| 137 | self.fail("Didn't appear to block with an empty queue") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 138 | except queue.Empty: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 139 | pass |
| 140 | try: |
| 141 | q.get(timeout=0.01) |
| 142 | self.fail("Didn't appear to time-out with an empty queue") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 143 | except queue.Empty: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 144 | pass |
| 145 | # Test a blocking get |
| 146 | self.do_blocking_test(q.get, (), q.put, ('empty',)) |
| 147 | self.do_blocking_test(q.get, (True, 10), q.put, ('empty',)) |
| 148 | |
| 149 | |
| 150 | def worker(self, q): |
| 151 | while True: |
| 152 | x = q.get() |
Amaury Forgeot d'Arc | b4febc7 | 2008-04-01 21:23:34 +0000 | [diff] [blame] | 153 | if x < 0: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 154 | q.task_done() |
| 155 | return |
| 156 | with self.cumlock: |
| 157 | self.cum += x |
| 158 | q.task_done() |
| 159 | |
| 160 | def queue_join_test(self, q): |
| 161 | self.cum = 0 |
| 162 | for i in (0,1): |
| 163 | threading.Thread(target=self.worker, args=(q,)).start() |
| 164 | for i in range(100): |
| 165 | q.put(i) |
| 166 | q.join() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 167 | self.assertEqual(self.cum, sum(range(100)), |
| 168 | "q.join() did not block until all tasks were done") |
Amaury Forgeot d'Arc | b4febc7 | 2008-04-01 21:23:34 +0000 | [diff] [blame] | 169 | for i in (0,1): |
| 170 | q.put(-1) # instruct the threads to close |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 171 | q.join() # verify that you can join twice |
| 172 | |
| 173 | def test_queue_task_done(self): |
| 174 | # Test to make sure a queue task completed successfully. |
| 175 | q = self.type2test() |
| 176 | try: |
| 177 | q.task_done() |
| 178 | except ValueError: |
| 179 | pass |
Tim Peters | 8d7626c | 2004-08-20 03:27:12 +0000 | [diff] [blame] | 180 | else: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 181 | self.fail("Did not detect task count going negative") |
| 182 | |
| 183 | def test_queue_join(self): |
| 184 | # Test that a queue join()s successfully, and before anything else |
| 185 | # (done twice for insurance). |
| 186 | q = self.type2test() |
| 187 | self.queue_join_test(q) |
| 188 | self.queue_join_test(q) |
| 189 | try: |
| 190 | q.task_done() |
| 191 | except ValueError: |
| 192 | pass |
| 193 | else: |
| 194 | self.fail("Did not detect task count going negative") |
| 195 | |
| 196 | def test_simple_queue(self): |
| 197 | # Do it a couple of times on the same queue. |
| 198 | # Done twice to make sure works with same instance reused. |
| 199 | q = self.type2test(QUEUE_SIZE) |
| 200 | self.simple_queue_test(q) |
| 201 | self.simple_queue_test(q) |
| 202 | |
Brett Cannon | 671153d | 2010-07-23 16:56:21 +0000 | [diff] [blame] | 203 | def test_negative_timeout_raises_exception(self): |
| 204 | q = self.type2test(QUEUE_SIZE) |
| 205 | with self.assertRaises(ValueError): |
| 206 | q.put(1, timeout=-1) |
| 207 | with self.assertRaises(ValueError): |
| 208 | q.get(1, timeout=-1) |
| 209 | |
| 210 | def test_nowait(self): |
| 211 | q = self.type2test(QUEUE_SIZE) |
| 212 | for i in range(QUEUE_SIZE): |
| 213 | q.put_nowait(1) |
| 214 | with self.assertRaises(queue.Full): |
| 215 | q.put_nowait(1) |
| 216 | |
| 217 | for i in range(QUEUE_SIZE): |
| 218 | q.get_nowait() |
| 219 | with self.assertRaises(queue.Empty): |
| 220 | q.get_nowait() |
| 221 | |
Raymond Hettinger | 189316a | 2010-10-31 17:57:52 +0000 | [diff] [blame] | 222 | def test_shrinking_queue(self): |
| 223 | # issue 10110 |
| 224 | q = self.type2test(3) |
| 225 | q.put(1) |
| 226 | q.put(2) |
| 227 | q.put(3) |
| 228 | with self.assertRaises(queue.Full): |
| 229 | q.put_nowait(4) |
| 230 | self.assertEqual(q.qsize(), 3) |
| 231 | q.maxsize = 2 # shrink the queue |
| 232 | with self.assertRaises(queue.Full): |
| 233 | q.put_nowait(4) |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 234 | |
R David Murray | c6bfce9 | 2012-03-17 16:38:39 -0400 | [diff] [blame] | 235 | class QueueTest(BaseQueueTestMixin, unittest.TestCase): |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 236 | type2test = queue.Queue |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 237 | |
R David Murray | c6bfce9 | 2012-03-17 16:38:39 -0400 | [diff] [blame] | 238 | class LifoQueueTest(BaseQueueTestMixin, unittest.TestCase): |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 239 | type2test = queue.LifoQueue |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 240 | |
R David Murray | c6bfce9 | 2012-03-17 16:38:39 -0400 | [diff] [blame] | 241 | class PriorityQueueTest(BaseQueueTestMixin, unittest.TestCase): |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 242 | type2test = queue.PriorityQueue |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 243 | |
| 244 | |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 245 | |
| 246 | # A Queue subclass that can provoke failure at a moment's notice :) |
| 247 | class FailingQueueException(Exception): |
| 248 | pass |
| 249 | |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 250 | class FailingQueue(queue.Queue): |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 251 | def __init__(self, *args): |
| 252 | self.fail_next_put = False |
| 253 | self.fail_next_get = False |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 254 | queue.Queue.__init__(self, *args) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 255 | def _put(self, item): |
| 256 | if self.fail_next_put: |
| 257 | self.fail_next_put = False |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 258 | raise FailingQueueException("You Lose") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 259 | return queue.Queue._put(self, item) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 260 | def _get(self): |
| 261 | if self.fail_next_get: |
| 262 | self.fail_next_get = False |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 263 | raise FailingQueueException("You Lose") |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 264 | return queue.Queue._get(self) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 265 | |
Ezio Melotti | 656c808 | 2013-03-23 23:35:06 +0200 | [diff] [blame] | 266 | class FailingQueueTest(BlockingTestMixin, unittest.TestCase): |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 267 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 268 | def failing_queue_test(self, q): |
| 269 | if q.qsize(): |
| 270 | raise RuntimeError("Call this function with an empty queue") |
| 271 | for i in range(QUEUE_SIZE-1): |
| 272 | q.put(i) |
| 273 | # Test a failing non-blocking put. |
| 274 | q.fail_next_put = True |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 275 | try: |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 276 | q.put("oops", block=0) |
| 277 | self.fail("The queue didn't fail when it should have") |
| 278 | except FailingQueueException: |
| 279 | pass |
| 280 | q.fail_next_put = True |
| 281 | try: |
| 282 | q.put("oops", timeout=0.1) |
| 283 | self.fail("The queue didn't fail when it should have") |
| 284 | except FailingQueueException: |
| 285 | pass |
| 286 | q.put("last") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 287 | self.assertTrue(qfull(q), "Queue should be full") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 288 | # Test a failing blocking put |
| 289 | q.fail_next_put = True |
| 290 | try: |
| 291 | self.do_blocking_test(q.put, ("full",), q.get, ()) |
| 292 | self.fail("The queue didn't fail when it should have") |
| 293 | except FailingQueueException: |
| 294 | pass |
| 295 | # Check the Queue isn't damaged. |
| 296 | # put failed, but get succeeded - re-add |
| 297 | q.put("last") |
| 298 | # Test a failing timeout put |
| 299 | q.fail_next_put = True |
| 300 | try: |
| 301 | self.do_exceptional_blocking_test(q.put, ("full", True, 10), q.get, (), |
| 302 | FailingQueueException) |
| 303 | self.fail("The queue didn't fail when it should have") |
| 304 | except FailingQueueException: |
| 305 | pass |
| 306 | # Check the Queue isn't damaged. |
| 307 | # put failed, but get succeeded - re-add |
| 308 | q.put("last") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 309 | self.assertTrue(qfull(q), "Queue should be full") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 310 | q.get() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 311 | self.assertTrue(not qfull(q), "Queue should not be full") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 312 | q.put("last") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 313 | self.assertTrue(qfull(q), "Queue should be full") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 314 | # Test a blocking put |
| 315 | self.do_blocking_test(q.put, ("full",), q.get, ()) |
| 316 | # Empty it |
| 317 | for i in range(QUEUE_SIZE): |
| 318 | q.get() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 319 | self.assertTrue(not q.qsize(), "Queue should be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 320 | q.put("first") |
| 321 | q.fail_next_get = True |
| 322 | try: |
| 323 | q.get() |
| 324 | self.fail("The queue didn't fail when it should have") |
| 325 | except FailingQueueException: |
| 326 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 327 | self.assertTrue(q.qsize(), "Queue should not be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 328 | q.fail_next_get = True |
| 329 | try: |
| 330 | q.get(timeout=0.1) |
| 331 | self.fail("The queue didn't fail when it should have") |
| 332 | except FailingQueueException: |
| 333 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 334 | self.assertTrue(q.qsize(), "Queue should not be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 335 | q.get() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 336 | self.assertTrue(not q.qsize(), "Queue should be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 337 | q.fail_next_get = True |
| 338 | try: |
| 339 | self.do_exceptional_blocking_test(q.get, (), q.put, ('empty',), |
| 340 | FailingQueueException) |
| 341 | self.fail("The queue didn't fail when it should have") |
| 342 | except FailingQueueException: |
| 343 | pass |
| 344 | # put succeeded, but get failed. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 345 | self.assertTrue(q.qsize(), "Queue should not be empty") |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 346 | q.get() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 347 | self.assertTrue(not q.qsize(), "Queue should be empty") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 348 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 349 | def test_failing_queue(self): |
| 350 | # Test to make sure a queue is functioning correctly. |
| 351 | # Done twice to the same instance. |
| 352 | q = FailingQueue(QUEUE_SIZE) |
| 353 | self.failing_queue_test(q) |
| 354 | self.failing_queue_test(q) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 355 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 356 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 357 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 358 | support.run_unittest(QueueTest, LifoQueueTest, PriorityQueueTest, |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 359 | FailingQueueTest) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 361 | |
Georg Brandl | 0e3b0ec | 2008-02-05 18:48:51 +0000 | [diff] [blame] | 362 | if __name__ == "__main__": |
| 363 | test_main() |