Skip to content

Improve coroutine timeout handling#295

Merged
deathaxe merged 1 commit into
masterfrom
feat/async-timeout
Jun 7, 2026
Merged

Improve coroutine timeout handling#295
deathaxe merged 1 commit into
masterfrom
feat/async-timeout

Conversation

@deathaxe
Copy link
Copy Markdown
Member

@deathaxe deathaxe commented Jun 7, 2026

resolves #294

This PR...

  1. adds class-level timeout_ms attribute to customize runtime of asyncio coroutines.

    Example:

       class MyAsyncTestCase(AsyncTestCase):
           timeout_ms=10000
  2. uses timeout_ms argument from test_... coroutines to cutomize test case runtime.

    Example:

       async def test_anything(self, timeout_ms=10000):
           ...
  3. raises TimeoutError within context of currently awaited asynchronous coroutine to ensure valuable tracebacks being printed.

    Example:

    test_coroutine (test_asyncio_timeout.MyAsyncTestCaseA.test_coroutine) ... ERROR
    
    ======================================================================
    ERROR: test_coroutine (test_asyncio_timeout.MyAsyncTestCaseA.test_coroutine)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "<ST>\Data\Packages\UnitTesting\unittesting\core\py313\case.py", line 120, in run
        outcome.expecting_failure = False
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<ST>\Data\Packages\UnitTesting\unittesting\core\py313\case.py", line 71, in _callMaybeCoro
      File "<ST>\Data\Packages\UnitTesting\tests\test_asyncio_timeout.py", line 23, in test_coroutine
        await a_coro(self)
      File "<ST>\Data\Packages\UnitTesting\tests\test_asyncio_timeout.py", line 7, in a_coro
        await a_coro2(test)
      File "<ST>\Data\Packages\UnitTesting\tests\test_asyncio_timeout.py", line 11, in a_coro2
        await a_coro3(test)
      File "<ST>\Data\Packages\UnitTesting\tests\test_asyncio_timeout.py", line 15, in a_coro3
        await asyncio.sleep(10.0)
      File "./python3.14/asyncio/tasks.py", line 702, in sleep
    TimeoutError: Task not completed within 4.50 seconds.
    
    ----------------------------------------------------------------------
    Ran 1 test in 4.667s
    
    FAILED (errors=1)
    

This commit...

1. adds class-level `timeout_ms` attribute to customize runtime
   of asyncio coroutines.
2. uses `timeout_ms` argument from test_... coroutines to cutomize
   test case runtime.

   example:

      async def test_anything(self, timeout_ms=10000):
          ...

3. raises TimeoutError within context of currently awaited asynchronous
   coroutine to ensure valuable tracebacks being printed.
@deathaxe deathaxe merged commit dcc7e97 into master Jun 7, 2026
14 checks passed
@deathaxe deathaxe deleted the feat/async-timeout branch June 7, 2026 16:48
yield {"condition": await_future, "timeout": timeout_ms}
except TimeoutError:
msg = f"Task not completed within {timeout_ms / 1000:.2f} seconds."
coro.throw(TimeoutError, msg)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn’t know this .throw was possible. Very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When a timeout occurs, the stacktrace from AsyncTestCase does not help

2 participants