1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
|
from __future__ import annotations
import asyncio
from typing import Any
import pytest
from anyio import (
CancelScope,
Condition,
Event,
Lock,
Semaphore,
WouldBlock,
create_task_group,
fail_after,
run,
to_thread,
wait_all_tasks_blocked,
)
from anyio.abc import CapacityLimiter, TaskStatus
from .conftest import asyncio_params
pytestmark = pytest.mark.anyio
class TestLock:
async def test_contextmanager(self) -> None:
async def task() -> None:
assert lock.locked()
async with lock:
results.append("2")
results = []
lock = Lock()
async with create_task_group() as tg:
async with lock:
tg.start_soon(task)
await wait_all_tasks_blocked()
results.append("1")
assert not lock.locked()
assert results == ["1", "2"]
async def test_manual_acquire(self) -> None:
async def task() -> None:
assert lock.locked()
await lock.acquire()
try:
results.append("2")
finally:
lock.release()
results = []
lock = Lock()
async with create_task_group() as tg:
await lock.acquire()
try:
tg.start_soon(task)
await wait_all_tasks_blocked()
results.append("1")
finally:
lock.release()
assert not lock.locked()
assert results == ["1", "2"]
async def test_fast_acquire(self) -> None:
"""
Test that fast_acquire=True does not yield back control to the event loop when
there is no contention.
"""
other_task_called = False
async def other_task() -> None:
nonlocal other_task_called
other_task_called = True
lock = Lock(fast_acquire=True)
async with create_task_group() as tg:
tg.start_soon(other_task)
async with lock:
assert not other_task_called
async def test_acquire_nowait(self) -> None:
lock = Lock()
lock.acquire_nowait()
assert lock.locked()
async def test_acquire_nowait_wouldblock(self) -> None:
async def try_lock() -> None:
pytest.raises(WouldBlock, lock.acquire_nowait)
lock = Lock()
async with lock, create_task_group() as tg:
assert lock.locked()
tg.start_soon(try_lock)
@pytest.mark.parametrize("fast_acquire", [True, False])
async def test_acquire_twice_async(self, fast_acquire: bool) -> None:
lock = Lock(fast_acquire=fast_acquire)
await lock.acquire()
with pytest.raises(
RuntimeError, match="Attempted to acquire an already held Lock"
):
await lock.acquire()
async def test_acquire_twice_sync(self) -> None:
lock = Lock()
lock.acquire_nowait()
with pytest.raises(
RuntimeError, match="Attempted to acquire an already held Lock"
):
lock.acquire_nowait()
@pytest.mark.parametrize(
"release_first",
[pytest.param(False, id="releaselast"), pytest.param(True, id="releasefirst")],
)
async def test_cancel_during_acquire(self, release_first: bool) -> None:
acquired = False
async def task(*, task_status: TaskStatus) -> None:
nonlocal acquired
task_status.started()
async with lock:
acquired = True
lock = Lock()
async with create_task_group() as tg:
await lock.acquire()
await tg.start(task)
tg.cancel_scope.cancel()
with CancelScope(shield=True):
if release_first:
lock.release()
await wait_all_tasks_blocked()
else:
await wait_all_tasks_blocked()
lock.release()
assert not acquired
assert not lock.locked()
async def test_statistics(self) -> None:
async def waiter() -> None:
async with lock:
pass
lock = Lock()
async with create_task_group() as tg:
assert not lock.statistics().locked
assert lock.statistics().tasks_waiting == 0
async with lock:
assert lock.statistics().locked
assert lock.statistics().tasks_waiting == 0
for i in range(1, 3):
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert lock.statistics().tasks_waiting == i
assert not lock.statistics().locked
assert lock.statistics().tasks_waiting == 0
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_asyncio_deadlock(self) -> None:
"""Regression test for #398."""
lock = Lock()
async def acquire() -> None:
async with lock:
await asyncio.sleep(0)
loop = asyncio.get_running_loop()
task1 = loop.create_task(acquire())
task2 = loop.create_task(acquire())
await asyncio.sleep(0)
task1.cancel()
await asyncio.wait_for(task2, 1)
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_cancel_after_release(self) -> None:
"""
Test that a native asyncio cancellation will not cause a lock ownership
to get lost between a release() and the resumption of acquire().
"""
# Create the lock and acquire it right away so that any task acquiring it will
# block
lock = Lock()
lock.acquire_nowait()
# Start a task that gets blocked on trying to acquire the semaphore
loop = asyncio.get_running_loop()
task1 = loop.create_task(lock.acquire(), name="task1")
await asyncio.sleep(0)
# Trigger the aqcuiring task to be rescheduled, but also cancel it right away
lock.release()
task1.cancel()
statistics = lock.statistics()
assert statistics.owner
assert statistics.owner.name == "task1"
await asyncio.wait([task1], timeout=1)
# The acquire() method should've released the semaphore because acquisition
# failed due to cancellation
statistics = lock.statistics()
assert statistics.owner is None
assert statistics.tasks_waiting == 0
lock.acquire_nowait()
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
async def use_lock() -> None:
async with lock:
pass
lock = Lock()
statistics = lock.statistics()
assert not statistics.locked
assert statistics.owner is None
assert statistics.tasks_waiting == 0
run(use_lock, backend=anyio_backend_name, backend_options=anyio_backend_options)
async def test_owner_after_release(self) -> None:
async def taskfunc1() -> None:
await lock.acquire()
owner = lock.statistics().owner
assert owner
assert owner.name == "task1"
await event.wait()
lock.release()
owner = lock.statistics().owner
assert owner
assert owner.name == "task2"
event = Event()
lock = Lock()
async with create_task_group() as tg:
tg.start_soon(taskfunc1, name="task1")
await wait_all_tasks_blocked()
tg.start_soon(lock.acquire, name="task2")
await wait_all_tasks_blocked()
event.set()
class TestEvent:
async def test_event(self) -> None:
async def setter() -> None:
assert not event.is_set()
event.set()
event = Event()
async with create_task_group() as tg:
tg.start_soon(setter)
await event.wait()
assert event.is_set()
async def test_event_cancel(self) -> None:
task_started = event_set = False
async def task() -> None:
nonlocal task_started, event_set
task_started = True
await event.wait()
event_set = True
event = Event()
async with create_task_group() as tg:
tg.start_soon(task)
tg.cancel_scope.cancel()
event.set()
assert task_started
assert not event_set
async def test_event_wait_before_set_before_cancel(self) -> None:
setter_started = waiter_woke = False
async def setter() -> None:
nonlocal setter_started
setter_started = True
assert not event.is_set()
event.set()
tg.cancel_scope.cancel()
event = Event()
async with create_task_group() as tg:
tg.start_soon(setter)
await event.wait()
waiter_woke = True
assert setter_started
assert waiter_woke
async def test_statistics(self) -> None:
async def waiter() -> None:
await event.wait()
event = Event()
async with create_task_group() as tg:
assert event.statistics().tasks_waiting == 0
for i in range(1, 3):
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert event.statistics().tasks_waiting == i
event.set()
assert event.statistics().tasks_waiting == 0
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
event = Event()
assert not event.is_set()
assert event.statistics().tasks_waiting == 0
event.set()
assert event.is_set()
run(
event.wait,
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)
class TestCondition:
async def test_contextmanager(self) -> None:
async def notifier() -> None:
async with condition:
condition.notify_all()
condition = Condition()
async with create_task_group() as tg:
async with condition:
assert condition.locked()
tg.start_soon(notifier)
await condition.wait()
async def test_manual_acquire(self) -> None:
async def notifier() -> None:
await condition.acquire()
try:
condition.notify_all()
finally:
condition.release()
condition = Condition()
async with create_task_group() as tg:
await condition.acquire()
try:
assert condition.locked()
tg.start_soon(notifier)
await condition.wait()
finally:
condition.release()
async def test_acquire_nowait(self) -> None:
condition = Condition()
condition.acquire_nowait()
assert condition.locked()
async def test_acquire_nowait_wouldblock(self) -> None:
async def try_lock() -> None:
pytest.raises(WouldBlock, condition.acquire_nowait)
condition = Condition()
async with condition, create_task_group() as tg:
assert condition.locked()
tg.start_soon(try_lock)
async def test_wait_cancel(self) -> None:
task_started = notified = False
async def task() -> None:
nonlocal task_started, notified
task_started = True
async with condition:
event.set()
await condition.wait()
notified = True
event = Event()
condition = Condition()
async with create_task_group() as tg:
tg.start_soon(task)
await event.wait()
await wait_all_tasks_blocked()
tg.cancel_scope.cancel()
assert task_started
assert not notified
async def test_statistics(self) -> None:
async def waiter() -> None:
async with condition:
await condition.wait()
condition = Condition()
async with create_task_group() as tg:
assert not condition.statistics().lock_statistics.locked
assert condition.statistics().tasks_waiting == 0
async with condition:
assert condition.statistics().lock_statistics.locked
assert condition.statistics().tasks_waiting == 0
for i in range(1, 3):
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert condition.statistics().tasks_waiting == i
for i in range(1, -1, -1):
async with condition:
condition.notify(1)
await wait_all_tasks_blocked()
assert condition.statistics().tasks_waiting == i
assert not condition.statistics().lock_statistics.locked
assert condition.statistics().tasks_waiting == 0
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
async def use_condition() -> None:
async with condition:
pass
condition = Condition()
assert condition.statistics().tasks_waiting == 0
run(
use_condition,
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)
class TestSemaphore:
async def test_contextmanager(self) -> None:
async def acquire() -> None:
async with semaphore:
assert semaphore.value in (0, 1)
semaphore = Semaphore(2)
async with create_task_group() as tg:
tg.start_soon(acquire, name="task 1")
tg.start_soon(acquire, name="task 2")
assert semaphore.value == 2
async def test_manual_acquire(self) -> None:
async def acquire() -> None:
await semaphore.acquire()
try:
assert semaphore.value in (0, 1)
finally:
semaphore.release()
semaphore = Semaphore(2)
async with create_task_group() as tg:
tg.start_soon(acquire, name="task 1")
tg.start_soon(acquire, name="task 2")
assert semaphore.value == 2
async def test_fast_acquire(self) -> None:
"""
Test that fast_acquire=True does not yield back control to the event loop when
there is no contention.
"""
other_task_called = False
async def other_task() -> None:
nonlocal other_task_called
other_task_called = True
semaphore = Semaphore(1, fast_acquire=True)
async with create_task_group() as tg:
tg.start_soon(other_task)
async with semaphore:
assert not other_task_called
async def test_acquire_nowait(self) -> None:
semaphore = Semaphore(1)
semaphore.acquire_nowait()
assert semaphore.value == 0
pytest.raises(WouldBlock, semaphore.acquire_nowait)
@pytest.mark.parametrize(
"release_first",
[pytest.param(False, id="releaselast"), pytest.param(True, id="releasefirst")],
)
async def test_cancel_during_acquire(self, release_first: bool) -> None:
acquired = False
async def task(*, task_status: TaskStatus) -> None:
nonlocal acquired
task_status.started()
async with semaphore:
acquired = True
semaphore = Semaphore(1)
async with create_task_group() as tg:
await semaphore.acquire()
await tg.start(task)
tg.cancel_scope.cancel()
with CancelScope(shield=True):
if release_first:
semaphore.release()
await wait_all_tasks_blocked()
else:
await wait_all_tasks_blocked()
semaphore.release()
assert not acquired
assert semaphore.value == 1
@pytest.mark.parametrize("max_value", [2, None])
async def test_max_value(self, max_value: int | None) -> None:
semaphore = Semaphore(0, max_value=max_value)
assert semaphore.max_value == max_value
async def test_max_value_exceeded(self) -> None:
semaphore = Semaphore(1, max_value=2)
semaphore.release()
pytest.raises(ValueError, semaphore.release)
async def test_statistics(self) -> None:
async def waiter() -> None:
async with semaphore:
pass
semaphore = Semaphore(1)
async with create_task_group() as tg:
assert semaphore.statistics().tasks_waiting == 0
async with semaphore:
assert semaphore.statistics().tasks_waiting == 0
for i in range(1, 3):
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert semaphore.statistics().tasks_waiting == i
assert semaphore.statistics().tasks_waiting == 0
async def test_acquire_race(self) -> None:
"""
Test against a race condition: when a task waiting on acquire() is rescheduled
but another task snatches the last available slot, the task should not raise
WouldBlock.
"""
semaphore = Semaphore(1)
async with create_task_group() as tg:
semaphore.acquire_nowait()
tg.start_soon(semaphore.acquire)
await wait_all_tasks_blocked()
semaphore.release()
pytest.raises(WouldBlock, semaphore.acquire_nowait)
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_asyncio_deadlock(self) -> None:
"""Regression test for #398."""
semaphore = Semaphore(1)
async def acquire() -> None:
async with semaphore:
await asyncio.sleep(0)
loop = asyncio.get_running_loop()
task1 = loop.create_task(acquire())
task2 = loop.create_task(acquire())
await asyncio.sleep(0)
task1.cancel()
await asyncio.wait_for(task2, 1)
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_cancel_after_release(self) -> None:
"""
Test that a native asyncio cancellation will not cause a semaphore ownership
to get lost between a release() and the resumption of acquire().
"""
# Create the semaphore in such a way that any task acquiring it will block
semaphore = Semaphore(0, max_value=1)
# Start a task that gets blocked on trying to acquire the semaphore
loop = asyncio.get_running_loop()
task1 = loop.create_task(semaphore.acquire())
await asyncio.sleep(0)
# Trigger the aqcuiring task to be rescheduled, but also cancel it right away
semaphore.release()
task1.cancel()
assert semaphore.value == 0
await asyncio.wait([task1], timeout=1)
# The acquire() method should've released the semaphore because acquisition
# failed due to cancellation
assert semaphore.value == 1
assert semaphore.statistics().tasks_waiting == 0
semaphore.acquire_nowait()
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
async def use_semaphore() -> None:
async with semaphore:
pass
semaphore = Semaphore(1, max_value=3)
assert semaphore.value == 1
assert semaphore.max_value == 3
assert semaphore.statistics().tasks_waiting == 0
run(
use_semaphore,
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)
class TestCapacityLimiter:
async def test_bad_init_type(self) -> None:
pytest.raises(TypeError, CapacityLimiter, 1.0).match(
"total_tokens must be an int or math.inf"
)
async def test_bad_init_value(self) -> None:
pytest.raises(ValueError, CapacityLimiter, 0).match("total_tokens must be >= 1")
async def test_borrow(self) -> None:
limiter = CapacityLimiter(2)
assert limiter.total_tokens == 2
assert limiter.available_tokens == 2
assert limiter.borrowed_tokens == 0
async with limiter:
assert limiter.total_tokens == 2
assert limiter.available_tokens == 1
assert limiter.borrowed_tokens == 1
async def test_limit(self) -> None:
value = 0
async def taskfunc() -> None:
nonlocal value
for _ in range(5):
async with limiter:
assert value == 0
value = 1
await wait_all_tasks_blocked()
value = 0
limiter = CapacityLimiter(1)
async with create_task_group() as tg:
for _ in range(3):
tg.start_soon(taskfunc)
async def test_borrow_twice(self) -> None:
limiter = CapacityLimiter(1)
await limiter.acquire()
with pytest.raises(RuntimeError) as exc:
await limiter.acquire()
exc.match(
"this borrower is already holding one of this CapacityLimiter's tokens"
)
async def test_bad_release(self) -> None:
limiter = CapacityLimiter(1)
with pytest.raises(RuntimeError) as exc:
limiter.release()
exc.match("this borrower isn't holding any of this CapacityLimiter's tokens")
async def test_increase_tokens(self) -> None:
async def setter() -> None:
# Wait until waiter() is inside the limiter block
await event1.wait()
async with limiter:
# This can only happen when total_tokens has been increased
event2.set()
async def waiter() -> None:
async with limiter:
event1.set()
await event2.wait()
limiter = CapacityLimiter(1)
event1, event2 = Event(), Event()
async with create_task_group() as tg:
tg.start_soon(setter)
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert event1.is_set()
assert not event2.is_set()
limiter.total_tokens = 2
assert event2.is_set()
async def test_current_default_thread_limiter(self) -> None:
limiter = to_thread.current_default_thread_limiter()
assert isinstance(limiter, CapacityLimiter)
assert limiter.total_tokens == 40
async def test_statistics(self) -> None:
async def waiter() -> None:
async with limiter:
pass
limiter = CapacityLimiter(1)
assert limiter.statistics().total_tokens == 1
assert limiter.statistics().borrowed_tokens == 0
assert limiter.statistics().tasks_waiting == 0
async with create_task_group() as tg:
async with limiter:
assert limiter.statistics().borrowed_tokens == 1
assert limiter.statistics().tasks_waiting == 0
for i in range(1, 3):
tg.start_soon(waiter)
await wait_all_tasks_blocked()
assert limiter.statistics().tasks_waiting == i
assert limiter.statistics().tasks_waiting == 0
assert limiter.statistics().borrowed_tokens == 0
@pytest.mark.parametrize("anyio_backend", asyncio_params)
async def test_asyncio_deadlock(self) -> None:
"""Regression test for #398."""
limiter = CapacityLimiter(1)
async def acquire() -> None:
async with limiter:
await asyncio.sleep(0)
loop = asyncio.get_running_loop()
task1 = loop.create_task(acquire())
task2 = loop.create_task(acquire())
await asyncio.sleep(0)
task1.cancel()
await asyncio.wait_for(task2, 1)
async def test_ordered_queue(self) -> None:
limiter = CapacityLimiter(1)
results = []
event = Event()
async def append(x: int, task_status: TaskStatus) -> None:
task_status.started()
async with limiter:
await event.wait()
results.append(x)
async with create_task_group() as tg:
for i in [0, 1, 2]:
await tg.start(append, i)
event.set()
assert results == [0, 1, 2]
async def test_increase_tokens_lets_others_acquire(self) -> None:
limiter = CapacityLimiter(1)
entered_events = [Event() for _ in range(3)]
continue_event = Event()
async def worker(entered_event: Event) -> None:
async with limiter:
entered_event.set()
await continue_event.wait()
async with create_task_group() as tg:
for event in entered_events[:2]:
tg.start_soon(worker, event)
# One task should be able to acquire the limiter while the other is left
# waiting
await wait_all_tasks_blocked()
assert sum(ev.is_set() for ev in entered_events) == 1
# Increase the total tokens and start another worker.
# All tasks should be able to acquire the limiter now.
limiter.total_tokens = 3
tg.start_soon(worker, entered_events[2])
with fail_after(1):
for ev in entered_events[1:]:
await ev.wait()
# Allow all tasks to exit
continue_event.set()
def test_instantiate_outside_event_loop(
self, anyio_backend_name: str, anyio_backend_options: dict[str, Any]
) -> None:
async def use_limiter() -> None:
async with limiter:
pass
limiter = CapacityLimiter(1)
limiter.total_tokens = 2
with pytest.raises(TypeError):
limiter.total_tokens = "2" # type: ignore[assignment]
with pytest.raises(TypeError):
limiter.total_tokens = 3.0
assert limiter.total_tokens == 2
assert limiter.borrowed_tokens == 0
statistics = limiter.statistics()
assert statistics.total_tokens == 2
assert statistics.borrowed_tokens == 0
assert statistics.borrowers == ()
assert statistics.tasks_waiting == 0
run(
use_limiter,
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)
async def test_total_tokens_as_kwarg(self) -> None:
# Regression test for #515
limiter = CapacityLimiter(total_tokens=1)
assert limiter.total_tokens == 1
|