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 |
| 607 | |
| 608 | def setUp(self): |
| 609 | self.barrier = self.barriertype(self.N, timeout=0.1) |
| 610 | def tearDown(self): |
| 611 | self.barrier.abort() |
| 612 | |
| 613 | def run_threads(self, f): |
| 614 | b = Bunch(f, self.N-1) |
| 615 | f() |
| 616 | b.wait_for_finished() |
| 617 | |
| 618 | def multipass(self, results, n): |
| 619 | m = self.barrier.parties |
| 620 | self.assertEqual(m, self.N) |
| 621 | for i in range(n): |
| 622 | results[0].append(True) |
| 623 | self.assertEqual(len(results[1]), i * m) |
| 624 | self.barrier.wait() |
| 625 | results[1].append(True) |
| 626 | self.assertEqual(len(results[0]), (i + 1) * m) |
| 627 | self.barrier.wait() |
| 628 | self.assertEqual(self.barrier.n_waiting, 0) |
| 629 | self.assertFalse(self.barrier.broken) |
| 630 | |
| 631 | def test_barrier(self, passes=1): |
| 632 | """ |
| 633 | Test that a barrier is passed in lockstep |
| 634 | """ |
| 635 | results = [[],[]] |
| 636 | def f(): |
| 637 | self.multipass(results, passes) |
| 638 | self.run_threads(f) |
| 639 | |
| 640 | def test_barrier_10(self): |
| 641 | """ |
| 642 | Test that a barrier works for 10 consecutive runs |
| 643 | """ |
| 644 | return self.test_barrier(10) |
| 645 | |
| 646 | def test_wait_return(self): |
| 647 | """ |
| 648 | test the return value from barrier.wait |
| 649 | """ |
| 650 | results = [] |
| 651 | def f(): |
| 652 | r = self.barrier.wait() |
| 653 | results.append(r) |
| 654 | |
| 655 | self.run_threads(f) |
| 656 | self.assertEqual(sum(results), sum(range(self.N))) |
| 657 | |
| 658 | def test_action(self): |
| 659 | """ |
| 660 | Test the 'action' callback |
| 661 | """ |
| 662 | results = [] |
| 663 | def action(): |
| 664 | results.append(True) |
| 665 | barrier = self.barriertype(self.N, action) |
| 666 | def f(): |
| 667 | barrier.wait() |
| 668 | self.assertEqual(len(results), 1) |
| 669 | |
| 670 | self.run_threads(f) |
| 671 | |
| 672 | def test_abort(self): |
| 673 | """ |
| 674 | Test that an abort will put the barrier in a broken state |
| 675 | """ |
| 676 | results1 = [] |
| 677 | results2 = [] |
| 678 | def f(): |
| 679 | try: |
| 680 | i = self.barrier.wait() |
| 681 | if i == self.N//2: |
| 682 | raise RuntimeError |
| 683 | self.barrier.wait() |
| 684 | results1.append(True) |
| 685 | except threading.BrokenBarrierError: |
| 686 | results2.append(True) |
| 687 | except RuntimeError: |
| 688 | self.barrier.abort() |
| 689 | pass |
| 690 | |
| 691 | self.run_threads(f) |
| 692 | self.assertEqual(len(results1), 0) |
| 693 | self.assertEqual(len(results2), self.N-1) |
| 694 | self.assertTrue(self.barrier.broken) |
| 695 | |
| 696 | def test_reset(self): |
| 697 | """ |
| 698 | Test that a 'reset' on a barrier frees the waiting threads |
| 699 | """ |
| 700 | results1 = [] |
| 701 | results2 = [] |
| 702 | results3 = [] |
| 703 | def f(): |
| 704 | i = self.barrier.wait() |
| 705 | if i == self.N//2: |
| 706 | # Wait until the other threads are all in the barrier. |
| 707 | while self.barrier.n_waiting < self.N-1: |
| 708 | time.sleep(0.001) |
| 709 | self.barrier.reset() |
| 710 | else: |
| 711 | try: |
| 712 | self.barrier.wait() |
| 713 | results1.append(True) |
| 714 | except threading.BrokenBarrierError: |
| 715 | results2.append(True) |
| 716 | # Now, pass the barrier again |
| 717 | self.barrier.wait() |
| 718 | results3.append(True) |
| 719 | |
| 720 | self.run_threads(f) |
| 721 | self.assertEqual(len(results1), 0) |
| 722 | self.assertEqual(len(results2), self.N-1) |
| 723 | self.assertEqual(len(results3), self.N) |
| 724 | |
| 725 | |
| 726 | def test_abort_and_reset(self): |
| 727 | """ |
| 728 | Test that a barrier can be reset after being broken. |
| 729 | """ |
| 730 | results1 = [] |
| 731 | results2 = [] |
| 732 | results3 = [] |
| 733 | barrier2 = self.barriertype(self.N) |
| 734 | def f(): |
| 735 | try: |
| 736 | i = self.barrier.wait() |
| 737 | if i == self.N//2: |
| 738 | raise RuntimeError |
| 739 | self.barrier.wait() |
| 740 | results1.append(True) |
| 741 | except threading.BrokenBarrierError: |
| 742 | results2.append(True) |
| 743 | except RuntimeError: |
| 744 | self.barrier.abort() |
| 745 | pass |
| 746 | # Synchronize and reset the barrier. Must synchronize first so |
| 747 | # that everyone has left it when we reset, and after so that no |
| 748 | # one enters it before the reset. |
| 749 | if barrier2.wait() == self.N//2: |
| 750 | self.barrier.reset() |
| 751 | barrier2.wait() |
| 752 | self.barrier.wait() |
| 753 | results3.append(True) |
| 754 | |
| 755 | self.run_threads(f) |
| 756 | self.assertEqual(len(results1), 0) |
| 757 | self.assertEqual(len(results2), self.N-1) |
| 758 | self.assertEqual(len(results3), self.N) |
| 759 | |
| 760 | def test_timeout(self): |
| 761 | """ |
| 762 | Test wait(timeout) |
| 763 | """ |
| 764 | def f(): |
| 765 | i = self.barrier.wait() |
| 766 | if i == self.N // 2: |
| 767 | # One thread is late! |
| 768 | time.sleep(0.1) |
| 769 | # Default timeout is 0.1, so this is shorter. |
| 770 | self.assertRaises(threading.BrokenBarrierError, |
| 771 | self.barrier.wait, 0.05) |
| 772 | self.run_threads(f) |
| 773 | |
| 774 | def test_default_timeout(self): |
| 775 | """ |
| 776 | Test the barrier's default timeout |
| 777 | """ |
| 778 | def f(): |
| 779 | i = self.barrier.wait() |
| 780 | if i == self.N // 2: |
| 781 | # One thread is later than the default timeout of 0.1s. |
| 782 | time.sleep(0.15) |
| 783 | self.assertRaises(threading.BrokenBarrierError, self.barrier.wait) |
| 784 | self.run_threads(f) |
| 785 | |
| 786 | def test_single_thread(self): |
| 787 | b = self.barriertype(1) |
| 788 | b.wait() |
| 789 | b.wait() |