Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 1 | """ |
| 2 | Various tests for synchronization primitives. |
| 3 | """ |
| 4 | |
| 5 | import sys |
| 6 | import time |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 7 | from _thread import start_new_thread, get_ident, TIMEOUT_MAX |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 8 | import threading |
| 9 | import unittest |
| 10 | |
| 11 | from test import support |
| 12 | |
| 13 | |
| 14 | def _wait(): |
| 15 | # A crude wait/yield function not relying on synchronization primitives. |
| 16 | time.sleep(0.01) |
| 17 | |
| 18 | class Bunch(object): |
| 19 | """ |
| 20 | A bunch of threads. |
| 21 | """ |
| 22 | def __init__(self, f, n, wait_before_exit=False): |
| 23 | """ |
| 24 | Construct a bunch of `n` threads running the same function `f`. |
| 25 | If `wait_before_exit` is True, the threads won't terminate until |
| 26 | do_finish() is called. |
| 27 | """ |
| 28 | self.f = f |
| 29 | self.n = n |
| 30 | self.started = [] |
| 31 | self.finished = [] |
| 32 | self._can_exit = not wait_before_exit |
| 33 | def task(): |
| 34 | tid = get_ident() |
| 35 | self.started.append(tid) |
| 36 | try: |
| 37 | f() |
| 38 | finally: |
| 39 | self.finished.append(tid) |
| 40 | while not self._can_exit: |
| 41 | _wait() |
| 42 | for i in range(n): |
| 43 | start_new_thread(task, ()) |
| 44 | |
| 45 | def wait_for_started(self): |
| 46 | while len(self.started) < self.n: |
| 47 | _wait() |
| 48 | |
| 49 | def wait_for_finished(self): |
| 50 | while len(self.finished) < self.n: |
| 51 | _wait() |
| 52 | |
| 53 | def do_finish(self): |
| 54 | self._can_exit = True |
| 55 | |
| 56 | |
| 57 | class BaseTestCase(unittest.TestCase): |
| 58 | def setUp(self): |
| 59 | self._threads = support.threading_setup() |
| 60 | |
| 61 | def tearDown(self): |
| 62 | support.threading_cleanup(*self._threads) |
| 63 | support.reap_children() |
| 64 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 65 | def assertTimeout(self, actual, expected): |
| 66 | # The waiting and/or time.time() can be imprecise, which |
| 67 | # is why comparing to the expected value would sometimes fail |
| 68 | # (especially under Windows). |
| 69 | self.assertGreaterEqual(actual, expected * 0.6) |
| 70 | # Test nothing insane happened |
| 71 | self.assertLess(actual, expected * 10.0) |
| 72 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 73 | |
| 74 | class BaseLockTests(BaseTestCase): |
| 75 | """ |
| 76 | Tests for both recursive and non-recursive locks. |
| 77 | """ |
| 78 | |
| 79 | def test_constructor(self): |
| 80 | lock = self.locktype() |
| 81 | del lock |
| 82 | |
| 83 | def test_acquire_destroy(self): |
| 84 | lock = self.locktype() |
| 85 | lock.acquire() |
| 86 | del lock |
| 87 | |
| 88 | def test_acquire_release(self): |
| 89 | lock = self.locktype() |
| 90 | lock.acquire() |
| 91 | lock.release() |
| 92 | del lock |
| 93 | |
| 94 | def test_try_acquire(self): |
| 95 | lock = self.locktype() |
| 96 | self.assertTrue(lock.acquire(False)) |
| 97 | lock.release() |
| 98 | |
| 99 | def test_try_acquire_contended(self): |
| 100 | lock = self.locktype() |
| 101 | lock.acquire() |
| 102 | result = [] |
| 103 | def f(): |
| 104 | result.append(lock.acquire(False)) |
| 105 | Bunch(f, 1).wait_for_finished() |
| 106 | self.assertFalse(result[0]) |
| 107 | lock.release() |
| 108 | |
| 109 | def test_acquire_contended(self): |
| 110 | lock = self.locktype() |
| 111 | lock.acquire() |
| 112 | N = 5 |
| 113 | def f(): |
| 114 | lock.acquire() |
| 115 | lock.release() |
| 116 | |
| 117 | b = Bunch(f, N) |
| 118 | b.wait_for_started() |
| 119 | _wait() |
| 120 | self.assertEqual(len(b.finished), 0) |
| 121 | lock.release() |
| 122 | b.wait_for_finished() |
| 123 | self.assertEqual(len(b.finished), N) |
| 124 | |
| 125 | def test_with(self): |
| 126 | lock = self.locktype() |
| 127 | def f(): |
| 128 | lock.acquire() |
| 129 | lock.release() |
| 130 | def _with(err=None): |
| 131 | with lock: |
| 132 | if err is not None: |
| 133 | raise err |
| 134 | _with() |
| 135 | # Check the lock is unacquired |
| 136 | Bunch(f, 1).wait_for_finished() |
| 137 | self.assertRaises(TypeError, _with, TypeError) |
| 138 | # Check the lock is unacquired |
| 139 | Bunch(f, 1).wait_for_finished() |
| 140 | |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 141 | def test_thread_leak(self): |
| 142 | # The lock shouldn't leak a Thread instance when used from a foreign |
| 143 | # (non-threading) thread. |
| 144 | lock = self.locktype() |
| 145 | def f(): |
| 146 | lock.acquire() |
| 147 | lock.release() |
| 148 | n = len(threading.enumerate()) |
| 149 | # We run many threads in the hope that existing threads ids won't |
| 150 | # be recycled. |
| 151 | Bunch(f, 15).wait_for_finished() |
| 152 | self.assertEqual(n, len(threading.enumerate())) |
| 153 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 154 | def test_timeout(self): |
| 155 | lock = self.locktype() |
| 156 | # Can't set timeout if not blocking |
| 157 | self.assertRaises(ValueError, lock.acquire, 0, 1) |
| 158 | # Invalid timeout values |
| 159 | self.assertRaises(ValueError, lock.acquire, timeout=-100) |
| 160 | self.assertRaises(OverflowError, lock.acquire, timeout=1e100) |
| 161 | self.assertRaises(OverflowError, lock.acquire, timeout=TIMEOUT_MAX + 1) |
| 162 | # TIMEOUT_MAX is ok |
| 163 | lock.acquire(timeout=TIMEOUT_MAX) |
| 164 | lock.release() |
| 165 | t1 = time.time() |
| 166 | self.assertTrue(lock.acquire(timeout=5)) |
| 167 | t2 = time.time() |
| 168 | # Just a sanity test that it didn't actually wait for the timeout. |
| 169 | self.assertLess(t2 - t1, 5) |
| 170 | results = [] |
| 171 | def f(): |
| 172 | t1 = time.time() |
| 173 | results.append(lock.acquire(timeout=0.5)) |
| 174 | t2 = time.time() |
| 175 | results.append(t2 - t1) |
| 176 | Bunch(f, 1).wait_for_finished() |
| 177 | self.assertFalse(results[0]) |
| 178 | self.assertTimeout(results[1], 0.5) |
| 179 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 180 | |
| 181 | class LockTests(BaseLockTests): |
| 182 | """ |
| 183 | Tests for non-recursive, weak locks |
| 184 | (which can be acquired and released from different threads). |
| 185 | """ |
| 186 | def test_reacquire(self): |
| 187 | # Lock needs to be released before re-acquiring. |
| 188 | lock = self.locktype() |
| 189 | phase = [] |
| 190 | def f(): |
| 191 | lock.acquire() |
| 192 | phase.append(None) |
| 193 | lock.acquire() |
| 194 | phase.append(None) |
| 195 | start_new_thread(f, ()) |
| 196 | while len(phase) == 0: |
| 197 | _wait() |
| 198 | _wait() |
| 199 | self.assertEqual(len(phase), 1) |
| 200 | lock.release() |
| 201 | while len(phase) == 1: |
| 202 | _wait() |
| 203 | self.assertEqual(len(phase), 2) |
| 204 | |
| 205 | def test_different_thread(self): |
| 206 | # Lock can be released from a different thread. |
| 207 | lock = self.locktype() |
| 208 | lock.acquire() |
| 209 | def f(): |
| 210 | lock.release() |
| 211 | b = Bunch(f, 1) |
| 212 | b.wait_for_finished() |
| 213 | lock.acquire() |
| 214 | lock.release() |
| 215 | |
| 216 | |
| 217 | class RLockTests(BaseLockTests): |
| 218 | """ |
| 219 | Tests for recursive locks. |
| 220 | """ |
| 221 | def test_reacquire(self): |
| 222 | lock = self.locktype() |
| 223 | lock.acquire() |
| 224 | lock.acquire() |
| 225 | lock.release() |
| 226 | lock.acquire() |
| 227 | lock.release() |
| 228 | lock.release() |
| 229 | |
| 230 | def test_release_unacquired(self): |
| 231 | # Cannot release an unacquired lock |
| 232 | lock = self.locktype() |
| 233 | self.assertRaises(RuntimeError, lock.release) |
| 234 | lock.acquire() |
| 235 | lock.acquire() |
| 236 | lock.release() |
| 237 | lock.acquire() |
| 238 | lock.release() |
| 239 | lock.release() |
| 240 | self.assertRaises(RuntimeError, lock.release) |
| 241 | |
| 242 | def test_different_thread(self): |
| 243 | # Cannot release from a different thread |
| 244 | lock = self.locktype() |
| 245 | def f(): |
| 246 | lock.acquire() |
| 247 | b = Bunch(f, 1, True) |
| 248 | try: |
| 249 | self.assertRaises(RuntimeError, lock.release) |
| 250 | finally: |
| 251 | b.do_finish() |
| 252 | |
| 253 | def test__is_owned(self): |
| 254 | lock = self.locktype() |
| 255 | self.assertFalse(lock._is_owned()) |
| 256 | lock.acquire() |
| 257 | self.assertTrue(lock._is_owned()) |
| 258 | lock.acquire() |
| 259 | self.assertTrue(lock._is_owned()) |
| 260 | result = [] |
| 261 | def f(): |
| 262 | result.append(lock._is_owned()) |
| 263 | Bunch(f, 1).wait_for_finished() |
| 264 | self.assertFalse(result[0]) |
| 265 | lock.release() |
| 266 | self.assertTrue(lock._is_owned()) |
| 267 | lock.release() |
| 268 | self.assertFalse(lock._is_owned()) |
| 269 | |
| 270 | |
| 271 | class EventTests(BaseTestCase): |
| 272 | """ |
| 273 | Tests for Event objects. |
| 274 | """ |
| 275 | |
| 276 | def test_is_set(self): |
| 277 | evt = self.eventtype() |
| 278 | self.assertFalse(evt.is_set()) |
| 279 | evt.set() |
| 280 | self.assertTrue(evt.is_set()) |
| 281 | evt.set() |
| 282 | self.assertTrue(evt.is_set()) |
| 283 | evt.clear() |
| 284 | self.assertFalse(evt.is_set()) |
| 285 | evt.clear() |
| 286 | self.assertFalse(evt.is_set()) |
| 287 | |
| 288 | def _check_notify(self, evt): |
| 289 | # All threads get notified |
| 290 | N = 5 |
| 291 | results1 = [] |
| 292 | results2 = [] |
| 293 | def f(): |
| 294 | results1.append(evt.wait()) |
| 295 | results2.append(evt.wait()) |
| 296 | b = Bunch(f, N) |
| 297 | b.wait_for_started() |
| 298 | _wait() |
| 299 | self.assertEqual(len(results1), 0) |
| 300 | evt.set() |
| 301 | b.wait_for_finished() |
| 302 | self.assertEqual(results1, [True] * N) |
| 303 | self.assertEqual(results2, [True] * N) |
| 304 | |
| 305 | def test_notify(self): |
| 306 | evt = self.eventtype() |
| 307 | self._check_notify(evt) |
| 308 | # Another time, after an explicit clear() |
| 309 | evt.set() |
| 310 | evt.clear() |
| 311 | self._check_notify(evt) |
| 312 | |
| 313 | def test_timeout(self): |
| 314 | evt = self.eventtype() |
| 315 | results1 = [] |
| 316 | results2 = [] |
| 317 | N = 5 |
| 318 | def f(): |
| 319 | results1.append(evt.wait(0.0)) |
| 320 | t1 = time.time() |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 321 | r = evt.wait(0.5) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 322 | t2 = time.time() |
| 323 | results2.append((r, t2 - t1)) |
| 324 | Bunch(f, N).wait_for_finished() |
| 325 | self.assertEqual(results1, [False] * N) |
| 326 | for r, dt in results2: |
| 327 | self.assertFalse(r) |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 328 | self.assertTimeout(dt, 0.5) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 329 | # The event is set |
| 330 | results1 = [] |
| 331 | results2 = [] |
| 332 | evt.set() |
| 333 | Bunch(f, N).wait_for_finished() |
| 334 | self.assertEqual(results1, [True] * N) |
| 335 | for r, dt in results2: |
| 336 | self.assertTrue(r) |
| 337 | |
| 338 | |
| 339 | class ConditionTests(BaseTestCase): |
| 340 | """ |
| 341 | Tests for condition variables. |
| 342 | """ |
| 343 | |
| 344 | def test_acquire(self): |
| 345 | cond = self.condtype() |
| 346 | # Be default we have an RLock: the condition can be acquired multiple |
| 347 | # times. |
| 348 | cond.acquire() |
| 349 | cond.acquire() |
| 350 | cond.release() |
| 351 | cond.release() |
| 352 | lock = threading.Lock() |
| 353 | cond = self.condtype(lock) |
| 354 | cond.acquire() |
| 355 | self.assertFalse(lock.acquire(False)) |
| 356 | cond.release() |
| 357 | self.assertTrue(lock.acquire(False)) |
| 358 | self.assertFalse(cond.acquire(False)) |
| 359 | lock.release() |
| 360 | with cond: |
| 361 | self.assertFalse(lock.acquire(False)) |
| 362 | |
| 363 | def test_unacquired_wait(self): |
| 364 | cond = self.condtype() |
| 365 | self.assertRaises(RuntimeError, cond.wait) |
| 366 | |
| 367 | def test_unacquired_notify(self): |
| 368 | cond = self.condtype() |
| 369 | self.assertRaises(RuntimeError, cond.notify) |
| 370 | |
| 371 | def _check_notify(self, cond): |
| 372 | N = 5 |
| 373 | results1 = [] |
| 374 | results2 = [] |
| 375 | phase_num = 0 |
| 376 | def f(): |
| 377 | cond.acquire() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 378 | result = cond.wait() |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 379 | cond.release() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 380 | results1.append((result, phase_num)) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 381 | cond.acquire() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 382 | result = cond.wait() |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 383 | cond.release() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 384 | results2.append((result, phase_num)) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 385 | b = Bunch(f, N) |
| 386 | b.wait_for_started() |
| 387 | _wait() |
| 388 | self.assertEqual(results1, []) |
| 389 | # Notify 3 threads at first |
| 390 | cond.acquire() |
| 391 | cond.notify(3) |
| 392 | _wait() |
| 393 | phase_num = 1 |
| 394 | cond.release() |
| 395 | while len(results1) < 3: |
| 396 | _wait() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 397 | self.assertEqual(results1, [(True, 1)] * 3) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 398 | self.assertEqual(results2, []) |
| 399 | # Notify 5 threads: they might be in their first or second wait |
| 400 | cond.acquire() |
| 401 | cond.notify(5) |
| 402 | _wait() |
| 403 | phase_num = 2 |
| 404 | cond.release() |
| 405 | while len(results1) + len(results2) < 8: |
| 406 | _wait() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 407 | self.assertEqual(results1, [(True, 1)] * 3 + [(True, 2)] * 2) |
| 408 | self.assertEqual(results2, [(True, 2)] * 3) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 409 | # Notify all threads: they are all in their second wait |
| 410 | cond.acquire() |
| 411 | cond.notify_all() |
| 412 | _wait() |
| 413 | phase_num = 3 |
| 414 | cond.release() |
| 415 | while len(results2) < 5: |
| 416 | _wait() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 417 | self.assertEqual(results1, [(True, 1)] * 3 + [(True,2)] * 2) |
| 418 | self.assertEqual(results2, [(True, 2)] * 3 + [(True, 3)] * 2) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 419 | b.wait_for_finished() |
| 420 | |
| 421 | def test_notify(self): |
| 422 | cond = self.condtype() |
| 423 | self._check_notify(cond) |
| 424 | # A second time, to check internal state is still ok. |
| 425 | self._check_notify(cond) |
| 426 | |
| 427 | def test_timeout(self): |
| 428 | cond = self.condtype() |
| 429 | results = [] |
| 430 | N = 5 |
| 431 | def f(): |
| 432 | cond.acquire() |
| 433 | t1 = time.time() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 434 | result = cond.wait(0.5) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 435 | t2 = time.time() |
| 436 | cond.release() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 437 | results.append((t2 - t1, result)) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 438 | Bunch(f, N).wait_for_finished() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 439 | self.assertEqual(len(results), N) |
| 440 | for dt, result in results: |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 441 | self.assertTimeout(dt, 0.5) |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 442 | # Note that conceptually (that"s the condition variable protocol) |
| 443 | # a wait() may succeed even if no one notifies us and before any |
| 444 | # timeout occurs. Spurious wakeups can occur. |
| 445 | # This makes it hard to verify the result value. |
| 446 | # In practice, this implementation has no spurious wakeups. |
| 447 | self.assertFalse(result) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 448 | |
| 449 | |
| 450 | class BaseSemaphoreTests(BaseTestCase): |
| 451 | """ |
| 452 | Common tests for {bounded, unbounded} semaphore objects. |
| 453 | """ |
| 454 | |
| 455 | def test_constructor(self): |
| 456 | self.assertRaises(ValueError, self.semtype, value = -1) |
| 457 | self.assertRaises(ValueError, self.semtype, value = -sys.maxsize) |
| 458 | |
| 459 | def test_acquire(self): |
| 460 | sem = self.semtype(1) |
| 461 | sem.acquire() |
| 462 | sem.release() |
| 463 | sem = self.semtype(2) |
| 464 | sem.acquire() |
| 465 | sem.acquire() |
| 466 | sem.release() |
| 467 | sem.release() |
| 468 | |
| 469 | def test_acquire_destroy(self): |
| 470 | sem = self.semtype() |
| 471 | sem.acquire() |
| 472 | del sem |
| 473 | |
| 474 | def test_acquire_contended(self): |
| 475 | sem = self.semtype(7) |
| 476 | sem.acquire() |
| 477 | N = 10 |
| 478 | results1 = [] |
| 479 | results2 = [] |
| 480 | phase_num = 0 |
| 481 | def f(): |
| 482 | sem.acquire() |
| 483 | results1.append(phase_num) |
| 484 | sem.acquire() |
| 485 | results2.append(phase_num) |
| 486 | b = Bunch(f, 10) |
| 487 | b.wait_for_started() |
| 488 | while len(results1) + len(results2) < 6: |
| 489 | _wait() |
| 490 | self.assertEqual(results1 + results2, [0] * 6) |
| 491 | phase_num = 1 |
| 492 | for i in range(7): |
| 493 | sem.release() |
| 494 | while len(results1) + len(results2) < 13: |
| 495 | _wait() |
| 496 | self.assertEqual(sorted(results1 + results2), [0] * 6 + [1] * 7) |
| 497 | phase_num = 2 |
| 498 | for i in range(6): |
| 499 | sem.release() |
| 500 | while len(results1) + len(results2) < 19: |
| 501 | _wait() |
| 502 | self.assertEqual(sorted(results1 + results2), [0] * 6 + [1] * 7 + [2] * 6) |
| 503 | # The semaphore is still locked |
| 504 | self.assertFalse(sem.acquire(False)) |
| 505 | # Final release, to let the last thread finish |
| 506 | sem.release() |
| 507 | b.wait_for_finished() |
| 508 | |
| 509 | def test_try_acquire(self): |
| 510 | sem = self.semtype(2) |
| 511 | self.assertTrue(sem.acquire(False)) |
| 512 | self.assertTrue(sem.acquire(False)) |
| 513 | self.assertFalse(sem.acquire(False)) |
| 514 | sem.release() |
| 515 | self.assertTrue(sem.acquire(False)) |
| 516 | |
| 517 | def test_try_acquire_contended(self): |
| 518 | sem = self.semtype(4) |
| 519 | sem.acquire() |
| 520 | results = [] |
| 521 | def f(): |
| 522 | results.append(sem.acquire(False)) |
| 523 | results.append(sem.acquire(False)) |
| 524 | Bunch(f, 5).wait_for_finished() |
| 525 | # There can be a thread switch between acquiring the semaphore and |
| 526 | # appending the result, therefore results will not necessarily be |
| 527 | # ordered. |
| 528 | self.assertEqual(sorted(results), [False] * 7 + [True] * 3 ) |
| 529 | |
Antoine Pitrou | 0454af9 | 2010-04-17 23:51:58 +0000 | [diff] [blame] | 530 | def test_acquire_timeout(self): |
| 531 | sem = self.semtype(2) |
| 532 | self.assertRaises(ValueError, sem.acquire, False, timeout=1.0) |
| 533 | self.assertTrue(sem.acquire(timeout=0.005)) |
| 534 | self.assertTrue(sem.acquire(timeout=0.005)) |
| 535 | self.assertFalse(sem.acquire(timeout=0.005)) |
| 536 | sem.release() |
| 537 | self.assertTrue(sem.acquire(timeout=0.005)) |
| 538 | t = time.time() |
| 539 | self.assertFalse(sem.acquire(timeout=0.5)) |
| 540 | dt = time.time() - t |
| 541 | self.assertTimeout(dt, 0.5) |
| 542 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 543 | def test_default_value(self): |
| 544 | # The default initial value is 1. |
| 545 | sem = self.semtype() |
| 546 | sem.acquire() |
| 547 | def f(): |
| 548 | sem.acquire() |
| 549 | sem.release() |
| 550 | b = Bunch(f, 1) |
| 551 | b.wait_for_started() |
| 552 | _wait() |
| 553 | self.assertFalse(b.finished) |
| 554 | sem.release() |
| 555 | b.wait_for_finished() |
| 556 | |
| 557 | def test_with(self): |
| 558 | sem = self.semtype(2) |
| 559 | def _with(err=None): |
| 560 | with sem: |
| 561 | self.assertTrue(sem.acquire(False)) |
| 562 | sem.release() |
| 563 | with sem: |
| 564 | self.assertFalse(sem.acquire(False)) |
| 565 | if err: |
| 566 | raise err |
| 567 | _with() |
| 568 | self.assertTrue(sem.acquire(False)) |
| 569 | sem.release() |
| 570 | self.assertRaises(TypeError, _with, TypeError) |
| 571 | self.assertTrue(sem.acquire(False)) |
| 572 | sem.release() |
| 573 | |
| 574 | class SemaphoreTests(BaseSemaphoreTests): |
| 575 | """ |
| 576 | Tests for unbounded semaphores. |
| 577 | """ |
| 578 | |
| 579 | def test_release_unacquired(self): |
| 580 | # Unbounded releases are allowed and increment the semaphore's value |
| 581 | sem = self.semtype(1) |
| 582 | sem.release() |
| 583 | sem.acquire() |
| 584 | sem.acquire() |
| 585 | sem.release() |
| 586 | |
| 587 | |
| 588 | class BoundedSemaphoreTests(BaseSemaphoreTests): |
| 589 | """ |
| 590 | Tests for bounded semaphores. |
| 591 | """ |
| 592 | |
| 593 | def test_release_unacquired(self): |
| 594 | # Cannot go past the initial value |
| 595 | sem = self.semtype() |
| 596 | self.assertRaises(ValueError, sem.release) |
| 597 | sem.acquire() |
| 598 | sem.release() |
| 599 | self.assertRaises(ValueError, sem.release) |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 600 | |
| 601 | |
| 602 | class BarrierTests(BaseTestCase): |
| 603 | """ |
| 604 | Tests for Barrier objects. |
| 605 | """ |
| 606 | N = 5 |
Kristján Valur Jónsson | f53a626 | 2010-10-31 03:00:57 +0000 | [diff] [blame] | 607 | defaultTimeout = 0.5 |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 608 | |
| 609 | def setUp(self): |
Kristján Valur Jónsson | f53a626 | 2010-10-31 03:00:57 +0000 | [diff] [blame] | 610 | self.barrier = self.barriertype(self.N, timeout=self.defaultTimeout) |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 611 | def tearDown(self): |
| 612 | self.barrier.abort() |
| 613 | |
| 614 | def run_threads(self, f): |
| 615 | b = Bunch(f, self.N-1) |
| 616 | f() |
| 617 | b.wait_for_finished() |
| 618 | |
| 619 | def multipass(self, results, n): |
| 620 | m = self.barrier.parties |
| 621 | self.assertEqual(m, self.N) |
| 622 | for i in range(n): |
| 623 | results[0].append(True) |
| 624 | self.assertEqual(len(results[1]), i * m) |
| 625 | self.barrier.wait() |
| 626 | results[1].append(True) |
| 627 | self.assertEqual(len(results[0]), (i + 1) * m) |
| 628 | self.barrier.wait() |
| 629 | self.assertEqual(self.barrier.n_waiting, 0) |
| 630 | self.assertFalse(self.barrier.broken) |
| 631 | |
| 632 | def test_barrier(self, passes=1): |
| 633 | """ |
| 634 | Test that a barrier is passed in lockstep |
| 635 | """ |
| 636 | results = [[],[]] |
| 637 | def f(): |
| 638 | self.multipass(results, passes) |
| 639 | self.run_threads(f) |
| 640 | |
| 641 | def test_barrier_10(self): |
| 642 | """ |
| 643 | Test that a barrier works for 10 consecutive runs |
| 644 | """ |
| 645 | return self.test_barrier(10) |
| 646 | |
| 647 | def test_wait_return(self): |
| 648 | """ |
| 649 | test the return value from barrier.wait |
| 650 | """ |
| 651 | results = [] |
| 652 | def f(): |
| 653 | r = self.barrier.wait() |
| 654 | results.append(r) |
| 655 | |
| 656 | self.run_threads(f) |
| 657 | self.assertEqual(sum(results), sum(range(self.N))) |
| 658 | |
| 659 | def test_action(self): |
| 660 | """ |
| 661 | Test the 'action' callback |
| 662 | """ |
| 663 | results = [] |
| 664 | def action(): |
| 665 | results.append(True) |
| 666 | barrier = self.barriertype(self.N, action) |
| 667 | def f(): |
| 668 | barrier.wait() |
| 669 | self.assertEqual(len(results), 1) |
| 670 | |
| 671 | self.run_threads(f) |
| 672 | |
| 673 | def test_abort(self): |
| 674 | """ |
| 675 | Test that an abort will put the barrier in a broken state |
| 676 | """ |
| 677 | results1 = [] |
| 678 | results2 = [] |
| 679 | def f(): |
| 680 | try: |
| 681 | i = self.barrier.wait() |
| 682 | if i == self.N//2: |
| 683 | raise RuntimeError |
| 684 | self.barrier.wait() |
| 685 | results1.append(True) |
| 686 | except threading.BrokenBarrierError: |
| 687 | results2.append(True) |
| 688 | except RuntimeError: |
| 689 | self.barrier.abort() |
| 690 | pass |
| 691 | |
| 692 | self.run_threads(f) |
| 693 | self.assertEqual(len(results1), 0) |
| 694 | self.assertEqual(len(results2), self.N-1) |
| 695 | self.assertTrue(self.barrier.broken) |
| 696 | |
| 697 | def test_reset(self): |
| 698 | """ |
| 699 | Test that a 'reset' on a barrier frees the waiting threads |
| 700 | """ |
| 701 | results1 = [] |
| 702 | results2 = [] |
| 703 | results3 = [] |
| 704 | def f(): |
| 705 | i = self.barrier.wait() |
| 706 | if i == self.N//2: |
| 707 | # Wait until the other threads are all in the barrier. |
| 708 | while self.barrier.n_waiting < self.N-1: |
| 709 | time.sleep(0.001) |
| 710 | self.barrier.reset() |
| 711 | else: |
| 712 | try: |
| 713 | self.barrier.wait() |
| 714 | results1.append(True) |
| 715 | except threading.BrokenBarrierError: |
| 716 | results2.append(True) |
| 717 | # Now, pass the barrier again |
| 718 | self.barrier.wait() |
| 719 | results3.append(True) |
| 720 | |
| 721 | self.run_threads(f) |
| 722 | self.assertEqual(len(results1), 0) |
| 723 | self.assertEqual(len(results2), self.N-1) |
| 724 | self.assertEqual(len(results3), self.N) |
| 725 | |
| 726 | |
| 727 | def test_abort_and_reset(self): |
| 728 | """ |
| 729 | Test that a barrier can be reset after being broken. |
| 730 | """ |
| 731 | results1 = [] |
| 732 | results2 = [] |
| 733 | results3 = [] |
| 734 | barrier2 = self.barriertype(self.N) |
| 735 | def f(): |
| 736 | try: |
| 737 | i = self.barrier.wait() |
| 738 | if i == self.N//2: |
| 739 | raise RuntimeError |
| 740 | self.barrier.wait() |
| 741 | results1.append(True) |
| 742 | except threading.BrokenBarrierError: |
| 743 | results2.append(True) |
| 744 | except RuntimeError: |
| 745 | self.barrier.abort() |
| 746 | pass |
| 747 | # Synchronize and reset the barrier. Must synchronize first so |
| 748 | # that everyone has left it when we reset, and after so that no |
| 749 | # one enters it before the reset. |
| 750 | if barrier2.wait() == self.N//2: |
| 751 | self.barrier.reset() |
| 752 | barrier2.wait() |
| 753 | self.barrier.wait() |
| 754 | results3.append(True) |
| 755 | |
| 756 | self.run_threads(f) |
| 757 | self.assertEqual(len(results1), 0) |
| 758 | self.assertEqual(len(results2), self.N-1) |
| 759 | self.assertEqual(len(results3), self.N) |
| 760 | |
| 761 | def test_timeout(self): |
| 762 | """ |
| 763 | Test wait(timeout) |
| 764 | """ |
| 765 | def f(): |
| 766 | i = self.barrier.wait() |
| 767 | if i == self.N // 2: |
| 768 | # One thread is late! |
| 769 | time.sleep(0.1) |
| 770 | # Default timeout is 0.1, so this is shorter. |
| 771 | self.assertRaises(threading.BrokenBarrierError, |
| 772 | self.barrier.wait, 0.05) |
| 773 | self.run_threads(f) |
| 774 | |
| 775 | def test_default_timeout(self): |
| 776 | """ |
| 777 | Test the barrier's default timeout |
| 778 | """ |
Kristján Valur Jónsson | f53a626 | 2010-10-31 03:00:57 +0000 | [diff] [blame] | 779 | #create a barrier with a low default timeout |
| 780 | barrier = self.barriertype(self.N, timeout=0.1) |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 781 | def f(): |
Kristján Valur Jónsson | f53a626 | 2010-10-31 03:00:57 +0000 | [diff] [blame] | 782 | i = barrier.wait() |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 783 | if i == self.N // 2: |
| 784 | # One thread is later than the default timeout of 0.1s. |
Kristján Valur Jónsson | f53a626 | 2010-10-31 03:00:57 +0000 | [diff] [blame] | 785 | time.sleep(0.2) |
| 786 | self.assertRaises(threading.BrokenBarrierError, barrier.wait) |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 787 | self.run_threads(f) |
| 788 | |
| 789 | def test_single_thread(self): |
| 790 | b = self.barriertype(1) |
| 791 | b.wait() |
| 792 | b.wait() |