Debian Codegraph @

tests/test_stress_controller.py

raw symbols SCIP
1 """Tests for StressController: mode management and process lifecycle."""
2
3 from unittest.mock import MagicMock
4
5 from s_tui.s_tui import StressController
6
7
8 class TestStressControllerInit:
9 def test_monitor_and_builtin_always_available(self):
10 """Monitor and Built-in modes are always available."""
11 sc = StressController(stress_installed=False)
12 assert sc.get_modes() == ["Monitor", "s-tui stress"]
13 assert sc.get_current_mode() == "Monitor"
14
15 def test_stress_installed(self):
16 """With stress installed, Monitor, s-tui stress, and Stress (ext) are available."""
17 sc = StressController(stress_installed=True)
18 modes = sc.get_modes()
19 assert "Monitor" in modes
20 assert "s-tui stress" in modes
21 assert "Stress (ext)" in modes
22
23 def test_builtin_stresser_lazy_init(self):
24 """BuiltinStresser is not created until first access."""
25 sc = StressController(stress_installed=False)
26 assert sc._builtin_stresser is None
27 # Accessing the property triggers creation
28 stresser = sc.builtin_stresser
29 assert stresser is not None
30 assert sc._builtin_stresser is stresser
31
32
33 class TestStressControllerModes:
34 def test_set_mode(self):
35 """set_mode changes current_mode."""
36 sc = StressController(True)
37 sc.set_mode("Stress (ext)")
38 assert sc.get_current_mode() == "Stress (ext)"
39
40 def test_set_mode_builtin(self):
41 """Can switch to Built-in mode."""
42 sc = StressController(False)
43 sc.set_mode("s-tui stress")
44 assert sc.get_current_mode() == "s-tui stress"
45
46 def test_set_mode_back_to_monitor(self):
47 """Can switch back to Monitor."""
48 sc = StressController(True)
49 sc.set_mode("Stress (ext)")
50 sc.set_mode("Monitor")
51 assert sc.get_current_mode() == "Monitor"
52
53
54 class TestStressControllerProcess:
55 def test_stress_process_initially_none(self):
56 """No stress process running initially."""
57 sc = StressController(True)
58 assert sc.get_stress_process() is None
59
60 def test_set_stress_process(self):
61 """set_stress_process stores the process."""
62 sc = StressController(True)
63 mock_proc = MagicMock()
64 sc.set_stress_process(mock_proc)
65 assert sc.get_stress_process() is mock_proc
66
67 def test_kill_stress_process(self, mocker):
68 """kill_stress_process calls kill_child_processes and resets to None."""
69 mocker.patch("s_tui.s_tui.kill_child_processes")
70 sc = StressController(True)
71 mock_proc = MagicMock()
72 sc.set_stress_process(mock_proc)
73 sc.kill_stress_process()
74 assert sc.get_stress_process() is None
75
76 def test_kill_stress_process_no_such_process(self, mocker):
77 """kill_stress_process handles NoSuchProcess gracefully."""
78 import psutil
79
80 mocker.patch(
81 "s_tui.s_tui.kill_child_processes",
82 side_effect=psutil.NoSuchProcess(pid=12345),
83 )
84 sc = StressController(True)
85 sc.set_stress_process(MagicMock())
86 sc.kill_stress_process()
87 assert sc.get_stress_process() is None
88
89 def test_kill_stress_process_stops_builtin(self, mocker):
90 """kill_stress_process also stops the builtin stresser if initialized."""
91 mocker.patch("s_tui.s_tui.kill_child_processes")
92 sc = StressController(True)
93 sc._builtin_stresser = MagicMock()
94 sc.kill_stress_process()
95 sc._builtin_stresser.stop.assert_called_once()
96
97 def test_kill_stress_process_skips_builtin_if_not_initialized(self, mocker):
98 """kill_stress_process does not create builtin stresser just to stop it."""
99 mocker.patch("s_tui.s_tui.kill_child_processes")
100 sc = StressController(True)
101 sc.kill_stress_process()
102 assert sc._builtin_stresser is None
103
104 def test_start_stress(self, mocker):
105 """start_stress launches subprocess and stores psutil.Process."""
106 mock_popen = mocker.patch("subprocess.Popen")
107 mock_popen.return_value.pid = 99999
108 mock_psutil_proc = MagicMock()
109 mocker.patch("psutil.Process", return_value=mock_psutil_proc)
110
111 sc = StressController(True)
112 sc.start_stress(["stress", "-c", "4"])
113 assert sc.get_stress_process() is mock_psutil_proc
114
115 def test_start_stress_uses_new_session(self, mocker):
116 """start_stress passes start_new_session=True for process group isolation."""
117 mock_popen = mocker.patch("subprocess.Popen")
118 mock_popen.return_value.pid = 99999
119 mocker.patch("psutil.Process", return_value=MagicMock())
120
121 sc = StressController(True)
122 sc.start_stress(["stress", "-c", "4"])
123 _, kwargs = mock_popen.call_args
124 assert kwargs.get("start_new_session") is True
125
126 def test_start_stress_oserror(self, mocker):
127 """start_stress handles OSError gracefully."""
128 mocker.patch("subprocess.Popen", side_effect=OSError("not found"))
129 sc = StressController(True)
130 sc.start_stress(["stress", "-c", "4"])
131 # Should not crash; process remains None
132 assert sc.get_stress_process() is None
133
134 def test_start_builtin_stress(self):
135 """start_builtin_stress delegates to builtin_stresser.start."""
136 sc = StressController(False)
137 sc._builtin_stresser = MagicMock()
138 sc.start_builtin_stress(4)
139 sc._builtin_stresser.start.assert_called_once_with(4, strategy=None)
140
141 def test_start_builtin_stress_with_strategy(self):
142 """start_builtin_stress passes strategy to builtin_stresser."""
143 sc = StressController(False)
144 sc._builtin_stresser = MagicMock()
145 sc.start_builtin_stress(2, strategy="hashlib")
146 sc._builtin_stresser.start.assert_called_once_with(2, strategy="hashlib")
147
148 def test_start_builtin_stress_oserror_falls_back_to_monitor(self):
149 """start_builtin_stress falls back to Monitor on OSError (e.g. no /dev/shm)."""
150 sc = StressController(False)
151 sc.set_mode("s-tui stress")
152 sc._builtin_stresser = MagicMock()
153 sc._builtin_stresser.start.side_effect = PermissionError("Permission denied")
154 sc.start_builtin_stress(4)
155 assert sc.get_current_mode() == "Monitor"