| 1 |
"""Tests for locks.py""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
from unittest import mock |
| 5 |
import re |
| 6 |
|
| 7 |
import asyncio |
| 8 |
import collections |
| 9 |
|
| 10 |
STR_RGX_REPR = ( |
| 11 |
r'^<(?P<class>.*?) object at (?P<address>.*?)' |
| 12 |
r'\[(?P<extras>' |
| 13 |
r'(set|unset|locked|unlocked|filling|draining|resetting|broken)' |
| 14 |
r'(, value:\d)?' |
| 15 |
r'(, waiters:\d+)?' |
| 16 |
r'(, waiters:\d+\/\d+)?' # barrier |
| 17 |
r')\]>\z' |
| 18 |
) |
| 19 |
RGX_REPR = re.compile(STR_RGX_REPR) |
| 20 |
|
| 21 |
|
| 22 |
def tearDownModule(): |
| 23 |
asyncio.events._set_event_loop_policy(None) |
| 24 |
|
| 25 |
|
| 26 |
class LockTests(unittest.IsolatedAsyncioTestCase): |
| 27 |
|
| 28 |
async def test_repr(self): |
| 29 |
lock = asyncio.Lock() |
| 30 |
self.assertEndsWith(repr(lock), '[unlocked]>') |
| 31 |
self.assertTrue(RGX_REPR.match(repr(lock))) |
| 32 |
|
| 33 |
await lock.acquire() |
| 34 |
self.assertEndsWith(repr(lock), '[locked]>') |
| 35 |
self.assertTrue(RGX_REPR.match(repr(lock))) |
| 36 |
|
| 37 |
async def test_lock(self): |
| 38 |
lock = asyncio.Lock() |
| 39 |
|
| 40 |
with self.assertRaisesRegex( |
| 41 |
TypeError, |
| 42 |
"'Lock' object can't be awaited" |
| 43 |
): |
| 44 |
await lock |
| 45 |
|
| 46 |
self.assertFalse(lock.locked()) |
| 47 |
|
| 48 |
async def test_lock_doesnt_accept_loop_parameter(self): |
| 49 |
primitives_cls = [ |
| 50 |
asyncio.Lock, |
| 51 |
asyncio.Condition, |
| 52 |
asyncio.Event, |
| 53 |
asyncio.Semaphore, |
| 54 |
asyncio.BoundedSemaphore, |
| 55 |
] |
| 56 |
|
| 57 |
loop = asyncio.get_running_loop() |
| 58 |
|
| 59 |
for cls in primitives_cls: |
| 60 |
with self.assertRaisesRegex( |
| 61 |
TypeError, |
| 62 |
rf"{cls.__name__}\.__init__\(\) got an unexpected " |
| 63 |
rf"keyword argument 'loop'" |
| 64 |
): |
| 65 |
cls(loop=loop) |
| 66 |
|
| 67 |
async def test_lock_by_with_statement(self): |
| 68 |
primitives = [ |
| 69 |
asyncio.Lock(), |
| 70 |
asyncio.Condition(), |
| 71 |
asyncio.Semaphore(), |
| 72 |
asyncio.BoundedSemaphore(), |
| 73 |
] |
| 74 |
|
| 75 |
for lock in primitives: |
| 76 |
await asyncio.sleep(0.01) |
| 77 |
self.assertFalse(lock.locked()) |
| 78 |
with self.assertRaisesRegex( |
| 79 |
TypeError, |
| 80 |
r"'\w+' object can't be awaited" |
| 81 |
): |
| 82 |
with await lock: |
| 83 |
pass |
| 84 |
self.assertFalse(lock.locked()) |
| 85 |
|
| 86 |
async def test_acquire(self): |
| 87 |
lock = asyncio.Lock() |
| 88 |
result = [] |
| 89 |
|
| 90 |
self.assertTrue(await lock.acquire()) |
| 91 |
|
| 92 |
async def c1(result): |
| 93 |
if await lock.acquire(): |
| 94 |
result.append(1) |
| 95 |
return True |
| 96 |
|
| 97 |
async def c2(result): |
| 98 |
if await lock.acquire(): |
| 99 |
result.append(2) |
| 100 |
return True |
| 101 |
|
| 102 |
async def c3(result): |
| 103 |
if await lock.acquire(): |
| 104 |
result.append(3) |
| 105 |
return True |
| 106 |
|
| 107 |
t1 = asyncio.create_task(c1(result)) |
| 108 |
t2 = asyncio.create_task(c2(result)) |
| 109 |
|
| 110 |
await asyncio.sleep(0) |
| 111 |
self.assertEqual([], result) |
| 112 |
|
| 113 |
lock.release() |
| 114 |
await asyncio.sleep(0) |
| 115 |
self.assertEqual([1], result) |
| 116 |
|
| 117 |
await asyncio.sleep(0) |
| 118 |
self.assertEqual([1], result) |
| 119 |
|
| 120 |
t3 = asyncio.create_task(c3(result)) |
| 121 |
|
| 122 |
lock.release() |
| 123 |
await asyncio.sleep(0) |
| 124 |
self.assertEqual([1, 2], result) |
| 125 |
|
| 126 |
lock.release() |
| 127 |
await asyncio.sleep(0) |
| 128 |
self.assertEqual([1, 2, 3], result) |
| 129 |
|
| 130 |
self.assertTrue(t1.done()) |
| 131 |
self.assertTrue(t1.result()) |
| 132 |
self.assertTrue(t2.done()) |
| 133 |
self.assertTrue(t2.result()) |
| 134 |
self.assertTrue(t3.done()) |
| 135 |
self.assertTrue(t3.result()) |
| 136 |
|
| 137 |
async def test_acquire_cancel(self): |
| 138 |
lock = asyncio.Lock() |
| 139 |
self.assertTrue(await lock.acquire()) |
| 140 |
|
| 141 |
task = asyncio.create_task(lock.acquire()) |
| 142 |
asyncio.get_running_loop().call_soon(task.cancel) |
| 143 |
with self.assertRaises(asyncio.CancelledError): |
| 144 |
await task |
| 145 |
self.assertFalse(lock._waiters) |
| 146 |
|
| 147 |
async def test_cancel_race(self): |
| 148 |
# Several tasks: |
| 149 |
# - A acquires the lock |
| 150 |
# - B is blocked in acquire() |
| 151 |
# - C is blocked in acquire() |
| 152 |
# |
| 153 |
# Now, concurrently: |
| 154 |
# - B is cancelled |
| 155 |
# - A releases the lock |
| 156 |
# |
| 157 |
# If B's waiter is marked cancelled but not yet removed from |
| 158 |
# _waiters, A's release() call will crash when trying to set |
| 159 |
# B's waiter; instead, it should move on to C's waiter. |
| 160 |
|
| 161 |
# Setup: A has the lock, b and c are waiting. |
| 162 |
lock = asyncio.Lock() |
| 163 |
|
| 164 |
async def lockit(name, blocker): |
| 165 |
await lock.acquire() |
| 166 |
try: |
| 167 |
if blocker is not None: |
| 168 |
await blocker |
| 169 |
finally: |
| 170 |
lock.release() |
| 171 |
|
| 172 |
fa = asyncio.get_running_loop().create_future() |
| 173 |
ta = asyncio.create_task(lockit('A', fa)) |
| 174 |
await asyncio.sleep(0) |
| 175 |
self.assertTrue(lock.locked()) |
| 176 |
tb = asyncio.create_task(lockit('B', None)) |
| 177 |
await asyncio.sleep(0) |
| 178 |
self.assertEqual(len(lock._waiters), 1) |
| 179 |
tc = asyncio.create_task(lockit('C', None)) |
| 180 |
await asyncio.sleep(0) |
| 181 |
self.assertEqual(len(lock._waiters), 2) |
| 182 |
|
| 183 |
# Create the race and check. |
| 184 |
# Without the fix this failed at the last assert. |
| 185 |
fa.set_result(None) |
| 186 |
tb.cancel() |
| 187 |
self.assertTrue(lock._waiters[0].cancelled()) |
| 188 |
await asyncio.sleep(0) |
| 189 |
self.assertFalse(lock.locked()) |
| 190 |
self.assertTrue(ta.done()) |
| 191 |
self.assertTrue(tb.cancelled()) |
| 192 |
await tc |
| 193 |
|
| 194 |
async def test_cancel_release_race(self): |
| 195 |
# Issue 32734 |
| 196 |
# Acquire 4 locks, cancel second, release first |
| 197 |
# and 2 locks are taken at once. |
| 198 |
loop = asyncio.get_running_loop() |
| 199 |
lock = asyncio.Lock() |
| 200 |
lock_count = 0 |
| 201 |
call_count = 0 |
| 202 |
|
| 203 |
async def lockit(): |
| 204 |
nonlocal lock_count |
| 205 |
nonlocal call_count |
| 206 |
call_count += 1 |
| 207 |
await lock.acquire() |
| 208 |
lock_count += 1 |
| 209 |
|
| 210 |
def trigger(): |
| 211 |
t1.cancel() |
| 212 |
lock.release() |
| 213 |
|
| 214 |
await lock.acquire() |
| 215 |
|
| 216 |
t1 = asyncio.create_task(lockit()) |
| 217 |
t2 = asyncio.create_task(lockit()) |
| 218 |
t3 = asyncio.create_task(lockit()) |
| 219 |
|
| 220 |
# Start scheduled tasks |
| 221 |
await asyncio.sleep(0) |
| 222 |
|
| 223 |
loop.call_soon(trigger) |
| 224 |
with self.assertRaises(asyncio.CancelledError): |
| 225 |
# Wait for cancellation |
| 226 |
await t1 |
| 227 |
|
| 228 |
# Make sure only one lock was taken |
| 229 |
self.assertEqual(lock_count, 1) |
| 230 |
# While 3 calls were made to lockit() |
| 231 |
self.assertEqual(call_count, 3) |
| 232 |
self.assertTrue(t1.cancelled() and t2.done()) |
| 233 |
|
| 234 |
# Cleanup the task that is stuck on acquire. |
| 235 |
t3.cancel() |
| 236 |
await asyncio.sleep(0) |
| 237 |
self.assertTrue(t3.cancelled()) |
| 238 |
|
| 239 |
async def test_finished_waiter_cancelled(self): |
| 240 |
lock = asyncio.Lock() |
| 241 |
|
| 242 |
await lock.acquire() |
| 243 |
self.assertTrue(lock.locked()) |
| 244 |
|
| 245 |
tb = asyncio.create_task(lock.acquire()) |
| 246 |
await asyncio.sleep(0) |
| 247 |
self.assertEqual(len(lock._waiters), 1) |
| 248 |
|
| 249 |
# Create a second waiter, wake up the first, and cancel it. |
| 250 |
# Without the fix, the second was not woken up. |
| 251 |
tc = asyncio.create_task(lock.acquire()) |
| 252 |
tb.cancel() |
| 253 |
lock.release() |
| 254 |
await asyncio.sleep(0) |
| 255 |
|
| 256 |
self.assertTrue(lock.locked()) |
| 257 |
self.assertTrue(tb.cancelled()) |
| 258 |
|
| 259 |
# Cleanup |
| 260 |
await tc |
| 261 |
|
| 262 |
async def test_release_not_acquired(self): |
| 263 |
lock = asyncio.Lock() |
| 264 |
|
| 265 |
self.assertRaises(RuntimeError, lock.release) |
| 266 |
|
| 267 |
async def test_release_no_waiters(self): |
| 268 |
lock = asyncio.Lock() |
| 269 |
await lock.acquire() |
| 270 |
self.assertTrue(lock.locked()) |
| 271 |
|
| 272 |
lock.release() |
| 273 |
self.assertFalse(lock.locked()) |
| 274 |
|
| 275 |
async def test_context_manager(self): |
| 276 |
lock = asyncio.Lock() |
| 277 |
self.assertFalse(lock.locked()) |
| 278 |
|
| 279 |
async with lock: |
| 280 |
self.assertTrue(lock.locked()) |
| 281 |
|
| 282 |
self.assertFalse(lock.locked()) |
| 283 |
|
| 284 |
|
| 285 |
class EventTests(unittest.IsolatedAsyncioTestCase): |
| 286 |
|
| 287 |
def test_repr(self): |
| 288 |
ev = asyncio.Event() |
| 289 |
self.assertEndsWith(repr(ev), '[unset]>') |
| 290 |
match = RGX_REPR.match(repr(ev)) |
| 291 |
self.assertEqual(match.group('extras'), 'unset') |
| 292 |
|
| 293 |
ev.set() |
| 294 |
self.assertEndsWith(repr(ev), '[set]>') |
| 295 |
self.assertTrue(RGX_REPR.match(repr(ev))) |
| 296 |
|
| 297 |
ev._waiters.append(mock.Mock()) |
| 298 |
self.assertTrue('waiters:1' in repr(ev)) |
| 299 |
self.assertTrue(RGX_REPR.match(repr(ev))) |
| 300 |
|
| 301 |
async def test_wait(self): |
| 302 |
ev = asyncio.Event() |
| 303 |
self.assertFalse(ev.is_set()) |
| 304 |
|
| 305 |
result = [] |
| 306 |
|
| 307 |
async def c1(result): |
| 308 |
if await ev.wait(): |
| 309 |
result.append(1) |
| 310 |
|
| 311 |
async def c2(result): |
| 312 |
if await ev.wait(): |
| 313 |
result.append(2) |
| 314 |
|
| 315 |
async def c3(result): |
| 316 |
if await ev.wait(): |
| 317 |
result.append(3) |
| 318 |
|
| 319 |
t1 = asyncio.create_task(c1(result)) |
| 320 |
t2 = asyncio.create_task(c2(result)) |
| 321 |
|
| 322 |
await asyncio.sleep(0) |
| 323 |
self.assertEqual([], result) |
| 324 |
|
| 325 |
t3 = asyncio.create_task(c3(result)) |
| 326 |
|
| 327 |
ev.set() |
| 328 |
await asyncio.sleep(0) |
| 329 |
self.assertEqual([3, 1, 2], result) |
| 330 |
|
| 331 |
self.assertTrue(t1.done()) |
| 332 |
self.assertIsNone(t1.result()) |
| 333 |
self.assertTrue(t2.done()) |
| 334 |
self.assertIsNone(t2.result()) |
| 335 |
self.assertTrue(t3.done()) |
| 336 |
self.assertIsNone(t3.result()) |
| 337 |
|
| 338 |
async def test_wait_on_set(self): |
| 339 |
ev = asyncio.Event() |
| 340 |
ev.set() |
| 341 |
|
| 342 |
res = await ev.wait() |
| 343 |
self.assertTrue(res) |
| 344 |
|
| 345 |
async def test_wait_cancel(self): |
| 346 |
ev = asyncio.Event() |
| 347 |
|
| 348 |
wait = asyncio.create_task(ev.wait()) |
| 349 |
asyncio.get_running_loop().call_soon(wait.cancel) |
| 350 |
with self.assertRaises(asyncio.CancelledError): |
| 351 |
await wait |
| 352 |
self.assertFalse(ev._waiters) |
| 353 |
|
| 354 |
async def test_clear(self): |
| 355 |
ev = asyncio.Event() |
| 356 |
self.assertFalse(ev.is_set()) |
| 357 |
|
| 358 |
ev.set() |
| 359 |
self.assertTrue(ev.is_set()) |
| 360 |
|
| 361 |
ev.clear() |
| 362 |
self.assertFalse(ev.is_set()) |
| 363 |
|
| 364 |
async def test_clear_with_waiters(self): |
| 365 |
ev = asyncio.Event() |
| 366 |
result = [] |
| 367 |
|
| 368 |
async def c1(result): |
| 369 |
if await ev.wait(): |
| 370 |
result.append(1) |
| 371 |
return True |
| 372 |
|
| 373 |
t = asyncio.create_task(c1(result)) |
| 374 |
await asyncio.sleep(0) |
| 375 |
self.assertEqual([], result) |
| 376 |
|
| 377 |
ev.set() |
| 378 |
ev.clear() |
| 379 |
self.assertFalse(ev.is_set()) |
| 380 |
|
| 381 |
ev.set() |
| 382 |
ev.set() |
| 383 |
self.assertEqual(1, len(ev._waiters)) |
| 384 |
|
| 385 |
await asyncio.sleep(0) |
| 386 |
self.assertEqual([1], result) |
| 387 |
self.assertEqual(0, len(ev._waiters)) |
| 388 |
|
| 389 |
self.assertTrue(t.done()) |
| 390 |
self.assertTrue(t.result()) |
| 391 |
|
| 392 |
|
| 393 |
class ConditionTests(unittest.IsolatedAsyncioTestCase): |
| 394 |
|
| 395 |
async def test_wait(self): |
| 396 |
cond = asyncio.Condition() |
| 397 |
result = [] |
| 398 |
|
| 399 |
async def c1(result): |
| 400 |
await cond.acquire() |
| 401 |
if await cond.wait(): |
| 402 |
result.append(1) |
| 403 |
return True |
| 404 |
|
| 405 |
async def c2(result): |
| 406 |
await cond.acquire() |
| 407 |
if await cond.wait(): |
| 408 |
result.append(2) |
| 409 |
return True |
| 410 |
|
| 411 |
async def c3(result): |
| 412 |
await cond.acquire() |
| 413 |
if await cond.wait(): |
| 414 |
result.append(3) |
| 415 |
return True |
| 416 |
|
| 417 |
t1 = asyncio.create_task(c1(result)) |
| 418 |
t2 = asyncio.create_task(c2(result)) |
| 419 |
t3 = asyncio.create_task(c3(result)) |
| 420 |
|
| 421 |
await asyncio.sleep(0) |
| 422 |
self.assertEqual([], result) |
| 423 |
self.assertFalse(cond.locked()) |
| 424 |
|
| 425 |
self.assertTrue(await cond.acquire()) |
| 426 |
cond.notify() |
| 427 |
await asyncio.sleep(0) |
| 428 |
self.assertEqual([], result) |
| 429 |
self.assertTrue(cond.locked()) |
| 430 |
|
| 431 |
cond.release() |
| 432 |
await asyncio.sleep(0) |
| 433 |
self.assertEqual([1], result) |
| 434 |
self.assertTrue(cond.locked()) |
| 435 |
|
| 436 |
cond.notify(2) |
| 437 |
await asyncio.sleep(0) |
| 438 |
self.assertEqual([1], result) |
| 439 |
self.assertTrue(cond.locked()) |
| 440 |
|
| 441 |
cond.release() |
| 442 |
await asyncio.sleep(0) |
| 443 |
self.assertEqual([1, 2], result) |
| 444 |
self.assertTrue(cond.locked()) |
| 445 |
|
| 446 |
cond.release() |
| 447 |
await asyncio.sleep(0) |
| 448 |
self.assertEqual([1, 2, 3], result) |
| 449 |
self.assertTrue(cond.locked()) |
| 450 |
|
| 451 |
self.assertTrue(t1.done()) |
| 452 |
self.assertTrue(t1.result()) |
| 453 |
self.assertTrue(t2.done()) |
| 454 |
self.assertTrue(t2.result()) |
| 455 |
self.assertTrue(t3.done()) |
| 456 |
self.assertTrue(t3.result()) |
| 457 |
|
| 458 |
async def test_wait_cancel(self): |
| 459 |
cond = asyncio.Condition() |
| 460 |
await cond.acquire() |
| 461 |
|
| 462 |
wait = asyncio.create_task(cond.wait()) |
| 463 |
asyncio.get_running_loop().call_soon(wait.cancel) |
| 464 |
with self.assertRaises(asyncio.CancelledError): |
| 465 |
await wait |
| 466 |
self.assertFalse(cond._waiters) |
| 467 |
self.assertTrue(cond.locked()) |
| 468 |
|
| 469 |
async def test_wait_cancel_contested(self): |
| 470 |
cond = asyncio.Condition() |
| 471 |
|
| 472 |
await cond.acquire() |
| 473 |
self.assertTrue(cond.locked()) |
| 474 |
|
| 475 |
wait_task = asyncio.create_task(cond.wait()) |
| 476 |
await asyncio.sleep(0) |
| 477 |
self.assertFalse(cond.locked()) |
| 478 |
|
| 479 |
# Notify, but contest the lock before cancelling |
| 480 |
await cond.acquire() |
| 481 |
self.assertTrue(cond.locked()) |
| 482 |
cond.notify() |
| 483 |
asyncio.get_running_loop().call_soon(wait_task.cancel) |
| 484 |
asyncio.get_running_loop().call_soon(cond.release) |
| 485 |
|
| 486 |
try: |
| 487 |
await wait_task |
| 488 |
except asyncio.CancelledError: |
| 489 |
# Should not happen, since no cancellation points |
| 490 |
pass |
| 491 |
|
| 492 |
self.assertTrue(cond.locked()) |
| 493 |
|
| 494 |
async def test_wait_cancel_after_notify(self): |
| 495 |
# See bpo-32841 |
| 496 |
waited = False |
| 497 |
|
| 498 |
cond = asyncio.Condition() |
| 499 |
|
| 500 |
async def wait_on_cond(): |
| 501 |
nonlocal waited |
| 502 |
async with cond: |
| 503 |
waited = True # Make sure this area was reached |
| 504 |
await cond.wait() |
| 505 |
|
| 506 |
waiter = asyncio.create_task(wait_on_cond()) |
| 507 |
await asyncio.sleep(0) # Start waiting |
| 508 |
|
| 509 |
await cond.acquire() |
| 510 |
cond.notify() |
| 511 |
await asyncio.sleep(0) # Get to acquire() |
| 512 |
waiter.cancel() |
| 513 |
await asyncio.sleep(0) # Activate cancellation |
| 514 |
cond.release() |
| 515 |
await asyncio.sleep(0) # Cancellation should occur |
| 516 |
|
| 517 |
self.assertTrue(waiter.cancelled()) |
| 518 |
self.assertTrue(waited) |
| 519 |
|
| 520 |
async def test_wait_unacquired(self): |
| 521 |
cond = asyncio.Condition() |
| 522 |
with self.assertRaises(RuntimeError): |
| 523 |
await cond.wait() |
| 524 |
|
| 525 |
async def test_wait_for(self): |
| 526 |
cond = asyncio.Condition() |
| 527 |
presult = False |
| 528 |
|
| 529 |
def predicate(): |
| 530 |
return presult |
| 531 |
|
| 532 |
result = [] |
| 533 |
|
| 534 |
async def c1(result): |
| 535 |
await cond.acquire() |
| 536 |
if await cond.wait_for(predicate): |
| 537 |
result.append(1) |
| 538 |
cond.release() |
| 539 |
return True |
| 540 |
|
| 541 |
t = asyncio.create_task(c1(result)) |
| 542 |
|
| 543 |
await asyncio.sleep(0) |
| 544 |
self.assertEqual([], result) |
| 545 |
|
| 546 |
await cond.acquire() |
| 547 |
cond.notify() |
| 548 |
cond.release() |
| 549 |
await asyncio.sleep(0) |
| 550 |
self.assertEqual([], result) |
| 551 |
|
| 552 |
presult = True |
| 553 |
await cond.acquire() |
| 554 |
cond.notify() |
| 555 |
cond.release() |
| 556 |
await asyncio.sleep(0) |
| 557 |
self.assertEqual([1], result) |
| 558 |
|
| 559 |
self.assertTrue(t.done()) |
| 560 |
self.assertTrue(t.result()) |
| 561 |
|
| 562 |
async def test_wait_for_unacquired(self): |
| 563 |
cond = asyncio.Condition() |
| 564 |
|
| 565 |
# predicate can return true immediately |
| 566 |
res = await cond.wait_for(lambda: [1, 2, 3]) |
| 567 |
self.assertEqual([1, 2, 3], res) |
| 568 |
|
| 569 |
with self.assertRaises(RuntimeError): |
| 570 |
await cond.wait_for(lambda: False) |
| 571 |
|
| 572 |
async def test_notify(self): |
| 573 |
cond = asyncio.Condition() |
| 574 |
result = [] |
| 575 |
|
| 576 |
async def c1(result): |
| 577 |
await cond.acquire() |
| 578 |
if await cond.wait(): |
| 579 |
result.append(1) |
| 580 |
cond.release() |
| 581 |
return True |
| 582 |
|
| 583 |
async def c2(result): |
| 584 |
await cond.acquire() |
| 585 |
if await cond.wait(): |
| 586 |
result.append(2) |
| 587 |
cond.release() |
| 588 |
return True |
| 589 |
|
| 590 |
async def c3(result): |
| 591 |
await cond.acquire() |
| 592 |
if await cond.wait(): |
| 593 |
result.append(3) |
| 594 |
cond.release() |
| 595 |
return True |
| 596 |
|
| 597 |
t1 = asyncio.create_task(c1(result)) |
| 598 |
t2 = asyncio.create_task(c2(result)) |
| 599 |
t3 = asyncio.create_task(c3(result)) |
| 600 |
|
| 601 |
await asyncio.sleep(0) |
| 602 |
self.assertEqual([], result) |
| 603 |
|
| 604 |
await cond.acquire() |
| 605 |
cond.notify(1) |
| 606 |
cond.release() |
| 607 |
await asyncio.sleep(0) |
| 608 |
self.assertEqual([1], result) |
| 609 |
|
| 610 |
await cond.acquire() |
| 611 |
cond.notify(1) |
| 612 |
cond.notify(2048) |
| 613 |
cond.release() |
| 614 |
await asyncio.sleep(0) |
| 615 |
self.assertEqual([1, 2, 3], result) |
| 616 |
|
| 617 |
self.assertTrue(t1.done()) |
| 618 |
self.assertTrue(t1.result()) |
| 619 |
self.assertTrue(t2.done()) |
| 620 |
self.assertTrue(t2.result()) |
| 621 |
self.assertTrue(t3.done()) |
| 622 |
self.assertTrue(t3.result()) |
| 623 |
|
| 624 |
async def test_notify_all(self): |
| 625 |
cond = asyncio.Condition() |
| 626 |
|
| 627 |
result = [] |
| 628 |
|
| 629 |
async def c1(result): |
| 630 |
await cond.acquire() |
| 631 |
if await cond.wait(): |
| 632 |
result.append(1) |
| 633 |
cond.release() |
| 634 |
return True |
| 635 |
|
| 636 |
async def c2(result): |
| 637 |
await cond.acquire() |
| 638 |
if await cond.wait(): |
| 639 |
result.append(2) |
| 640 |
cond.release() |
| 641 |
return True |
| 642 |
|
| 643 |
t1 = asyncio.create_task(c1(result)) |
| 644 |
t2 = asyncio.create_task(c2(result)) |
| 645 |
|
| 646 |
await asyncio.sleep(0) |
| 647 |
self.assertEqual([], result) |
| 648 |
|
| 649 |
await cond.acquire() |
| 650 |
cond.notify_all() |
| 651 |
cond.release() |
| 652 |
await asyncio.sleep(0) |
| 653 |
self.assertEqual([1, 2], result) |
| 654 |
|
| 655 |
self.assertTrue(t1.done()) |
| 656 |
self.assertTrue(t1.result()) |
| 657 |
self.assertTrue(t2.done()) |
| 658 |
self.assertTrue(t2.result()) |
| 659 |
|
| 660 |
def test_notify_unacquired(self): |
| 661 |
cond = asyncio.Condition() |
| 662 |
self.assertRaises(RuntimeError, cond.notify) |
| 663 |
|
| 664 |
def test_notify_all_unacquired(self): |
| 665 |
cond = asyncio.Condition() |
| 666 |
self.assertRaises(RuntimeError, cond.notify_all) |
| 667 |
|
| 668 |
async def test_repr(self): |
| 669 |
cond = asyncio.Condition() |
| 670 |
self.assertTrue('unlocked' in repr(cond)) |
| 671 |
self.assertTrue(RGX_REPR.match(repr(cond))) |
| 672 |
|
| 673 |
await cond.acquire() |
| 674 |
self.assertTrue('locked' in repr(cond)) |
| 675 |
|
| 676 |
cond._waiters.append(mock.Mock()) |
| 677 |
self.assertTrue('waiters:1' in repr(cond)) |
| 678 |
self.assertTrue(RGX_REPR.match(repr(cond))) |
| 679 |
|
| 680 |
cond._waiters.append(mock.Mock()) |
| 681 |
self.assertTrue('waiters:2' in repr(cond)) |
| 682 |
self.assertTrue(RGX_REPR.match(repr(cond))) |
| 683 |
|
| 684 |
async def test_context_manager(self): |
| 685 |
cond = asyncio.Condition() |
| 686 |
self.assertFalse(cond.locked()) |
| 687 |
async with cond: |
| 688 |
self.assertTrue(cond.locked()) |
| 689 |
self.assertFalse(cond.locked()) |
| 690 |
|
| 691 |
async def test_explicit_lock(self): |
| 692 |
async def f(lock=None, cond=None): |
| 693 |
if lock is None: |
| 694 |
lock = asyncio.Lock() |
| 695 |
if cond is None: |
| 696 |
cond = asyncio.Condition(lock) |
| 697 |
self.assertIs(cond._lock, lock) |
| 698 |
self.assertFalse(lock.locked()) |
| 699 |
self.assertFalse(cond.locked()) |
| 700 |
async with cond: |
| 701 |
self.assertTrue(lock.locked()) |
| 702 |
self.assertTrue(cond.locked()) |
| 703 |
self.assertFalse(lock.locked()) |
| 704 |
self.assertFalse(cond.locked()) |
| 705 |
async with lock: |
| 706 |
self.assertTrue(lock.locked()) |
| 707 |
self.assertTrue(cond.locked()) |
| 708 |
self.assertFalse(lock.locked()) |
| 709 |
self.assertFalse(cond.locked()) |
| 710 |
|
| 711 |
# All should work in the same way. |
| 712 |
await f() |
| 713 |
await f(asyncio.Lock()) |
| 714 |
lock = asyncio.Lock() |
| 715 |
await f(lock, asyncio.Condition(lock)) |
| 716 |
|
| 717 |
async def test_ambiguous_loops(self): |
| 718 |
loop = asyncio.new_event_loop() |
| 719 |
self.addCleanup(loop.close) |
| 720 |
|
| 721 |
async def wrong_loop_in_lock(): |
| 722 |
with self.assertRaises(TypeError): |
| 723 |
asyncio.Lock(loop=loop) # actively disallowed since 3.10 |
| 724 |
lock = asyncio.Lock() |
| 725 |
lock._loop = loop # use private API for testing |
| 726 |
async with lock: |
| 727 |
# acquired immediately via the fast-path |
| 728 |
# without interaction with any event loop. |
| 729 |
cond = asyncio.Condition(lock) |
| 730 |
# cond.acquire() will trigger waiting on the lock |
| 731 |
# and it will discover the event loop mismatch. |
| 732 |
with self.assertRaisesRegex( |
| 733 |
RuntimeError, |
| 734 |
"is bound to a different event loop", |
| 735 |
): |
| 736 |
await cond.acquire() |
| 737 |
|
| 738 |
async def wrong_loop_in_cond(): |
| 739 |
# Same analogy here with the condition's loop. |
| 740 |
lock = asyncio.Lock() |
| 741 |
async with lock: |
| 742 |
with self.assertRaises(TypeError): |
| 743 |
asyncio.Condition(lock, loop=loop) |
| 744 |
cond = asyncio.Condition(lock) |
| 745 |
cond._loop = loop |
| 746 |
with self.assertRaisesRegex( |
| 747 |
RuntimeError, |
| 748 |
"is bound to a different event loop", |
| 749 |
): |
| 750 |
await cond.wait() |
| 751 |
|
| 752 |
await wrong_loop_in_lock() |
| 753 |
await wrong_loop_in_cond() |
| 754 |
|
| 755 |
async def test_timeout_in_block(self): |
| 756 |
condition = asyncio.Condition() |
| 757 |
async with condition: |
| 758 |
with self.assertRaises(asyncio.TimeoutError): |
| 759 |
await asyncio.wait_for(condition.wait(), timeout=0.5) |
| 760 |
|
| 761 |
async def test_cancelled_error_wakeup(self): |
| 762 |
# Test that a cancelled error, received when awaiting wakeup, |
| 763 |
# will be re-raised un-modified. |
| 764 |
wake = False |
| 765 |
raised = None |
| 766 |
cond = asyncio.Condition() |
| 767 |
|
| 768 |
async def func(): |
| 769 |
nonlocal raised |
| 770 |
async with cond: |
| 771 |
with self.assertRaises(asyncio.CancelledError) as err: |
| 772 |
await cond.wait_for(lambda: wake) |
| 773 |
raised = err.exception |
| 774 |
raise raised |
| 775 |
|
| 776 |
task = asyncio.create_task(func()) |
| 777 |
await asyncio.sleep(0) |
| 778 |
# Task is waiting on the condition, cancel it there. |
| 779 |
task.cancel(msg="foo") |
| 780 |
with self.assertRaises(asyncio.CancelledError) as err: |
| 781 |
await task |
| 782 |
self.assertEqual(err.exception.args, ("foo",)) |
| 783 |
# We should have got the _same_ exception instance as the one |
| 784 |
# originally raised. |
| 785 |
self.assertIs(err.exception, raised) |
| 786 |
|
| 787 |
async def test_cancelled_error_re_aquire(self): |
| 788 |
# Test that a cancelled error, received when re-aquiring lock, |
| 789 |
# will be re-raised un-modified. |
| 790 |
wake = False |
| 791 |
raised = None |
| 792 |
cond = asyncio.Condition() |
| 793 |
|
| 794 |
async def func(): |
| 795 |
nonlocal raised |
| 796 |
async with cond: |
| 797 |
with self.assertRaises(asyncio.CancelledError) as err: |
| 798 |
await cond.wait_for(lambda: wake) |
| 799 |
raised = err.exception |
| 800 |
raise raised |
| 801 |
|
| 802 |
task = asyncio.create_task(func()) |
| 803 |
await asyncio.sleep(0) |
| 804 |
# Task is waiting on the condition |
| 805 |
await cond.acquire() |
| 806 |
wake = True |
| 807 |
cond.notify() |
| 808 |
await asyncio.sleep(0) |
| 809 |
# Task is now trying to re-acquire the lock, cancel it there. |
| 810 |
task.cancel(msg="foo") |
| 811 |
cond.release() |
| 812 |
with self.assertRaises(asyncio.CancelledError) as err: |
| 813 |
await task |
| 814 |
self.assertEqual(err.exception.args, ("foo",)) |
| 815 |
# We should have got the _same_ exception instance as the one |
| 816 |
# originally raised. |
| 817 |
self.assertIs(err.exception, raised) |
| 818 |
|
| 819 |
async def test_cancelled_wakeup(self): |
| 820 |
# Test that a task cancelled at the "same" time as it is woken |
| 821 |
# up as part of a Condition.notify() does not result in a lost wakeup. |
| 822 |
# This test simulates a cancel while the target task is awaiting initial |
| 823 |
# wakeup on the wakeup queue. |
| 824 |
condition = asyncio.Condition() |
| 825 |
state = 0 |
| 826 |
async def consumer(): |
| 827 |
nonlocal state |
| 828 |
async with condition: |
| 829 |
while True: |
| 830 |
await condition.wait_for(lambda: state != 0) |
| 831 |
if state < 0: |
| 832 |
return |
| 833 |
state -= 1 |
| 834 |
|
| 835 |
# create two consumers |
| 836 |
c = [asyncio.create_task(consumer()) for _ in range(2)] |
| 837 |
# wait for them to settle |
| 838 |
await asyncio.sleep(0) |
| 839 |
async with condition: |
| 840 |
# produce one item and wake up one |
| 841 |
state += 1 |
| 842 |
condition.notify(1) |
| 843 |
|
| 844 |
# Cancel it while it is awaiting to be run. |
| 845 |
# This cancellation could come from the outside |
| 846 |
c[0].cancel() |
| 847 |
|
| 848 |
# now wait for the item to be consumed |
| 849 |
# if it doesn't means that our "notify" didn"t take hold. |
| 850 |
# because it raced with a cancel() |
| 851 |
try: |
| 852 |
async with asyncio.timeout(0.01): |
| 853 |
await condition.wait_for(lambda: state == 0) |
| 854 |
except TimeoutError: |
| 855 |
pass |
| 856 |
self.assertEqual(state, 0) |
| 857 |
|
| 858 |
# clean up |
| 859 |
state = -1 |
| 860 |
condition.notify_all() |
| 861 |
await c[1] |
| 862 |
|
| 863 |
async def test_cancelled_wakeup_relock(self): |
| 864 |
# Test that a task cancelled at the "same" time as it is woken |
| 865 |
# up as part of a Condition.notify() does not result in a lost wakeup. |
| 866 |
# This test simulates a cancel while the target task is acquiring the lock |
| 867 |
# again. |
| 868 |
condition = asyncio.Condition() |
| 869 |
state = 0 |
| 870 |
async def consumer(): |
| 871 |
nonlocal state |
| 872 |
async with condition: |
| 873 |
while True: |
| 874 |
await condition.wait_for(lambda: state != 0) |
| 875 |
if state < 0: |
| 876 |
return |
| 877 |
state -= 1 |
| 878 |
|
| 879 |
# create two consumers |
| 880 |
c = [asyncio.create_task(consumer()) for _ in range(2)] |
| 881 |
# wait for them to settle |
| 882 |
await asyncio.sleep(0) |
| 883 |
async with condition: |
| 884 |
# produce one item and wake up one |
| 885 |
state += 1 |
| 886 |
condition.notify(1) |
| 887 |
|
| 888 |
# now we sleep for a bit. This allows the target task to wake up and |
| 889 |
# settle on re-aquiring the lock |
| 890 |
await asyncio.sleep(0) |
| 891 |
|
| 892 |
# Cancel it while awaiting the lock |
| 893 |
# This cancel could come the outside. |
| 894 |
c[0].cancel() |
| 895 |
|
| 896 |
# now wait for the item to be consumed |
| 897 |
# if it doesn't means that our "notify" didn"t take hold. |
| 898 |
# because it raced with a cancel() |
| 899 |
try: |
| 900 |
async with asyncio.timeout(0.01): |
| 901 |
await condition.wait_for(lambda: state == 0) |
| 902 |
except TimeoutError: |
| 903 |
pass |
| 904 |
self.assertEqual(state, 0) |
| 905 |
|
| 906 |
# clean up |
| 907 |
state = -1 |
| 908 |
condition.notify_all() |
| 909 |
await c[1] |
| 910 |
|
| 911 |
class SemaphoreTests(unittest.IsolatedAsyncioTestCase): |
| 912 |
|
| 913 |
def test_initial_value_zero(self): |
| 914 |
sem = asyncio.Semaphore(0) |
| 915 |
self.assertTrue(sem.locked()) |
| 916 |
|
| 917 |
async def test_repr(self): |
| 918 |
sem = asyncio.Semaphore() |
| 919 |
self.assertEndsWith(repr(sem), '[unlocked, value:1]>') |
| 920 |
self.assertTrue(RGX_REPR.match(repr(sem))) |
| 921 |
|
| 922 |
await sem.acquire() |
| 923 |
self.assertEndsWith(repr(sem), '[locked]>') |
| 924 |
self.assertTrue('waiters' not in repr(sem)) |
| 925 |
self.assertTrue(RGX_REPR.match(repr(sem))) |
| 926 |
|
| 927 |
if sem._waiters is None: |
| 928 |
sem._waiters = collections.deque() |
| 929 |
|
| 930 |
sem._waiters.append(mock.Mock()) |
| 931 |
self.assertTrue('waiters:1' in repr(sem)) |
| 932 |
self.assertTrue(RGX_REPR.match(repr(sem))) |
| 933 |
|
| 934 |
sem._waiters.append(mock.Mock()) |
| 935 |
self.assertTrue('waiters:2' in repr(sem)) |
| 936 |
self.assertTrue(RGX_REPR.match(repr(sem))) |
| 937 |
|
| 938 |
async def test_semaphore(self): |
| 939 |
sem = asyncio.Semaphore() |
| 940 |
self.assertEqual(1, sem._value) |
| 941 |
|
| 942 |
with self.assertRaisesRegex( |
| 943 |
TypeError, |
| 944 |
"'Semaphore' object can't be awaited", |
| 945 |
): |
| 946 |
await sem |
| 947 |
|
| 948 |
self.assertFalse(sem.locked()) |
| 949 |
self.assertEqual(1, sem._value) |
| 950 |
|
| 951 |
def test_semaphore_value(self): |
| 952 |
self.assertRaises(ValueError, asyncio.Semaphore, -1) |
| 953 |
|
| 954 |
async def test_acquire(self): |
| 955 |
sem = asyncio.Semaphore(3) |
| 956 |
result = [] |
| 957 |
|
| 958 |
self.assertTrue(await sem.acquire()) |
| 959 |
self.assertTrue(await sem.acquire()) |
| 960 |
self.assertFalse(sem.locked()) |
| 961 |
|
| 962 |
async def c1(result): |
| 963 |
await sem.acquire() |
| 964 |
result.append(1) |
| 965 |
return True |
| 966 |
|
| 967 |
async def c2(result): |
| 968 |
await sem.acquire() |
| 969 |
result.append(2) |
| 970 |
return True |
| 971 |
|
| 972 |
async def c3(result): |
| 973 |
await sem.acquire() |
| 974 |
result.append(3) |
| 975 |
return True |
| 976 |
|
| 977 |
async def c4(result): |
| 978 |
await sem.acquire() |
| 979 |
result.append(4) |
| 980 |
return True |
| 981 |
|
| 982 |
t1 = asyncio.create_task(c1(result)) |
| 983 |
t2 = asyncio.create_task(c2(result)) |
| 984 |
t3 = asyncio.create_task(c3(result)) |
| 985 |
|
| 986 |
await asyncio.sleep(0) |
| 987 |
self.assertEqual([1], result) |
| 988 |
self.assertTrue(sem.locked()) |
| 989 |
self.assertEqual(2, len(sem._waiters)) |
| 990 |
self.assertEqual(0, sem._value) |
| 991 |
|
| 992 |
t4 = asyncio.create_task(c4(result)) |
| 993 |
|
| 994 |
sem.release() |
| 995 |
sem.release() |
| 996 |
self.assertEqual(0, sem._value) |
| 997 |
|
| 998 |
await asyncio.sleep(0) |
| 999 |
self.assertEqual(0, sem._value) |
| 1000 |
self.assertEqual(3, len(result)) |
| 1001 |
self.assertTrue(sem.locked()) |
| 1002 |
self.assertEqual(1, len(sem._waiters)) |
| 1003 |
self.assertEqual(0, sem._value) |
| 1004 |
|
| 1005 |
self.assertTrue(t1.done()) |
| 1006 |
self.assertTrue(t1.result()) |
| 1007 |
race_tasks = [t2, t3, t4] |
| 1008 |
done_tasks = [t for t in race_tasks if t.done() and t.result()] |
| 1009 |
self.assertEqual(2, len(done_tasks)) |
| 1010 |
|
| 1011 |
# cleanup locked semaphore |
| 1012 |
sem.release() |
| 1013 |
await asyncio.gather(*race_tasks) |
| 1014 |
|
| 1015 |
async def test_acquire_cancel(self): |
| 1016 |
sem = asyncio.Semaphore() |
| 1017 |
await sem.acquire() |
| 1018 |
|
| 1019 |
acquire = asyncio.create_task(sem.acquire()) |
| 1020 |
asyncio.get_running_loop().call_soon(acquire.cancel) |
| 1021 |
with self.assertRaises(asyncio.CancelledError): |
| 1022 |
await acquire |
| 1023 |
self.assertTrue((not sem._waiters) or |
| 1024 |
all(waiter.done() for waiter in sem._waiters)) |
| 1025 |
|
| 1026 |
async def test_acquire_cancel_before_awoken(self): |
| 1027 |
sem = asyncio.Semaphore(value=0) |
| 1028 |
|
| 1029 |
t1 = asyncio.create_task(sem.acquire()) |
| 1030 |
t2 = asyncio.create_task(sem.acquire()) |
| 1031 |
t3 = asyncio.create_task(sem.acquire()) |
| 1032 |
t4 = asyncio.create_task(sem.acquire()) |
| 1033 |
|
| 1034 |
await asyncio.sleep(0) |
| 1035 |
|
| 1036 |
t1.cancel() |
| 1037 |
t2.cancel() |
| 1038 |
sem.release() |
| 1039 |
|
| 1040 |
await asyncio.sleep(0) |
| 1041 |
await asyncio.sleep(0) |
| 1042 |
num_done = sum(t.done() for t in [t3, t4]) |
| 1043 |
self.assertEqual(num_done, 1) |
| 1044 |
self.assertTrue(t3.done()) |
| 1045 |
self.assertFalse(t4.done()) |
| 1046 |
|
| 1047 |
t3.cancel() |
| 1048 |
t4.cancel() |
| 1049 |
await asyncio.sleep(0) |
| 1050 |
|
| 1051 |
async def test_acquire_hang(self): |
| 1052 |
sem = asyncio.Semaphore(value=0) |
| 1053 |
|
| 1054 |
t1 = asyncio.create_task(sem.acquire()) |
| 1055 |
t2 = asyncio.create_task(sem.acquire()) |
| 1056 |
await asyncio.sleep(0) |
| 1057 |
|
| 1058 |
t1.cancel() |
| 1059 |
sem.release() |
| 1060 |
await asyncio.sleep(0) |
| 1061 |
await asyncio.sleep(0) |
| 1062 |
self.assertTrue(sem.locked()) |
| 1063 |
self.assertTrue(t2.done()) |
| 1064 |
|
| 1065 |
async def test_acquire_no_hang(self): |
| 1066 |
|
| 1067 |
sem = asyncio.Semaphore(1) |
| 1068 |
|
| 1069 |
async def c1(): |
| 1070 |
async with sem: |
| 1071 |
await asyncio.sleep(0) |
| 1072 |
t2.cancel() |
| 1073 |
|
| 1074 |
async def c2(): |
| 1075 |
async with sem: |
| 1076 |
self.assertFalse(True) |
| 1077 |
|
| 1078 |
t1 = asyncio.create_task(c1()) |
| 1079 |
t2 = asyncio.create_task(c2()) |
| 1080 |
|
| 1081 |
r1, r2 = await asyncio.gather(t1, t2, return_exceptions=True) |
| 1082 |
self.assertTrue(r1 is None) |
| 1083 |
self.assertTrue(isinstance(r2, asyncio.CancelledError)) |
| 1084 |
|
| 1085 |
await asyncio.wait_for(sem.acquire(), timeout=1.0) |
| 1086 |
|
| 1087 |
def test_release_not_acquired(self): |
| 1088 |
sem = asyncio.BoundedSemaphore() |
| 1089 |
|
| 1090 |
self.assertRaises(ValueError, sem.release) |
| 1091 |
|
| 1092 |
async def test_release_no_waiters(self): |
| 1093 |
sem = asyncio.Semaphore() |
| 1094 |
await sem.acquire() |
| 1095 |
self.assertTrue(sem.locked()) |
| 1096 |
|
| 1097 |
sem.release() |
| 1098 |
self.assertFalse(sem.locked()) |
| 1099 |
|
| 1100 |
async def test_acquire_fifo_order(self): |
| 1101 |
sem = asyncio.Semaphore(1) |
| 1102 |
result = [] |
| 1103 |
|
| 1104 |
async def coro(tag): |
| 1105 |
await sem.acquire() |
| 1106 |
result.append(f'{tag}_1') |
| 1107 |
await asyncio.sleep(0.01) |
| 1108 |
sem.release() |
| 1109 |
|
| 1110 |
await sem.acquire() |
| 1111 |
result.append(f'{tag}_2') |
| 1112 |
await asyncio.sleep(0.01) |
| 1113 |
sem.release() |
| 1114 |
|
| 1115 |
async with asyncio.TaskGroup() as tg: |
| 1116 |
tg.create_task(coro('c1')) |
| 1117 |
tg.create_task(coro('c2')) |
| 1118 |
tg.create_task(coro('c3')) |
| 1119 |
|
| 1120 |
self.assertEqual( |
| 1121 |
['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'], |
| 1122 |
result |
| 1123 |
) |
| 1124 |
|
| 1125 |
async def test_acquire_fifo_order_2(self): |
| 1126 |
sem = asyncio.Semaphore(1) |
| 1127 |
result = [] |
| 1128 |
|
| 1129 |
async def c1(result): |
| 1130 |
await sem.acquire() |
| 1131 |
result.append(1) |
| 1132 |
return True |
| 1133 |
|
| 1134 |
async def c2(result): |
| 1135 |
await sem.acquire() |
| 1136 |
result.append(2) |
| 1137 |
sem.release() |
| 1138 |
await sem.acquire() |
| 1139 |
result.append(4) |
| 1140 |
return True |
| 1141 |
|
| 1142 |
async def c3(result): |
| 1143 |
await sem.acquire() |
| 1144 |
result.append(3) |
| 1145 |
return True |
| 1146 |
|
| 1147 |
t1 = asyncio.create_task(c1(result)) |
| 1148 |
t2 = asyncio.create_task(c2(result)) |
| 1149 |
t3 = asyncio.create_task(c3(result)) |
| 1150 |
|
| 1151 |
await asyncio.sleep(0) |
| 1152 |
|
| 1153 |
sem.release() |
| 1154 |
sem.release() |
| 1155 |
|
| 1156 |
tasks = [t1, t2, t3] |
| 1157 |
await asyncio.gather(*tasks) |
| 1158 |
self.assertEqual([1, 2, 3, 4], result) |
| 1159 |
|
| 1160 |
async def test_acquire_fifo_order_3(self): |
| 1161 |
sem = asyncio.Semaphore(0) |
| 1162 |
result = [] |
| 1163 |
|
| 1164 |
async def c1(result): |
| 1165 |
await sem.acquire() |
| 1166 |
result.append(1) |
| 1167 |
return True |
| 1168 |
|
| 1169 |
async def c2(result): |
| 1170 |
await sem.acquire() |
| 1171 |
result.append(2) |
| 1172 |
return True |
| 1173 |
|
| 1174 |
async def c3(result): |
| 1175 |
await sem.acquire() |
| 1176 |
result.append(3) |
| 1177 |
return True |
| 1178 |
|
| 1179 |
t1 = asyncio.create_task(c1(result)) |
| 1180 |
t2 = asyncio.create_task(c2(result)) |
| 1181 |
t3 = asyncio.create_task(c3(result)) |
| 1182 |
|
| 1183 |
await asyncio.sleep(0) |
| 1184 |
|
| 1185 |
t1.cancel() |
| 1186 |
|
| 1187 |
await asyncio.sleep(0) |
| 1188 |
|
| 1189 |
sem.release() |
| 1190 |
sem.release() |
| 1191 |
|
| 1192 |
tasks = [t1, t2, t3] |
| 1193 |
await asyncio.gather(*tasks, return_exceptions=True) |
| 1194 |
self.assertEqual([2, 3], result) |
| 1195 |
|
| 1196 |
async def test_acquire_fifo_order_4(self): |
| 1197 |
# Test that a successful `acquire()` will wake up multiple Tasks |
| 1198 |
# that were waiting in the Semaphore queue due to FIFO rules. |
| 1199 |
sem = asyncio.Semaphore(0) |
| 1200 |
result = [] |
| 1201 |
count = 0 |
| 1202 |
|
| 1203 |
async def c1(result): |
| 1204 |
# First task immediately waits for semaphore. It will be awoken by c2. |
| 1205 |
self.assertEqual(sem._value, 0) |
| 1206 |
await sem.acquire() |
| 1207 |
# We should have woken up all waiting tasks now. |
| 1208 |
self.assertEqual(sem._value, 0) |
| 1209 |
# Create a fourth task. It should run after c3, not c2. |
| 1210 |
nonlocal t4 |
| 1211 |
t4 = asyncio.create_task(c4(result)) |
| 1212 |
result.append(1) |
| 1213 |
return True |
| 1214 |
|
| 1215 |
async def c2(result): |
| 1216 |
# The second task begins by releasing semaphore three times, |
| 1217 |
# for c1, c2, and c3. |
| 1218 |
sem.release() |
| 1219 |
sem.release() |
| 1220 |
sem.release() |
| 1221 |
self.assertEqual(sem._value, 2) |
| 1222 |
# It is locked, because c1 hasn't woken up yet. |
| 1223 |
self.assertTrue(sem.locked()) |
| 1224 |
await sem.acquire() |
| 1225 |
result.append(2) |
| 1226 |
return True |
| 1227 |
|
| 1228 |
async def c3(result): |
| 1229 |
await sem.acquire() |
| 1230 |
self.assertTrue(sem.locked()) |
| 1231 |
result.append(3) |
| 1232 |
return True |
| 1233 |
|
| 1234 |
async def c4(result): |
| 1235 |
result.append(4) |
| 1236 |
return True |
| 1237 |
|
| 1238 |
t1 = asyncio.create_task(c1(result)) |
| 1239 |
t2 = asyncio.create_task(c2(result)) |
| 1240 |
t3 = asyncio.create_task(c3(result)) |
| 1241 |
t4 = None |
| 1242 |
|
| 1243 |
await asyncio.sleep(0) |
| 1244 |
# Three tasks are in the queue, the first hasn't woken up yet. |
| 1245 |
self.assertEqual(sem._value, 2) |
| 1246 |
self.assertEqual(len(sem._waiters), 3) |
| 1247 |
await asyncio.sleep(0) |
| 1248 |
|
| 1249 |
tasks = [t1, t2, t3, t4] |
| 1250 |
await asyncio.gather(*tasks) |
| 1251 |
self.assertEqual([1, 2, 3, 4], result) |
| 1252 |
|
| 1253 |
class BarrierTests(unittest.IsolatedAsyncioTestCase): |
| 1254 |
|
| 1255 |
async def asyncSetUp(self): |
| 1256 |
await super().asyncSetUp() |
| 1257 |
self.N = 5 |
| 1258 |
|
| 1259 |
def make_tasks(self, n, coro): |
| 1260 |
tasks = [asyncio.create_task(coro()) for _ in range(n)] |
| 1261 |
return tasks |
| 1262 |
|
| 1263 |
async def gather_tasks(self, n, coro): |
| 1264 |
tasks = self.make_tasks(n, coro) |
| 1265 |
res = await asyncio.gather(*tasks) |
| 1266 |
return res, tasks |
| 1267 |
|
| 1268 |
async def test_barrier(self): |
| 1269 |
barrier = asyncio.Barrier(self.N) |
| 1270 |
self.assertIn("filling", repr(barrier)) |
| 1271 |
with self.assertRaisesRegex( |
| 1272 |
TypeError, |
| 1273 |
"'Barrier' object can't be awaited", |
| 1274 |
): |
| 1275 |
await barrier |
| 1276 |
|
| 1277 |
self.assertIn("filling", repr(barrier)) |
| 1278 |
|
| 1279 |
async def test_repr(self): |
| 1280 |
barrier = asyncio.Barrier(self.N) |
| 1281 |
|
| 1282 |
self.assertTrue(RGX_REPR.match(repr(barrier))) |
| 1283 |
self.assertIn("filling", repr(barrier)) |
| 1284 |
|
| 1285 |
waiters = [] |
| 1286 |
async def wait(barrier): |
| 1287 |
await barrier.wait() |
| 1288 |
|
| 1289 |
incr = 2 |
| 1290 |
for i in range(incr): |
| 1291 |
waiters.append(asyncio.create_task(wait(barrier))) |
| 1292 |
await asyncio.sleep(0) |
| 1293 |
|
| 1294 |
self.assertTrue(RGX_REPR.match(repr(barrier))) |
| 1295 |
self.assertTrue(f"waiters:{incr}/{self.N}" in repr(barrier)) |
| 1296 |
self.assertIn("filling", repr(barrier)) |
| 1297 |
|
| 1298 |
# create missing waiters |
| 1299 |
for i in range(barrier.parties - barrier.n_waiting): |
| 1300 |
waiters.append(asyncio.create_task(wait(barrier))) |
| 1301 |
await asyncio.sleep(0) |
| 1302 |
|
| 1303 |
self.assertTrue(RGX_REPR.match(repr(barrier))) |
| 1304 |
self.assertIn("draining", repr(barrier)) |
| 1305 |
|
| 1306 |
# add a part of waiters |
| 1307 |
for i in range(incr): |
| 1308 |
waiters.append(asyncio.create_task(wait(barrier))) |
| 1309 |
await asyncio.sleep(0) |
| 1310 |
# and reset |
| 1311 |
await barrier.reset() |
| 1312 |
|
| 1313 |
self.assertTrue(RGX_REPR.match(repr(barrier))) |
| 1314 |
self.assertIn("resetting", repr(barrier)) |
| 1315 |
|
| 1316 |
# add a part of waiters again |
| 1317 |
for i in range(incr): |
| 1318 |
waiters.append(asyncio.create_task(wait(barrier))) |
| 1319 |
await asyncio.sleep(0) |
| 1320 |
# and abort |
| 1321 |
await barrier.abort() |
| 1322 |
|
| 1323 |
self.assertTrue(RGX_REPR.match(repr(barrier))) |
| 1324 |
self.assertIn("broken", repr(barrier)) |
| 1325 |
self.assertTrue(barrier.broken) |
| 1326 |
|
| 1327 |
# suppress unhandled exceptions |
| 1328 |
await asyncio.gather(*waiters, return_exceptions=True) |
| 1329 |
|
| 1330 |
async def test_barrier_parties(self): |
| 1331 |
self.assertRaises(ValueError, lambda: asyncio.Barrier(0)) |
| 1332 |
self.assertRaises(ValueError, lambda: asyncio.Barrier(-4)) |
| 1333 |
|
| 1334 |
self.assertIsInstance(asyncio.Barrier(self.N), asyncio.Barrier) |
| 1335 |
|
| 1336 |
async def test_context_manager(self): |
| 1337 |
self.N = 3 |
| 1338 |
barrier = asyncio.Barrier(self.N) |
| 1339 |
results = [] |
| 1340 |
|
| 1341 |
async def coro(): |
| 1342 |
async with barrier as i: |
| 1343 |
results.append(i) |
| 1344 |
|
| 1345 |
await self.gather_tasks(self.N, coro) |
| 1346 |
|
| 1347 |
self.assertListEqual(sorted(results), list(range(self.N))) |
| 1348 |
self.assertEqual(barrier.n_waiting, 0) |
| 1349 |
self.assertFalse(barrier.broken) |
| 1350 |
|
| 1351 |
async def test_filling_one_task(self): |
| 1352 |
barrier = asyncio.Barrier(1) |
| 1353 |
|
| 1354 |
async def f(): |
| 1355 |
async with barrier as i: |
| 1356 |
return True |
| 1357 |
|
| 1358 |
ret = await f() |
| 1359 |
|
| 1360 |
self.assertTrue(ret) |
| 1361 |
self.assertEqual(barrier.n_waiting, 0) |
| 1362 |
self.assertFalse(barrier.broken) |
| 1363 |
|
| 1364 |
async def test_filling_one_task_twice(self): |
| 1365 |
barrier = asyncio.Barrier(1) |
| 1366 |
|
| 1367 |
t1 = asyncio.create_task(barrier.wait()) |
| 1368 |
await asyncio.sleep(0) |
| 1369 |
self.assertEqual(barrier.n_waiting, 0) |
| 1370 |
|
| 1371 |
t2 = asyncio.create_task(barrier.wait()) |
| 1372 |
await asyncio.sleep(0) |
| 1373 |
|
| 1374 |
self.assertEqual(t1.result(), t2.result()) |
| 1375 |
self.assertEqual(t1.done(), t2.done()) |
| 1376 |
|
| 1377 |
self.assertEqual(barrier.n_waiting, 0) |
| 1378 |
self.assertFalse(barrier.broken) |
| 1379 |
|
| 1380 |
async def test_filling_task_by_task(self): |
| 1381 |
self.N = 3 |
| 1382 |
barrier = asyncio.Barrier(self.N) |
| 1383 |
|
| 1384 |
t1 = asyncio.create_task(barrier.wait()) |
| 1385 |
await asyncio.sleep(0) |
| 1386 |
self.assertEqual(barrier.n_waiting, 1) |
| 1387 |
self.assertIn("filling", repr(barrier)) |
| 1388 |
|
| 1389 |
t2 = asyncio.create_task(barrier.wait()) |
| 1390 |
await asyncio.sleep(0) |
| 1391 |
self.assertEqual(barrier.n_waiting, 2) |
| 1392 |
self.assertIn("filling", repr(barrier)) |
| 1393 |
|
| 1394 |
t3 = asyncio.create_task(barrier.wait()) |
| 1395 |
await asyncio.sleep(0) |
| 1396 |
|
| 1397 |
await asyncio.wait([t1, t2, t3]) |
| 1398 |
|
| 1399 |
self.assertEqual(barrier.n_waiting, 0) |
| 1400 |
self.assertFalse(barrier.broken) |
| 1401 |
|
| 1402 |
async def test_filling_tasks_wait_twice(self): |
| 1403 |
barrier = asyncio.Barrier(self.N) |
| 1404 |
results = [] |
| 1405 |
|
| 1406 |
async def coro(): |
| 1407 |
async with barrier: |
| 1408 |
results.append(True) |
| 1409 |
|
| 1410 |
async with barrier: |
| 1411 |
results.append(False) |
| 1412 |
|
| 1413 |
await self.gather_tasks(self.N, coro) |
| 1414 |
|
| 1415 |
self.assertEqual(len(results), self.N*2) |
| 1416 |
self.assertEqual(results.count(True), self.N) |
| 1417 |
self.assertEqual(results.count(False), self.N) |
| 1418 |
|
| 1419 |
self.assertEqual(barrier.n_waiting, 0) |
| 1420 |
self.assertFalse(barrier.broken) |
| 1421 |
|
| 1422 |
async def test_filling_tasks_check_return_value(self): |
| 1423 |
barrier = asyncio.Barrier(self.N) |
| 1424 |
results1 = [] |
| 1425 |
results2 = [] |
| 1426 |
|
| 1427 |
async def coro(): |
| 1428 |
async with barrier: |
| 1429 |
results1.append(True) |
| 1430 |
|
| 1431 |
async with barrier as i: |
| 1432 |
results2.append(True) |
| 1433 |
return i |
| 1434 |
|
| 1435 |
res, _ = await self.gather_tasks(self.N, coro) |
| 1436 |
|
| 1437 |
self.assertEqual(len(results1), self.N) |
| 1438 |
self.assertTrue(all(results1)) |
| 1439 |
self.assertEqual(len(results2), self.N) |
| 1440 |
self.assertTrue(all(results2)) |
| 1441 |
self.assertListEqual(sorted(res), list(range(self.N))) |
| 1442 |
|
| 1443 |
self.assertEqual(barrier.n_waiting, 0) |
| 1444 |
self.assertFalse(barrier.broken) |
| 1445 |
|
| 1446 |
async def test_draining_state(self): |
| 1447 |
barrier = asyncio.Barrier(self.N) |
| 1448 |
results = [] |
| 1449 |
|
| 1450 |
async def coro(): |
| 1451 |
async with barrier: |
| 1452 |
# barrier state change to filling for the last task release |
| 1453 |
results.append("draining" in repr(barrier)) |
| 1454 |
|
| 1455 |
await self.gather_tasks(self.N, coro) |
| 1456 |
|
| 1457 |
self.assertEqual(len(results), self.N) |
| 1458 |
self.assertEqual(results[-1], False) |
| 1459 |
self.assertTrue(all(results[:self.N-1])) |
| 1460 |
|
| 1461 |
self.assertEqual(barrier.n_waiting, 0) |
| 1462 |
self.assertFalse(barrier.broken) |
| 1463 |
|
| 1464 |
async def test_blocking_tasks_while_draining(self): |
| 1465 |
rewait = 2 |
| 1466 |
barrier = asyncio.Barrier(self.N) |
| 1467 |
barrier_nowaiting = asyncio.Barrier(self.N - rewait) |
| 1468 |
results = [] |
| 1469 |
rewait_n = rewait |
| 1470 |
counter = 0 |
| 1471 |
|
| 1472 |
async def coro(): |
| 1473 |
nonlocal rewait_n |
| 1474 |
|
| 1475 |
# first time waiting |
| 1476 |
await barrier.wait() |
| 1477 |
|
| 1478 |
# after waiting once for all tasks |
| 1479 |
if rewait_n > 0: |
| 1480 |
rewait_n -= 1 |
| 1481 |
# wait again only for rewait tasks |
| 1482 |
await barrier.wait() |
| 1483 |
else: |
| 1484 |
# wait for end of draining state |
| 1485 |
await barrier_nowaiting.wait() |
| 1486 |
# wait for other waiting tasks |
| 1487 |
await barrier.wait() |
| 1488 |
|
| 1489 |
# a success means that barrier_nowaiting |
| 1490 |
# was waited for exactly N-rewait=3 times |
| 1491 |
await self.gather_tasks(self.N, coro) |
| 1492 |
|
| 1493 |
async def test_filling_tasks_cancel_one(self): |
| 1494 |
self.N = 3 |
| 1495 |
barrier = asyncio.Barrier(self.N) |
| 1496 |
results = [] |
| 1497 |
|
| 1498 |
async def coro(): |
| 1499 |
await barrier.wait() |
| 1500 |
results.append(True) |
| 1501 |
|
| 1502 |
t1 = asyncio.create_task(coro()) |
| 1503 |
await asyncio.sleep(0) |
| 1504 |
self.assertEqual(barrier.n_waiting, 1) |
| 1505 |
|
| 1506 |
t2 = asyncio.create_task(coro()) |
| 1507 |
await asyncio.sleep(0) |
| 1508 |
self.assertEqual(barrier.n_waiting, 2) |
| 1509 |
|
| 1510 |
t1.cancel() |
| 1511 |
await asyncio.sleep(0) |
| 1512 |
self.assertEqual(barrier.n_waiting, 1) |
| 1513 |
with self.assertRaises(asyncio.CancelledError): |
| 1514 |
await t1 |
| 1515 |
self.assertTrue(t1.cancelled()) |
| 1516 |
|
| 1517 |
t3 = asyncio.create_task(coro()) |
| 1518 |
await asyncio.sleep(0) |
| 1519 |
self.assertEqual(barrier.n_waiting, 2) |
| 1520 |
|
| 1521 |
t4 = asyncio.create_task(coro()) |
| 1522 |
await asyncio.gather(t2, t3, t4) |
| 1523 |
|
| 1524 |
self.assertEqual(len(results), self.N) |
| 1525 |
self.assertTrue(all(results)) |
| 1526 |
|
| 1527 |
self.assertEqual(barrier.n_waiting, 0) |
| 1528 |
self.assertFalse(barrier.broken) |
| 1529 |
|
| 1530 |
async def test_reset_barrier(self): |
| 1531 |
barrier = asyncio.Barrier(1) |
| 1532 |
|
| 1533 |
asyncio.create_task(barrier.reset()) |
| 1534 |
await asyncio.sleep(0) |
| 1535 |
|
| 1536 |
self.assertEqual(barrier.n_waiting, 0) |
| 1537 |
self.assertFalse(barrier.broken) |
| 1538 |
|
| 1539 |
async def test_reset_barrier_while_tasks_waiting(self): |
| 1540 |
barrier = asyncio.Barrier(self.N) |
| 1541 |
results = [] |
| 1542 |
|
| 1543 |
async def coro(): |
| 1544 |
try: |
| 1545 |
await barrier.wait() |
| 1546 |
except asyncio.BrokenBarrierError: |
| 1547 |
results.append(True) |
| 1548 |
|
| 1549 |
async def coro_reset(): |
| 1550 |
await barrier.reset() |
| 1551 |
|
| 1552 |
# N-1 tasks waiting on barrier with N parties |
| 1553 |
tasks = self.make_tasks(self.N-1, coro) |
| 1554 |
await asyncio.sleep(0) |
| 1555 |
|
| 1556 |
# reset the barrier |
| 1557 |
asyncio.create_task(coro_reset()) |
| 1558 |
await asyncio.gather(*tasks) |
| 1559 |
|
| 1560 |
self.assertEqual(len(results), self.N-1) |
| 1561 |
self.assertTrue(all(results)) |
| 1562 |
self.assertEqual(barrier.n_waiting, 0) |
| 1563 |
self.assertNotIn("resetting", repr(barrier)) |
| 1564 |
self.assertFalse(barrier.broken) |
| 1565 |
|
| 1566 |
async def test_reset_barrier_when_tasks_half_draining(self): |
| 1567 |
barrier = asyncio.Barrier(self.N) |
| 1568 |
results1 = [] |
| 1569 |
rest_of_tasks = self.N//2 |
| 1570 |
|
| 1571 |
async def coro(): |
| 1572 |
try: |
| 1573 |
await barrier.wait() |
| 1574 |
except asyncio.BrokenBarrierError: |
| 1575 |
# catch here waiting tasks |
| 1576 |
results1.append(True) |
| 1577 |
else: |
| 1578 |
# here drained task outside the barrier |
| 1579 |
if rest_of_tasks == barrier._count: |
| 1580 |
# tasks outside the barrier |
| 1581 |
await barrier.reset() |
| 1582 |
|
| 1583 |
await self.gather_tasks(self.N, coro) |
| 1584 |
|
| 1585 |
self.assertEqual(results1, [True]*rest_of_tasks) |
| 1586 |
self.assertEqual(barrier.n_waiting, 0) |
| 1587 |
self.assertNotIn("resetting", repr(barrier)) |
| 1588 |
self.assertFalse(barrier.broken) |
| 1589 |
|
| 1590 |
async def test_reset_barrier_when_tasks_half_draining_half_blocking(self): |
| 1591 |
barrier = asyncio.Barrier(self.N) |
| 1592 |
results1 = [] |
| 1593 |
results2 = [] |
| 1594 |
blocking_tasks = self.N//2 |
| 1595 |
count = 0 |
| 1596 |
|
| 1597 |
async def coro(): |
| 1598 |
nonlocal count |
| 1599 |
try: |
| 1600 |
await barrier.wait() |
| 1601 |
except asyncio.BrokenBarrierError: |
| 1602 |
# here catch still waiting tasks |
| 1603 |
results1.append(True) |
| 1604 |
|
| 1605 |
# so now waiting again to reach nb_parties |
| 1606 |
await barrier.wait() |
| 1607 |
else: |
| 1608 |
count += 1 |
| 1609 |
if count > blocking_tasks: |
| 1610 |
# reset now: raise asyncio.BrokenBarrierError for waiting tasks |
| 1611 |
await barrier.reset() |
| 1612 |
|
| 1613 |
# so now waiting again to reach nb_parties |
| 1614 |
await barrier.wait() |
| 1615 |
else: |
| 1616 |
try: |
| 1617 |
await barrier.wait() |
| 1618 |
except asyncio.BrokenBarrierError: |
| 1619 |
# here no catch - blocked tasks go to wait |
| 1620 |
results2.append(True) |
| 1621 |
|
| 1622 |
await self.gather_tasks(self.N, coro) |
| 1623 |
|
| 1624 |
self.assertEqual(results1, [True]*blocking_tasks) |
| 1625 |
self.assertEqual(results2, []) |
| 1626 |
self.assertEqual(barrier.n_waiting, 0) |
| 1627 |
self.assertNotIn("resetting", repr(barrier)) |
| 1628 |
self.assertFalse(barrier.broken) |
| 1629 |
|
| 1630 |
async def test_reset_barrier_while_tasks_waiting_and_waiting_again(self): |
| 1631 |
barrier = asyncio.Barrier(self.N) |
| 1632 |
results1 = [] |
| 1633 |
results2 = [] |
| 1634 |
|
| 1635 |
async def coro1(): |
| 1636 |
try: |
| 1637 |
await barrier.wait() |
| 1638 |
except asyncio.BrokenBarrierError: |
| 1639 |
results1.append(True) |
| 1640 |
finally: |
| 1641 |
await barrier.wait() |
| 1642 |
results2.append(True) |
| 1643 |
|
| 1644 |
async def coro2(): |
| 1645 |
async with barrier: |
| 1646 |
results2.append(True) |
| 1647 |
|
| 1648 |
tasks = self.make_tasks(self.N-1, coro1) |
| 1649 |
|
| 1650 |
# reset barrier, N-1 waiting tasks raise an BrokenBarrierError |
| 1651 |
asyncio.create_task(barrier.reset()) |
| 1652 |
await asyncio.sleep(0) |
| 1653 |
|
| 1654 |
# complete waiting tasks in the `finally` |
| 1655 |
asyncio.create_task(coro2()) |
| 1656 |
|
| 1657 |
await asyncio.gather(*tasks) |
| 1658 |
|
| 1659 |
self.assertFalse(barrier.broken) |
| 1660 |
self.assertEqual(len(results1), self.N-1) |
| 1661 |
self.assertTrue(all(results1)) |
| 1662 |
self.assertEqual(len(results2), self.N) |
| 1663 |
self.assertTrue(all(results2)) |
| 1664 |
|
| 1665 |
self.assertEqual(barrier.n_waiting, 0) |
| 1666 |
|
| 1667 |
|
| 1668 |
async def test_reset_barrier_while_tasks_draining(self): |
| 1669 |
barrier = asyncio.Barrier(self.N) |
| 1670 |
results1 = [] |
| 1671 |
results2 = [] |
| 1672 |
results3 = [] |
| 1673 |
count = 0 |
| 1674 |
|
| 1675 |
async def coro(): |
| 1676 |
nonlocal count |
| 1677 |
|
| 1678 |
i = await barrier.wait() |
| 1679 |
count += 1 |
| 1680 |
if count == self.N: |
| 1681 |
# last task exited from barrier |
| 1682 |
await barrier.reset() |
| 1683 |
|
| 1684 |
# wait here to reach the `parties` |
| 1685 |
await barrier.wait() |
| 1686 |
else: |
| 1687 |
try: |
| 1688 |
# second waiting |
| 1689 |
await barrier.wait() |
| 1690 |
|
| 1691 |
# N-1 tasks here |
| 1692 |
results1.append(True) |
| 1693 |
except Exception as e: |
| 1694 |
# never goes here |
| 1695 |
results2.append(True) |
| 1696 |
|
| 1697 |
# Now, pass the barrier again |
| 1698 |
# last wait, must be completed |
| 1699 |
k = await barrier.wait() |
| 1700 |
results3.append(True) |
| 1701 |
|
| 1702 |
await self.gather_tasks(self.N, coro) |
| 1703 |
|
| 1704 |
self.assertFalse(barrier.broken) |
| 1705 |
self.assertTrue(all(results1)) |
| 1706 |
self.assertEqual(len(results1), self.N-1) |
| 1707 |
self.assertEqual(len(results2), 0) |
| 1708 |
self.assertEqual(len(results3), self.N) |
| 1709 |
self.assertTrue(all(results3)) |
| 1710 |
|
| 1711 |
self.assertEqual(barrier.n_waiting, 0) |
| 1712 |
|
| 1713 |
async def test_abort_barrier(self): |
| 1714 |
barrier = asyncio.Barrier(1) |
| 1715 |
|
| 1716 |
asyncio.create_task(barrier.abort()) |
| 1717 |
await asyncio.sleep(0) |
| 1718 |
|
| 1719 |
self.assertEqual(barrier.n_waiting, 0) |
| 1720 |
self.assertTrue(barrier.broken) |
| 1721 |
|
| 1722 |
async def test_abort_barrier_when_tasks_half_draining_half_blocking(self): |
| 1723 |
barrier = asyncio.Barrier(self.N) |
| 1724 |
results1 = [] |
| 1725 |
results2 = [] |
| 1726 |
blocking_tasks = self.N//2 |
| 1727 |
count = 0 |
| 1728 |
|
| 1729 |
async def coro(): |
| 1730 |
nonlocal count |
| 1731 |
try: |
| 1732 |
await barrier.wait() |
| 1733 |
except asyncio.BrokenBarrierError: |
| 1734 |
# here catch tasks waiting to drain |
| 1735 |
results1.append(True) |
| 1736 |
else: |
| 1737 |
count += 1 |
| 1738 |
if count > blocking_tasks: |
| 1739 |
# abort now: raise asyncio.BrokenBarrierError for all tasks |
| 1740 |
await barrier.abort() |
| 1741 |
else: |
| 1742 |
try: |
| 1743 |
await barrier.wait() |
| 1744 |
except asyncio.BrokenBarrierError: |
| 1745 |
# here catch blocked tasks (already drained) |
| 1746 |
results2.append(True) |
| 1747 |
|
| 1748 |
await self.gather_tasks(self.N, coro) |
| 1749 |
|
| 1750 |
self.assertTrue(barrier.broken) |
| 1751 |
self.assertEqual(results1, [True]*blocking_tasks) |
| 1752 |
self.assertEqual(results2, [True]*(self.N-blocking_tasks-1)) |
| 1753 |
self.assertEqual(barrier.n_waiting, 0) |
| 1754 |
self.assertNotIn("resetting", repr(barrier)) |
| 1755 |
|
| 1756 |
async def test_abort_barrier_when_exception(self): |
| 1757 |
# test from threading.Barrier: see `lock_tests.test_reset` |
| 1758 |
barrier = asyncio.Barrier(self.N) |
| 1759 |
results1 = [] |
| 1760 |
results2 = [] |
| 1761 |
|
| 1762 |
async def coro(): |
| 1763 |
try: |
| 1764 |
async with barrier as i : |
| 1765 |
if i == self.N//2: |
| 1766 |
raise RuntimeError |
| 1767 |
async with barrier: |
| 1768 |
results1.append(True) |
| 1769 |
except asyncio.BrokenBarrierError: |
| 1770 |
results2.append(True) |
| 1771 |
except RuntimeError: |
| 1772 |
await barrier.abort() |
| 1773 |
|
| 1774 |
await self.gather_tasks(self.N, coro) |
| 1775 |
|
| 1776 |
self.assertTrue(barrier.broken) |
| 1777 |
self.assertEqual(len(results1), 0) |
| 1778 |
self.assertEqual(len(results2), self.N-1) |
| 1779 |
self.assertTrue(all(results2)) |
| 1780 |
self.assertEqual(barrier.n_waiting, 0) |
| 1781 |
|
| 1782 |
async def test_abort_barrier_when_exception_then_resetting(self): |
| 1783 |
# test from threading.Barrier: see `lock_tests.test_abort_and_reset` |
| 1784 |
barrier1 = asyncio.Barrier(self.N) |
| 1785 |
barrier2 = asyncio.Barrier(self.N) |
| 1786 |
results1 = [] |
| 1787 |
results2 = [] |
| 1788 |
results3 = [] |
| 1789 |
|
| 1790 |
async def coro(): |
| 1791 |
try: |
| 1792 |
i = await barrier1.wait() |
| 1793 |
if i == self.N//2: |
| 1794 |
raise RuntimeError |
| 1795 |
await barrier1.wait() |
| 1796 |
results1.append(True) |
| 1797 |
except asyncio.BrokenBarrierError: |
| 1798 |
results2.append(True) |
| 1799 |
except RuntimeError: |
| 1800 |
await barrier1.abort() |
| 1801 |
|
| 1802 |
# Synchronize and reset the barrier. Must synchronize first so |
| 1803 |
# that everyone has left it when we reset, and after so that no |
| 1804 |
# one enters it before the reset. |
| 1805 |
i = await barrier2.wait() |
| 1806 |
if i == self.N//2: |
| 1807 |
await barrier1.reset() |
| 1808 |
await barrier2.wait() |
| 1809 |
await barrier1.wait() |
| 1810 |
results3.append(True) |
| 1811 |
|
| 1812 |
await self.gather_tasks(self.N, coro) |
| 1813 |
|
| 1814 |
self.assertFalse(barrier1.broken) |
| 1815 |
self.assertEqual(len(results1), 0) |
| 1816 |
self.assertEqual(len(results2), self.N-1) |
| 1817 |
self.assertTrue(all(results2)) |
| 1818 |
self.assertEqual(len(results3), self.N) |
| 1819 |
self.assertTrue(all(results3)) |
| 1820 |
|
| 1821 |
self.assertEqual(barrier1.n_waiting, 0) |
| 1822 |
|
| 1823 |
|
| 1824 |
if __name__ == '__main__': |
| 1825 |
unittest.main() |