File: test_win_sleep.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (41 lines) | stat: -rw-r--r-- 1,204 bytes parent folder | download | duplicates (2)
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
import asyncio
import time
import sys

import pytest

from textual.app import App

pytestmark = pytest.mark.skipif(
    sys.platform != "win32", reason="We only need to test this on Windows."
)


def test_win_sleep_timer_is_cancellable():
    """Regression test for https://github.com/Textualize/textual/issues/2711.

    When we exit an app with a "long" timer, everything asyncio-related
    should shutdown quickly. So, we create an app with a timer that triggers
    every SLEEP_FOR seconds and we shut the app down immediately after creating
    it. `asyncio` should be done quickly (i.e., the timer was cancelled) and
    thus the total time this takes should be considerably lesser than the time
    we originally set the timer for.
    """

    SLEEP_FOR = 10

    class WindowsIntervalBugApp(App[None]):
        def on_mount(self) -> None:
            self.set_interval(SLEEP_FOR, lambda: None)

        def key_e(self):
            self.exit()

    async def actual_test():
        async with WindowsIntervalBugApp().run_test() as pilot:
            await pilot.press("e")

    start = time.perf_counter()
    asyncio.run(actual_test())
    end = time.perf_counter()
    assert end - start < 1