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