Debian Codegraph @

tests/integrations/subprocess/test_Subprocess__cleanup.py

raw
1 import subprocess
2 from unittest import mock
3
4
5 def test_clean_termination(mock_sub, capsys):
6 """A popen process can be cleanly terminated."""
7 process = mock.MagicMock(spec_set=subprocess.Popen)
8
9 mock_sub.cleanup("testing process", process)
10
11 process.terminate.assert_called_once()
12 process.wait.assert_called_once_with(timeout=3)
13 process.kill.assert_not_called()
14
15 # No log messages for a clean exit
16 assert capsys.readouterr().out == ""
17
18
19 def test_dirty_termination(mock_sub, capsys):
20 """If terminate doesn't stop the process, it will be forcibly killed."""
21 process = mock.MagicMock(spec_set=subprocess.Popen)
22 process.wait.side_effect = subprocess.TimeoutExpired(cmd="ls", timeout=3)
23
24 mock_sub.cleanup("testing process", process)
25
26 process.terminate.assert_called_once()
27 process.wait.assert_called_once_with(timeout=3)
28 process.kill.assert_called_once_with()
29
30 # Log contains a contextual message.
31 assert capsys.readouterr().out == "Forcibly killing testing process...\n"