diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ed24b7eb..fb3166c3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -51,7 +51,7 @@ jobs: - run: pip install -r requirements.txt - run: pip install -e .[${{ matrix.install-extras }}] if: ${{ matrix.install-extras }} - - run: pytest --color=yes --cov=kopf --cov-branch + - run: pytest --color=yes --timeout=2 --cov=kopf --cov-branch - name: Publish coverage to Coveralls.io if: success() @@ -94,7 +94,7 @@ jobs: - run: pip install -r requirements.txt - run: pip install -e .[${{ matrix.install-extras }}] if: ${{ matrix.install-extras }} - - run: pytest --color=yes --no-cov + - run: pytest --color=yes --timeout=2 --no-cov functional: strategy: @@ -114,7 +114,7 @@ jobs: version: ${{ matrix.k3s }} github-token: ${{ secrets.GITHUB_TOKEN }} - run: pip install -r requirements.txt -r examples/requirements.txt - - run: pytest --color=yes --only-e2e + - run: pytest --color=yes --timeout=30 --only-e2e coveralls-finish: name: Finalize coveralls.io diff --git a/.github/workflows/thorough.yaml b/.github/workflows/thorough.yaml index 363c1adc..1091cecc 100644 --- a/.github/workflows/thorough.yaml +++ b/.github/workflows/thorough.yaml @@ -55,7 +55,7 @@ jobs: - run: pip install -r requirements.txt - run: pip install -e .[${{ matrix.install-extras }}] if: ${{ matrix.install-extras }} - - run: pytest --color=yes --cov=kopf --cov-branch + - run: pytest --color=yes --timeout=2 --cov=kopf --cov-branch - name: Publish coverage to Coveralls.io if: success() @@ -98,7 +98,7 @@ jobs: - run: pip install -r requirements.txt - run: pip install -e .[${{ matrix.install-extras }}] if: ${{ matrix.install-extras }} - - run: pytest --color=yes --no-cov + - run: pytest --color=yes --timeout=2 --no-cov functional: strategy: @@ -118,7 +118,7 @@ jobs: version: ${{ matrix.k3s }} github-token: ${{ secrets.GITHUB_TOKEN }} - run: pip install -r requirements.txt -r examples/requirements.txt - - run: pytest --color=yes --only-e2e + - run: pytest --color=yes --timeout=30 --only-e2e full-scale: strategy: @@ -137,7 +137,7 @@ jobs: python-version: "3.10" - run: tools/install-minikube.sh - run: pip install -r requirements.txt -r examples/requirements.txt - - run: pytest --color=yes --only-e2e + - run: pytest --color=yes --timeout=30 --only-e2e coveralls-finish: name: Finalize coveralls.io diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..a68aafec --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +addopts = + --strict-markers diff --git a/requirements.txt b/requirements.txt index 7b349939..c1fdac47 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,6 @@ -e . aresponses astpath[xpath] -async-timeout asynctest certbuilder certvalidator @@ -23,5 +22,6 @@ pytest-aiohttp pytest-asyncio pytest-cov pytest-mock +pytest-timeout types-pkg_resources types-PyYAML diff --git a/tests/handling/indexing/test_blocking_until_indexed.py b/tests/handling/indexing/test_blocking_until_indexed.py index 3f68d3bf..8d8c58a3 100644 --- a/tests/handling/indexing/test_blocking_until_indexed.py +++ b/tests/handling/indexing/test_blocking_until_indexed.py @@ -1,7 +1,6 @@ import asyncio import logging -import async_timeout import pytest from kopf._cogs.aiokits.aiotoggles import ToggleSet @@ -22,7 +21,7 @@ async def test_reporting_on_resource_readiness( operator_indexed = ToggleSet(all) resource_indexed = await operator_indexed.make_toggle() - async with timer, async_timeout.timeout(0.5) as timeout: + with timer: await process_resource_event( lifecycle=all_at_once, registry=registry, @@ -36,7 +35,6 @@ async def test_reporting_on_resource_readiness( operator_indexed=operator_indexed, resource_indexed=resource_indexed, ) - assert not timeout.expired assert timer.seconds < 0.2 # asap, nowait assert operator_indexed.is_on() assert set(operator_indexed) == set() # save RAM @@ -51,22 +49,20 @@ async def test_blocking_when_operator_is_not_ready( operator_indexed = ToggleSet(all) resource_listed = await operator_indexed.make_toggle() resource_indexed = await operator_indexed.make_toggle() - with pytest.raises(asyncio.TimeoutError): - async with timer, async_timeout.timeout(0.2) as timeout: - await process_resource_event( - lifecycle=all_at_once, - registry=registry, - settings=settings, - resource=resource, - indexers=indexers, - memories=ResourceMemories(), - memobase=Memo(), - raw_event={'type': event_type, 'object': {}}, - event_queue=asyncio.Queue(), - operator_indexed=operator_indexed, - resource_indexed=resource_indexed, - ) - assert timeout.expired + with pytest.raises(asyncio.TimeoutError), timer: + await asyncio.wait_for(process_resource_event( + lifecycle=all_at_once, + registry=registry, + settings=settings, + resource=resource, + indexers=indexers, + memories=ResourceMemories(), + memobase=Memo(), + raw_event={'type': event_type, 'object': {}}, + event_queue=asyncio.Queue(), + operator_indexed=operator_indexed, + resource_indexed=resource_indexed, + ), timeout=0.2) assert 0.2 < timer.seconds < 0.4 assert operator_indexed.is_off() assert set(operator_indexed) == {resource_listed} @@ -85,7 +81,7 @@ async def delayed_readiness(delay: float): operator_indexed = ToggleSet(all) resource_listed = await operator_indexed.make_toggle() resource_indexed = await operator_indexed.make_toggle() - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: asyncio.create_task(delayed_readiness(0.2)) await process_resource_event( lifecycle=all_at_once, @@ -100,7 +96,6 @@ async def delayed_readiness(delay: float): operator_indexed=operator_indexed, resource_indexed=resource_indexed, ) - assert not timeout.expired assert 0.2 < timer.seconds < 0.4 assert operator_indexed.is_on() assert set(operator_indexed) == {resource_listed} diff --git a/tests/k8s/test_watching_with_freezes.py b/tests/k8s/test_watching_with_freezes.py index 47ba50c1..c1512da8 100644 --- a/tests/k8s/test_watching_with_freezes.py +++ b/tests/k8s/test_watching_with_freezes.py @@ -1,7 +1,6 @@ import asyncio import logging -import async_timeout import pytest from kopf._cogs.aiokits.aiotoggles import ToggleSet @@ -15,7 +14,7 @@ async def test_pausing_is_ignored_if_turned_off( operator_paused = ToggleSet(any) await operator_paused.make_toggle(False) - async with timer, async_timeout.timeout(0.5) as timeout: + with timer: async with streaming_block( resource=resource, namespace=namespace, @@ -23,7 +22,6 @@ async def test_pausing_is_ignored_if_turned_off( ): pass - assert not timeout.expired assert timer.seconds < 0.2 # no waits, exits as soon as possible assert_logs([], prohibited=[ r"Pausing the watch-stream for", @@ -38,16 +36,17 @@ async def test_pausing_waits_forever_if_not_resumed( operator_paused = ToggleSet(any) await operator_paused.make_toggle(True) - with pytest.raises(asyncio.TimeoutError): - async with timer, async_timeout.timeout(0.5) as timeout: - async with streaming_block( + async def do_it(): + async with streaming_block( resource=resource, namespace=namespace, operator_paused=operator_paused, - ): - pass + ): + pass + + with pytest.raises(asyncio.TimeoutError), timer: + await asyncio.wait_for(do_it(), timeout=0.5) - assert timeout.expired assert timer.seconds >= 0.5 assert_logs([ r"Pausing the watch-stream for", @@ -67,7 +66,7 @@ async def delayed_resuming(delay: float): await asyncio.sleep(delay) await conflicts_found.turn_to(False) - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: asyncio.create_task(delayed_resuming(0.2)) async with streaming_block( resource=resource, @@ -76,7 +75,6 @@ async def delayed_resuming(delay: float): ): pass - assert not timeout.expired assert timer.seconds >= 0.2 assert timer.seconds <= 0.5 assert_logs([ diff --git a/tests/observation/test_processing_of_namespaces.py b/tests/observation/test_processing_of_namespaces.py index 16ad9049..a8c6ba4c 100644 --- a/tests/observation/test_processing_of_namespaces.py +++ b/tests/observation/test_processing_of_namespaces.py @@ -1,6 +1,5 @@ import asyncio -import async_timeout import pytest from kopf._cogs.structs.bodies import RawBody, RawEvent @@ -19,11 +18,9 @@ async def delayed_injection(delay: float): task = asyncio.create_task(delayed_injection(0)) with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - async with insights.revised: - await insights.revised.wait() + async with insights.revised: + await asyncio.wait_for(insights.revised.wait(), timeout=0.1) await task - assert timeout.expired assert not insights.namespaces @@ -38,7 +35,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, namespaces=['ns*']) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1): + with timer: async with insights.revised: await insights.revised.wait() await task @@ -58,7 +55,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, namespaces=['ns*']) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1): + with timer: async with insights.revised: await insights.revised.wait() await task diff --git a/tests/observation/test_processing_of_resources.py b/tests/observation/test_processing_of_resources.py index 9f1a349c..9347b369 100644 --- a/tests/observation/test_processing_of_resources.py +++ b/tests/observation/test_processing_of_resources.py @@ -1,7 +1,6 @@ import asyncio import aiohttp.web -import async_timeout import pytest import kopf @@ -126,7 +125,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, registry=registry, settings=settings) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1.0): + with timer: async with insights.revised: await insights.revised.wait() await task @@ -148,11 +147,9 @@ async def delayed_injection(delay: float): task = asyncio.create_task(delayed_injection(0)) with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - async with insights.revised: - await insights.revised.wait() + async with insights.revised: + await asyncio.wait_for(insights.revised.wait(), timeout=0.1) await task - assert timeout.expired assert not insights.indexed_resources assert not insights.watched_resources assert not insights.webhook_resources @@ -173,7 +170,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, registry=registry, settings=settings) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1.0): + with timer: async with insights.revised: await insights.revised.wait() await task @@ -198,7 +195,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, registry=registry, settings=settings) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1.0): + with timer: async with insights.revised: await insights.revised.wait() await task @@ -222,7 +219,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, registry=registry, settings=settings) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1.0): + with timer: async with insights.revised: await insights.revised.wait() await task @@ -244,7 +241,7 @@ async def delayed_injection(delay: float): insights=insights, raw_event=e1, registry=registry, settings=settings) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1.0): + with timer: await insights.backbone.wait_for(NAMESPACES) await task assert 0.1 < timer.seconds < 1.0 diff --git a/tests/posting/test_poster.py b/tests/posting/test_poster.py index 1c5e1b96..c526200e 100644 --- a/tests/posting/test_poster.py +++ b/tests/posting/test_poster.py @@ -1,9 +1,7 @@ import asyncio import logging -import async_timeout import pytest -from asynctest import call from kopf import event, exception, info, warn from kopf._cogs.structs.references import Backbone, Resource @@ -45,8 +43,7 @@ def _cancel(*args, **kwargs): # A way to cancel a `while True` cycle by timing, even if the routines are not called. with pytest.raises(asyncio.CancelledError): - async with async_timeout.timeout(0.5): - await poster(event_queue=event_queue, backbone=backbone, settings=settings) + await poster(event_queue=event_queue, backbone=backbone, settings=settings) assert post.call_count == 2 assert post.call_args_list[0][1]['url'] == '/api/v1/namespaces/ns1/events' diff --git a/tests/primitives/test_conditions.py b/tests/primitives/test_conditions.py index 2620a179..75919bd8 100644 --- a/tests/primitives/test_conditions.py +++ b/tests/primitives/test_conditions.py @@ -1,6 +1,5 @@ import asyncio -import async_timeout import pytest from kopf._cogs.aiokits.aiobindings import condition_chain @@ -11,14 +10,9 @@ async def test_no_triggering(): target = asyncio.Condition() task = asyncio.create_task(condition_chain(source, target)) try: - with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - async with target: - await target.wait() - - assert timeout.expired - + async with target: + await asyncio.wait_for(target.wait(), timeout=0.1) finally: task.cancel() await asyncio.wait([task]) @@ -36,11 +30,10 @@ async def delayed_trigger(): event_loop.call_later(0.1, asyncio.create_task, delayed_trigger()) - async with timer, async_timeout.timeout(10) as timeout: + with timer: async with target: await target.wait() - assert not timeout.expired assert 0.1 <= timer.seconds <= 0.2 finally: diff --git a/tests/primitives/test_containers.py b/tests/primitives/test_containers.py index 2604e0f9..d289d129 100644 --- a/tests/primitives/test_containers.py +++ b/tests/primitives/test_containers.py @@ -1,6 +1,5 @@ import asyncio -import async_timeout import pytest from kopf._cogs.aiokits.aiovalues import Container @@ -9,9 +8,7 @@ async def test_empty_by_default(): container = Container() with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - await container.wait() - assert timeout.expired + await asyncio.wait_for(container.wait(), timeout=0.1) async def test_does_not_wake_up_when_reset(event_loop, timer): @@ -23,20 +20,16 @@ async def reset_it(): event_loop.call_later(0.05, asyncio.create_task, reset_it()) with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - await container.wait() - - assert timeout.expired + await asyncio.wait_for(container.wait(), timeout=0.1) async def test_wakes_up_when_preset(event_loop, timer): container = Container() await container.set(123) - async with timer, async_timeout.timeout(10) as timeout: + with timer: result = await container.wait() - assert not timeout.expired assert timer.seconds <= 0.1 assert result == 123 @@ -49,10 +42,9 @@ async def set_it(): event_loop.call_later(0.1, asyncio.create_task, set_it()) - async with timer, async_timeout.timeout(10) as timeout: + with timer: result = await container.wait() - assert not timeout.expired assert 0.1 <= timer.seconds <= 0.2 assert result == 123 @@ -67,13 +59,12 @@ async def set_it(v): event_loop.call_later(0.2, asyncio.create_task, set_it(234)) values = [] - async with timer, async_timeout.timeout(10) as timeout: + with timer: async for value in container.as_changed(): values.append(value) if value == 234: break - assert not timeout.expired assert 0.2 <= timer.seconds <= 0.3 assert values == [123, 234] @@ -83,11 +74,10 @@ async def test_iterates_when_preset(event_loop, timer): await container.set(123) values = [] - async with timer, async_timeout.timeout(10) as timeout: + with timer: async for value in container.as_changed(): values.append(value) break - assert not timeout.expired assert timer.seconds <= 0.1 assert values == [123] diff --git a/tests/primitives/test_flags.py b/tests/primitives/test_flags.py index 207739c8..f18826b4 100644 --- a/tests/primitives/test_flags.py +++ b/tests/primitives/test_flags.py @@ -2,7 +2,6 @@ import concurrent.futures import threading -import async_timeout import pytest from kopf._cogs.aiokits.aioadapters import check_flag, raise_flag, wait_flag @@ -135,19 +134,16 @@ async def test_waiting_of_none_does_nothing(): async def test_waiting_for_unraised_times_out(flag, timer): - with pytest.raises(asyncio.TimeoutError): - async with timer, async_timeout.timeout(0.1) as timeout: - await wait_flag(flag) + with pytest.raises(asyncio.TimeoutError), timer: + await asyncio.wait_for(wait_flag(flag), timeout=0.1) assert timer.seconds >= 0.1 - assert timeout.expired async def test_waiting_for_preraised_is_instant(flag, timer): await raise_flag(flag) # tested separately above - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: await wait_flag(flag) assert timer.seconds < 0.5 # near-instant, plus code overhead - assert not timeout.expired async def test_waiting_for_raised_during_the_wait(flag, timer): @@ -157,7 +153,6 @@ async def raise_delayed(delay: float) -> None: await raise_flag(flag) # tested separately above asyncio.create_task(raise_delayed(0.2)) - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: await wait_flag(flag) assert 0.2 <= timer.seconds < 0.5 # near-instant once raised - assert not timeout.expired diff --git a/tests/primitives/test_toggles.py b/tests/primitives/test_toggles.py index 0351fa8f..d670bbd9 100644 --- a/tests/primitives/test_toggles.py +++ b/tests/primitives/test_toggles.py @@ -1,6 +1,5 @@ import asyncio -import async_timeout import pytest from kopf._cogs.aiokits.aiotoggles import Toggle @@ -41,21 +40,17 @@ async def test_turning_off(): async def test_waiting_until_on_fails_when_not_turned_on(): toggle = Toggle(False) with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - await toggle.wait_for(True) + await asyncio.wait_for(toggle.wait_for(True), timeout=0.1) assert toggle.is_off() - assert timeout.expired async def test_waiting_until_off_fails_when_not_turned_off(): toggle = Toggle(True) with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - await toggle.wait_for(False) + await asyncio.wait_for(toggle.wait_for(False), timeout=0.1) assert toggle.is_on() - assert timeout.expired async def test_waiting_until_on_wakes_when_turned_on(timer): @@ -65,12 +60,11 @@ async def delayed_turning_on(delay: float): await asyncio.sleep(delay) await toggle.turn_to(True) - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: asyncio.create_task(delayed_turning_on(0.05)) await toggle.wait_for(True) assert toggle.is_on() - assert not timeout.expired assert timer.seconds < 0.5 # approx. 0.05 plus some code overhead @@ -81,12 +75,11 @@ async def delayed_turning_off(delay: float): await asyncio.sleep(delay) await toggle.turn_to(False) - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: asyncio.create_task(delayed_turning_off(0.05)) await toggle.wait_for(False) assert toggle.is_off() - assert not timeout.expired assert timer.seconds < 0.5 # approx. 0.05 plus some code overhead diff --git a/tests/reactor/test_queueing.py b/tests/reactor/test_queueing.py index e67d2547..f41a35d6 100644 --- a/tests/reactor/test_queueing.py +++ b/tests/reactor/test_queueing.py @@ -19,7 +19,6 @@ import gc import weakref -import async_timeout import pytest from kopf._core.reactor.queueing import EOS, watcher @@ -214,9 +213,8 @@ async def test_garbage_collection_of_streams(settings, stream, events, unique, w settings.batching.idle_timeout + # idling on empty queues. 1.0) # the code itself takes time: add a max tolerable delay. with contextlib.suppress(asyncio.TimeoutError): - async with async_timeout.timeout(allowed_timeout): - async with signaller: - await signaller.wait_for(lambda: not streams) + async with signaller: + await asyncio.wait_for(signaller.wait_for(lambda: not streams), timeout=allowed_timeout) # The mutable(!) streams dict is now empty, i.e. garbage-collected. assert len(streams) == 0 diff --git a/tests/references/test_backbone.py b/tests/references/test_backbone.py index bc1b4b77..767c352d 100644 --- a/tests/references/test_backbone.py +++ b/tests/references/test_backbone.py @@ -1,6 +1,5 @@ import asyncio -import async_timeout import pytest from kopf._cogs.structs.references import CLUSTER_PEERINGS_K, CLUSTER_PEERINGS_Z, CRDS, EVENTS, \ @@ -51,16 +50,14 @@ async def test_refill_is_cumulative_ie_does_not_reset(): async def test_waiting_for_absent_resources_never_ends(timer): backbone = Backbone() with pytest.raises(asyncio.TimeoutError): - async with async_timeout.timeout(0.1) as timeout: - await backbone.wait_for(NAMESPACES) - assert timeout.expired + await asyncio.wait_for(backbone.wait_for(NAMESPACES), timeout=0.1) async def test_waiting_for_preexisting_resources_ends_instantly(timer): resource = Resource('', 'v1', 'namespaces') backbone = Backbone() await backbone.fill(resources=[resource]) - async with timer, async_timeout.timeout(1): + with timer: found_resource = await backbone.wait_for(NAMESPACES) assert timer.seconds < 0.1 assert found_resource == resource @@ -75,7 +72,7 @@ async def delayed_injection(delay: float): await backbone.fill(resources=[resource]) task = asyncio.create_task(delayed_injection(0.1)) - async with timer, async_timeout.timeout(1): + with timer: found_resource = await backbone.wait_for(NAMESPACES) await task assert 0.1 < timer.seconds < 0.11 diff --git a/tests/timing/test_sleeping.py b/tests/timing/test_sleeping.py index 52145bc0..dfc8d77e 100644 --- a/tests/timing/test_sleeping.py +++ b/tests/timing/test_sleeping.py @@ -1,32 +1,28 @@ import asyncio -import async_timeout import pytest from kopf._cogs.aiokits.aiotime import sleep async def test_the_only_delay_is_awaited(timer): - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(0.10) assert 0.10 <= timer.seconds < 0.11 - assert not timeout.expired assert unslept is None async def test_the_shortest_delay_is_awaited(timer): - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep([0.10, 0.20]) assert 0.10 <= timer.seconds < 0.11 - assert not timeout.expired assert unslept is None async def test_specific_delays_only_are_awaited(timer): - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep([0.10, None]) assert 0.10 <= timer.seconds < 0.11 - assert not timeout.expired assert unslept is None @@ -36,10 +32,9 @@ async def test_specific_delays_only_are_awaited(timer): pytest.param(-10, id='alone'), ]) async def test_negative_delays_skip_sleeping(timer, delays): - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(delays) assert timer.seconds < 0.01 - assert not timeout.expired assert unslept is None @@ -48,39 +43,35 @@ async def test_negative_delays_skip_sleeping(timer, delays): pytest.param([None], id='list-of-none'), ]) async def test_no_delays_skip_sleeping(timer, delays): - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(delays) assert timer.seconds < 0.01 - assert not timeout.expired assert unslept is None async def test_by_event_set_before_time_comes(timer): event = asyncio.Event() asyncio.get_running_loop().call_later(0.07, event.set) - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(0.10, event) assert unslept is not None assert 0.02 <= unslept <= 0.04 assert 0.06 <= timer.seconds <= 0.08 - assert not timeout.expired async def test_with_zero_time_and_event_initially_cleared(timer): event = asyncio.Event() event.clear() - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(0, event) assert timer.seconds <= 0.01 - assert not timeout.expired assert unslept is None async def test_with_zero_time_and_event_initially_set(timer): event = asyncio.Event() event.set() - async with timer, async_timeout.timeout(1.0) as timeout: + with timer: unslept = await sleep(0, event) assert timer.seconds <= 0.01 - assert not timeout.expired assert not unslept # 0/None; undefined for such case: both goals reached. diff --git a/tests/utilities/aiotasks/test_scheduler.py b/tests/utilities/aiotasks/test_scheduler.py index b12d039a..2670748c 100644 --- a/tests/utilities/aiotasks/test_scheduler.py +++ b/tests/utilities/aiotasks/test_scheduler.py @@ -1,7 +1,6 @@ import asyncio from unittest.mock import Mock -import async_timeout import pytest from kopf._cogs.aiokits.aiotasks import Scheduler @@ -26,7 +25,7 @@ async def f(mock, *args): async def test_empty_scheduler_lifecycle(timer): - async with timer, async_timeout.timeout(1): + with timer: scheduler = Scheduler() assert scheduler.empty() await scheduler.wait() @@ -45,12 +44,12 @@ async def test_task_spawning_and_graceful_finishing(timer): result = await scheduler.spawn(f(mock, flag1, 0.1, flag2)) assert result is None - async with timer, async_timeout.timeout(1): + with timer: await flag1.wait() assert timer.seconds < CODE_OVERHEAD assert mock.call_args[0][0] == 'started' - async with timer, async_timeout.timeout(1): + with timer: await flag2.wait() assert timer.seconds > 0.1 assert timer.seconds < 0.1 + CODE_OVERHEAD @@ -68,12 +67,12 @@ async def test_task_spawning_and_cancellation(timer): result = await scheduler.spawn(f(mock, flag1, 1.0, flag2)) assert result is None - async with timer, async_timeout.timeout(1): + with timer: await flag1.wait() assert timer.seconds < CODE_OVERHEAD assert mock.call_args[0][0] == 'started' - async with timer, async_timeout.timeout(1): + with timer: await scheduler.close() assert timer.seconds < CODE_OVERHEAD # near-instant assert mock.call_args[0][0] == 'cancelled' @@ -87,18 +86,16 @@ async def test_no_tasks_are_accepted_after_closing(): assert scheduler._spawning_task.done() assert scheduler._cleaning_task.done() - async with async_timeout.timeout(1): - with pytest.raises(RuntimeError, match=r"Cannot add new coroutines"): - await scheduler.spawn(f(Mock(), 1.0)) + with pytest.raises(RuntimeError, match=r"Cannot add new coroutines"): + await scheduler.spawn(f(Mock(), 1.0)) async def test_successes_are_not_reported(): exception_handler = Mock() scheduler = Scheduler(exception_handler=exception_handler) - async with async_timeout.timeout(1): - await scheduler.spawn(f(Mock())) - await scheduler.wait() - await scheduler.close() + await scheduler.spawn(f(Mock())) + await scheduler.wait() + await scheduler.close() assert exception_handler.call_count == 0 @@ -106,10 +103,9 @@ async def test_cancellations_are_not_reported(): exception_handler = Mock() mock = Mock(side_effect=asyncio.CancelledError()) scheduler = Scheduler(exception_handler=exception_handler) - async with async_timeout.timeout(1): - await scheduler.spawn(f(mock, 1)) - await scheduler.wait() - await scheduler.close() + await scheduler.spawn(f(mock, 1)) + await scheduler.wait() + await scheduler.close() assert exception_handler.call_count == 0 @@ -118,10 +114,9 @@ async def test_exceptions_are_reported(): exception_handler = Mock() mock = Mock(side_effect=exception) scheduler = Scheduler(exception_handler=exception_handler) - async with async_timeout.timeout(1): - await scheduler.spawn(f(mock)) - await scheduler.wait() - await scheduler.close() + await scheduler.spawn(f(mock)) + await scheduler.wait() + await scheduler.close() assert exception_handler.call_count == 1 assert exception_handler.call_args[0][0] is exception @@ -138,12 +133,12 @@ async def test_tasks_are_parallel_if_limit_is_not_reached(timer): task2_finished = asyncio.Event() scheduler = Scheduler(limit=2) - async with timer, async_timeout.timeout(1): + with timer: await scheduler.spawn(f(Mock(), task1_started, 0.1, task1_finished)) await scheduler.spawn(f(Mock(), task2_started, 0.1, task2_finished)) assert timer.seconds < CODE_OVERHEAD # i.e. spawning is not not blocking - async with timer, async_timeout.timeout(1): + with timer: await task1_finished.wait() assert task2_started.is_set() await task2_finished.wait() @@ -166,12 +161,12 @@ async def test_tasks_are_pending_if_limit_is_reached(timer): task2_finished = asyncio.Event() scheduler = Scheduler(limit=1) - async with timer, async_timeout.timeout(1): + with timer: await scheduler.spawn(f(Mock(), task1_started, 0.1, task1_finished)) await scheduler.spawn(f(Mock(), task2_started, 0.1, task2_finished)) assert timer.seconds < CODE_OVERHEAD # i.e. spawning is not not blocking - async with timer, async_timeout.timeout(1): + with timer: await task1_finished.wait() assert not task2_started.is_set() await task2_finished.wait() diff --git a/tests/utilities/aiotasks/test_task_stopping.py b/tests/utilities/aiotasks/test_task_stopping.py index 1e62eb1c..3717bd46 100644 --- a/tests/utilities/aiotasks/test_task_stopping.py +++ b/tests/utilities/aiotasks/test_task_stopping.py @@ -1,7 +1,6 @@ import asyncio import logging -import async_timeout import pytest from kopf._cogs.aiokits.aiotasks import create_task, stop @@ -41,8 +40,7 @@ async def test_stop_immediately_with_finishing(assert_logs, caplog): caplog.set_level(0) task1 = create_task(simple()) task2 = create_task(simple()) - async with async_timeout.timeout(1): # extra test safety - done, pending = await stop([task1, task2], title='sample', logger=logger, cancelled=False) + done, pending = await stop([task1, task2], title='sample', logger=logger, cancelled=False) assert done assert not pending assert_logs(["Sample tasks are stopped: finishing normally"]) @@ -55,8 +53,7 @@ async def test_stop_immediately_with_cancelling(assert_logs, caplog): caplog.set_level(0) task1 = create_task(simple()) task2 = create_task(simple()) - async with async_timeout.timeout(1): # extra test safety - done, pending = await stop([task1, task2], title='sample', logger=logger, cancelled=True) + done, pending = await stop([task1, task2], title='sample', logger=logger, cancelled=True) assert done assert not pending assert_logs(["Sample tasks are stopped: cancelling normally"]) @@ -72,16 +69,14 @@ async def test_stop_iteratively(assert_logs, caplog, cancelled): task2 = create_task(stuck()) stask = create_task(stop([task1, task2], title='sample', logger=logger, interval=0.01, cancelled=cancelled)) - async with async_timeout.timeout(1): # extra test safety - done, pending = await asyncio.wait({stask}, timeout=0.011) + done, pending = await asyncio.wait({stask}, timeout=0.011) assert not done assert task1.done() assert not task2.done() task2.cancel() - async with async_timeout.timeout(1): # extra test safety - done, pending = await asyncio.wait({stask}, timeout=0.011) + done, pending = await asyncio.wait({stask}, timeout=0.011) assert done assert task1.done() assert task2.done() @@ -100,16 +95,14 @@ async def test_stop_itself_is_cancelled(assert_logs, caplog, cancelled): task2 = create_task(stuck()) stask = create_task(stop([task1, task2], title='sample', logger=logger, interval=0.01, cancelled=cancelled)) - async with async_timeout.timeout(1): # extra test safety - done, pending = await asyncio.wait({stask}, timeout=0.011) + done, pending = await asyncio.wait({stask}, timeout=0.011) assert not done assert task1.done() assert not task2.done() stask.cancel() - async with async_timeout.timeout(1): # extra test safety - done, pending = await asyncio.wait({stask}, timeout=0.011) + done, pending = await asyncio.wait({stask}, timeout=0.011) assert done assert task1.done() assert not task2.done() @@ -122,8 +115,7 @@ async def test_stop_itself_is_cancelled(assert_logs, caplog, cancelled): ]) task2.cancel() - async with async_timeout.timeout(1): # extra test safety - done, pending = await asyncio.wait({task1, task2}) + done, pending = await asyncio.wait({task1, task2}) assert done assert task1.done() assert task2.done()