summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/tests/test_ctrl_c.py
blob: 53a798a33bea5733e4932f4d203f4bc98a69ac32 (plain) (blame)
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
"""
Ctrl+C stops the client at once, whatever its loop is waiting for.

On Windows the client runs a selector loop (aiortc's ICE needs one), and a
console event does not interrupt select(): Ctrl+C waited for the loop's next
timer — half a minute in `chat --follow` between pings.
"""

import sys
import time

from console import ctrl_c, interruptible

IDLE = ("import asyncio, sys\n"
        "from meshbay_cli.__main__ import loop_factory\n"
        "print('ready', flush=True)\n"
        "try:\n"
        "    asyncio.run(asyncio.sleep(60), loop_factory=loop_factory())\n"
        "except KeyboardInterrupt:\n"
        "    sys.exit(130)\n")


def test_ctrl_c_wakes_an_idle_client_at_once():
    proc = interruptible([sys.executable, "-c", IDLE], None)
    try:
        assert proc.stdout.readline().strip() == "ready", proc.stderr.read()
        started = time.monotonic()
        ctrl_c(proc)
        proc.wait(timeout=30)
        assert proc.returncode == 130, proc.stderr.read()
        assert time.monotonic() - started < 5
    finally:
        if proc.poll() is None:
            proc.kill()