Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the libtorrent tests more responsive #7541

Merged
merged 4 commits into from
Jul 17, 2023

Conversation

drew2a
Copy link
Contributor

@drew2a drew2a commented Jul 11, 2023

This PR doesn't completely solve the issue causing the flakiness of the libtorrent tests. However, the changes have made the tests more responsive and should, at the very least, prevent the main thread from freezing.

Besides minor refactoring, there are two significant changes in this PR:

  1. An asyncio switch was added to the wait_for_status function due to the potential of blocking the main thread during pytest execution. To understand the possibility, consider this example:

    async def test_without_switch():
    # In this test, we reproduce the asyncio behavior where there's a possibility
    # to block the main thread if there's no blocking event in the coroutine.
    class EndOfTheTest(TimeoutError):
    """Exception to stop a test after 0.2 seconds."""
    start_time = time.time()
    async def any_non_blocking_coro():
    duration = time.time() - start_time
    if duration >= 0.2:
    # to prevent indefinite block of a thread, raise an exception after 2 seconds.
    raise EndOfTheTest
    # With this line ↓
    # await asyncio.sleep(0)
    # `asyncio.wait_for()` will work as expected, interrupting the execution of the coroutine after 0.1 sec.
    # Without this line, the main thread will be blocked indefinitely.
    async def a():
    while True:
    await any_non_blocking_coro()
    # We are waiting for the timeout, but coroutine a() will never be cancelled.
    with pytest.raises(EndOfTheTest):
    await asyncio.wait_for(a(), timeout=0.1)

  2. The shutdown logic has been rewritten to be slightly more accurate. While this change may not directly influence test stability, it could contribute slightly to it.

Related to #7495

Refs:

@drew2a drew2a requested a review from kozlovsky July 12, 2023 10:26
@drew2a drew2a marked this pull request as ready for review July 12, 2023 10:26
@drew2a drew2a requested a review from a team July 12, 2023 10:26
@drew2a drew2a force-pushed the fix/libtorrent_tests branch 2 times, most recently from bcd9a6a to f2c11fb Compare July 13, 2023 13:06
@drew2a drew2a force-pushed the fix/libtorrent_tests branch from f2c11fb to 895e0d2 Compare July 14, 2023 10:17
@drew2a drew2a force-pushed the fix/libtorrent_tests branch from 7b6d124 to cc1e93b Compare July 17, 2023 09:40
@drew2a drew2a merged commit 6ae3027 into Tribler:main Jul 17, 2023
@drew2a drew2a deleted the fix/libtorrent_tests branch July 17, 2023 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants