| 1 |
import time |
| 2 |
from threading import Event |
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from briefcase.console import LogLevel |
| 8 |
from briefcase.integrations import subprocess |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def mock_sub(mock_sub): |
| 13 |
# also mock cleanup for stream output testing |
| 14 |
mock_sub.cleanup = mock.MagicMock() |
| 15 |
return mock_sub |
| 16 |
|
| 17 |
|
| 18 |
def test_output(mock_sub, streaming_process, sleep_zero, capsys): |
| 19 |
"""Process output is printed.""" |
| 20 |
mock_sub.stream_output("testing", streaming_process) |
| 21 |
|
| 22 |
# fmt: off |
| 23 |
assert capsys.readouterr().out == ( |
| 24 |
"output line 1\n" |
| 25 |
"\n" |
| 26 |
"output line 3\n" |
| 27 |
) |
| 28 |
# fmt: on |
| 29 |
mock_sub.cleanup.assert_called_once_with("testing", streaming_process) |
| 30 |
|
| 31 |
|
| 32 |
def test_output_debug(mock_sub, streaming_process, sleep_zero, capsys): |
| 33 |
"""Process output is printed; no debug output for only stream_output.""" |
| 34 |
mock_sub.tools.console.verbosity = LogLevel.DEBUG |
| 35 |
|
| 36 |
mock_sub.stream_output("testing", streaming_process) |
| 37 |
|
| 38 |
# fmt: off |
| 39 |
assert capsys.readouterr().out == ( |
| 40 |
"output line 1\n" |
| 41 |
"\n" |
| 42 |
"output line 3\n" |
| 43 |
) |
| 44 |
# fmt: on |
| 45 |
|
| 46 |
mock_sub.cleanup.assert_called_once_with("testing", streaming_process) |
| 47 |
|
| 48 |
|
| 49 |
def test_keyboard_interrupt(mock_sub, streaming_process, capsys): |
| 50 |
"""KeyboardInterrupt is suppressed if user sends CTRL+C and all output is |
| 51 |
printed.""" |
| 52 |
|
| 53 |
send_ctrl_c = mock.MagicMock() |
| 54 |
send_ctrl_c.side_effect = [False, KeyboardInterrupt] |
| 55 |
|
| 56 |
with pytest.raises(KeyboardInterrupt): |
| 57 |
mock_sub.stream_output("testing", streaming_process, stop_func=send_ctrl_c) |
| 58 |
|
| 59 |
# fmt: off |
| 60 |
assert capsys.readouterr().out == ( |
| 61 |
"output line 1\n" |
| 62 |
"\n" |
| 63 |
"output line 3\n" |
| 64 |
"Stopping...\n" |
| 65 |
) |
| 66 |
# fmt: on |
| 67 |
mock_sub.cleanup.assert_called_once_with("testing", streaming_process) |
| 68 |
|
| 69 |
|
| 70 |
def test_process_exit_with_queued_output( |
| 71 |
mock_sub, |
| 72 |
streaming_process, |
| 73 |
sleep_zero, |
| 74 |
capsys, |
| 75 |
): |
| 76 |
"""All output is printed despite the process exiting early.""" |
| 77 |
streaming_process.poll.side_effect = [None, -3, -3, -3] |
| 78 |
|
| 79 |
mock_sub.stream_output("testing", streaming_process) |
| 80 |
|
| 81 |
# fmt: off |
| 82 |
assert capsys.readouterr().out == ( |
| 83 |
"output line 1\n" |
| 84 |
"\n" |
| 85 |
"output line 3\n" |
| 86 |
) |
| 87 |
# fmt: on |
| 88 |
mock_sub.cleanup.assert_called_once_with("testing", streaming_process) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.parametrize("stop_func_ret_val", [True, False]) |
| 92 |
def test_stop_func(mock_sub, streaming_process, stop_func_ret_val, sleep_zero, capsys): |
| 93 |
"""All output is printed whether stop_func aborts streaming or not.""" |
| 94 |
mock_sub.stream_output( |
| 95 |
"testing", streaming_process, stop_func=lambda: stop_func_ret_val |
| 96 |
) |
| 97 |
|
| 98 |
# fmt: off |
| 99 |
assert capsys.readouterr().out == ( |
| 100 |
"output line 1\n" |
| 101 |
"\n" |
| 102 |
"output line 3\n" |
| 103 |
) |
| 104 |
# fmt: on |
| 105 |
mock_sub.cleanup.assert_called_once_with("testing", streaming_process) |
| 106 |
|
| 107 |
|
| 108 |
def test_stuck_streamer(mock_sub, streaming_process, sleep_zero, monkeypatch, capsys): |
| 109 |
"""Following a KeyboardInterrupt, output streaming returns even if the output |
| 110 |
streamer becomes stuck.""" |
| 111 |
|
| 112 |
# Mock time.time() to return times that monotonically increase by 1s |
| 113 |
# every time it is invoked. This allows us to simulate the progress of |
| 114 |
# time much faster than the actual calls to time.sleep() would. |
| 115 |
mock_time = mock.MagicMock(side_effect=range(1000, 1005)) |
| 116 |
monkeypatch.setattr(time, "time", mock_time) |
| 117 |
|
| 118 |
# Flag that Briefcase has finished simulating its waiting on the output |
| 119 |
# streamer to exit normally; so, it should now exit. |
| 120 |
monkeypatched_streamer_should_exit = Event() |
| 121 |
# Flag that Briefcase waited too long on the output streamer |
| 122 |
monkeypatched_streamer_was_improperly_awaited = Event() |
| 123 |
# Flag that output streamer has exited |
| 124 |
monkeypatched_streamer_exited = Event() |
| 125 |
|
| 126 |
def monkeypatched_blocked_streamer(*a, **kw): |
| 127 |
"""Simulate a streamer that blocks longer than it will be waited on.""" |
| 128 |
if not monkeypatched_streamer_should_exit.wait(timeout=5): |
| 129 |
monkeypatched_streamer_was_improperly_awaited.set() |
| 130 |
monkeypatched_streamer_exited.set() |
| 131 |
|
| 132 |
monkeypatch.setattr( |
| 133 |
subprocess.PopenOutputStreamer, |
| 134 |
"run", |
| 135 |
monkeypatched_blocked_streamer, |
| 136 |
) |
| 137 |
|
| 138 |
send_ctrl_c = mock.MagicMock() |
| 139 |
send_ctrl_c.side_effect = [False, KeyboardInterrupt] |
| 140 |
with pytest.raises(KeyboardInterrupt): |
| 141 |
mock_sub.stream_output("testing", streaming_process, stop_func=send_ctrl_c) |
| 142 |
|
| 143 |
monkeypatched_streamer_should_exit.set() |
| 144 |
|
| 145 |
# Since the waiting around for the output streamer has been |
| 146 |
# short-circuited, Briefcase should quickly give up waiting on |
| 147 |
# the output streamer and it should exit...so, confirm it does. |
| 148 |
assert monkeypatched_streamer_exited.wait(timeout=1) |
| 149 |
assert not monkeypatched_streamer_was_improperly_awaited.is_set() |
| 150 |
|
| 151 |
# fmt: off |
| 152 |
assert capsys.readouterr().out == ( |
| 153 |
"Stopping...\n" |
| 154 |
"Log stream hasn't terminated; log output may be corrupted.\n" |
| 155 |
) |
| 156 |
# fmt: on |