| 1 |
"""Tests for FreqSource with mocked psutil.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from s_tui.sources.freq_source import FreqSource, _read_throttle_count |
| 6 |
|
| 7 |
|
| 8 |
def _make_fake_throttle_reader( |
| 9 |
throttle_core: int = 0, |
| 10 |
core_base: int = 100, |
| 11 |
pkg_base: int = 200, |
| 12 |
pkg_throttle: bool = False, |
| 13 |
): |
| 14 |
"""Factory for sysfs throttle counter mocks. |
| 15 |
|
| 16 |
Returns a callable suitable for ``mocker.patch(..., side_effect=...)``. |
| 17 |
By default, core ``throttle_core`` increases its ``core_throttle_count`` |
| 18 |
by 1 on the second read (simulating throttle on first update). |
| 19 |
Set ``pkg_throttle=True`` to simulate a package throttle instead. |
| 20 |
""" |
| 21 |
call_counts: dict[str, int] = {} |
| 22 |
|
| 23 |
def fake_read(core_id, counter): |
| 24 |
if counter == "core_throttle_count": |
| 25 |
if not pkg_throttle and core_id == throttle_core: |
| 26 |
key = f"core_{core_id}" |
| 27 |
call_counts[key] = call_counts.get(key, 0) + 1 |
| 28 |
return core_base if call_counts[key] <= 1 else core_base + 1 |
| 29 |
return core_base |
| 30 |
if counter == "package_throttle_count": |
| 31 |
if pkg_throttle: |
| 32 |
call_counts["pkg"] = call_counts.get("pkg", 0) + 1 |
| 33 |
return pkg_base if call_counts["pkg"] <= 1 else pkg_base + 1 |
| 34 |
return pkg_base |
| 35 |
return None |
| 36 |
|
| 37 |
return fake_read |
| 38 |
|
| 39 |
|
| 40 |
class TestFreqSourceInit: |
| 41 |
def test_name(self, mock_cpu_freq): |
| 42 |
src = FreqSource() |
| 43 |
assert src.get_source_name() == "Frequency" |
| 44 |
|
| 45 |
def test_measurement_unit(self, mock_cpu_freq): |
| 46 |
src = FreqSource() |
| 47 |
assert src.get_measurement_unit() == "MHz" |
| 48 |
|
| 49 |
def test_is_available(self, mock_cpu_freq): |
| 50 |
src = FreqSource() |
| 51 |
assert src.get_is_available() is True |
| 52 |
|
| 53 |
def test_sensor_list(self, mock_cpu_freq): |
| 54 |
src = FreqSource() |
| 55 |
sensors = src.get_sensor_list() |
| 56 |
assert sensors[0] == "Avg" |
| 57 |
assert len(sensors) == 5 |
| 58 |
|
| 59 |
def test_top_freq(self, mock_cpu_freq): |
| 60 |
src = FreqSource() |
| 61 |
assert src.get_top() == 3600.0 |
| 62 |
|
| 63 |
def test_max_freq(self, mock_cpu_freq): |
| 64 |
src = FreqSource() |
| 65 |
assert src.get_maximum() == 3600.0 |
| 66 |
|
| 67 |
def test_pallet(self, mock_cpu_freq): |
| 68 |
src = FreqSource() |
| 69 |
assert "freq" in src.get_pallet()[0] |
| 70 |
|
| 71 |
|
| 72 |
class TestFreqSourceUpdate: |
| 73 |
def test_update_populates_values(self, mock_cpu_freq): |
| 74 |
src = FreqSource() |
| 75 |
src.update() |
| 76 |
readings = src.get_reading_list() |
| 77 |
|
| 78 |
assert readings[0] == pytest.approx(2400.0) |
| 79 |
assert all(r == pytest.approx(2400.0) for r in readings[1:]) |
| 80 |
|
| 81 |
def test_update_summary(self, mock_cpu_freq): |
| 82 |
src = FreqSource() |
| 83 |
src.update() |
| 84 |
summary = src.get_sensors_summary() |
| 85 |
assert "Avg" in summary |
| 86 |
assert "Core 0" in summary |
| 87 |
|
| 88 |
assert summary["Avg"] == "2400" |
| 89 |
|
| 90 |
|
| 91 |
class TestFreqSourceThrottle: |
| 92 |
"""Tests for sysfs-based throttle detection in FreqSource.""" |
| 93 |
|
| 94 |
def test_no_throttle_by_default(self, mock_cpu_freq, mocker): |
| 95 |
"""When sysfs is unavailable, no throttle suffixes should be set.""" |
| 96 |
mocker.patch( |
| 97 |
"s_tui.sources.freq_source._read_throttle_count", return_value=None |
| 98 |
) |
| 99 |
mocker.patch( |
| 100 |
"s_tui.sources.freq_source.intel_therm.available", return_value=False |
| 101 |
) |
| 102 |
src = FreqSource() |
| 103 |
src.update() |
| 104 |
suffixes = src.get_sensor_suffixes() |
| 105 |
assert all(s == "" for s in suffixes) |
| 106 |
|
| 107 |
def test_no_throttle_alerts_by_default(self, mock_cpu_freq, mocker): |
| 108 |
"""When not throttled, all sensor alerts should be None.""" |
| 109 |
mocker.patch( |
| 110 |
"s_tui.sources.freq_source._read_throttle_count", return_value=None |
| 111 |
) |
| 112 |
mocker.patch( |
| 113 |
"s_tui.sources.freq_source.intel_therm.available", return_value=False |
| 114 |
) |
| 115 |
src = FreqSource() |
| 116 |
src.update() |
| 117 |
alerts = src.get_sensor_alerts() |
| 118 |
assert all(a is None for a in alerts) |
| 119 |
|
| 120 |
def test_edge_triggered_always_false(self, mock_cpu_freq): |
| 121 |
"""FreqSource uses per-sensor thresholds, not global edge trigger.""" |
| 122 |
src = FreqSource() |
| 123 |
assert src.get_edge_triggered() is False |
| 124 |
|
| 125 |
def test_alert_pallet_set(self, mock_cpu_freq): |
| 126 |
"""FreqSource should have an alert pallet for throttle coloring.""" |
| 127 |
src = FreqSource() |
| 128 |
assert src.get_alert_pallet() is not None |
| 129 |
assert "throttle" in src.get_alert_pallet()[0] |
| 130 |
|
| 131 |
def test_core_throttle_detected(self, mock_cpu_freq, mocker): |
| 132 |
"""When core_throttle_count increases, core gets 'Tc' suffix.""" |
| 133 |
mocker.patch( |
| 134 |
"s_tui.sources.freq_source._read_throttle_count", |
| 135 |
side_effect=_make_fake_throttle_reader(), |
| 136 |
) |
| 137 |
src = FreqSource() |
| 138 |
src.update() |
| 139 |
suffixes = src.get_sensor_suffixes() |
| 140 |
assert "Tc" in suffixes[0] |
| 141 |
assert "Tc" in suffixes[1] |
| 142 |
assert suffixes[2] == "" |
| 143 |
|
| 144 |
def test_core_throttle_sets_threshold(self, mock_cpu_freq, mocker): |
| 145 |
"""Throttled core should have threshold 0.0 to trigger alert colors.""" |
| 146 |
mocker.patch( |
| 147 |
"s_tui.sources.freq_source._read_throttle_count", |
| 148 |
side_effect=_make_fake_throttle_reader(), |
| 149 |
) |
| 150 |
src = FreqSource() |
| 151 |
src.update() |
| 152 |
|
| 153 |
assert src.last_thresholds[1] == 0.0 |
| 154 |
|
| 155 |
assert src.last_thresholds[2] is None |
| 156 |
|
| 157 |
assert src.last_thresholds[0] == 0.0 |
| 158 |
|
| 159 |
def test_core_throttle_sets_alert(self, mock_cpu_freq, mocker): |
| 160 |
"""Throttled core should have alert attribute for summary coloring.""" |
| 161 |
mocker.patch( |
| 162 |
"s_tui.sources.freq_source._read_throttle_count", |
| 163 |
side_effect=_make_fake_throttle_reader(), |
| 164 |
) |
| 165 |
src = FreqSource() |
| 166 |
src.update() |
| 167 |
alerts = src.get_sensor_alerts() |
| 168 |
assert alerts[0] == "throttle txt" |
| 169 |
assert alerts[1] == "throttle txt" |
| 170 |
assert alerts[2] is None |
| 171 |
|
| 172 |
def test_package_throttle_detected(self, mock_cpu_freq, mocker): |
| 173 |
"""When package_throttle_count increases, all cores show 'Tp'.""" |
| 174 |
mocker.patch( |
| 175 |
"s_tui.sources.freq_source._read_throttle_count", |
| 176 |
side_effect=_make_fake_throttle_reader(pkg_throttle=True), |
| 177 |
) |
| 178 |
src = FreqSource() |
| 179 |
src.update() |
| 180 |
suffixes = src.get_sensor_suffixes() |
| 181 |
assert suffixes[0] == "Tp" |
| 182 |
assert suffixes[1] == "Tp" |
| 183 |
|
| 184 |
def test_throttle_clears_after_interval(self, mock_cpu_freq, mocker): |
| 185 |
"""When counts stop increasing, throttle indicators disappear.""" |
| 186 |
mocker.patch( |
| 187 |
"s_tui.sources.freq_source._read_throttle_count", |
| 188 |
side_effect=_make_fake_throttle_reader(), |
| 189 |
) |
| 190 |
src = FreqSource() |
| 191 |
src.update() |
| 192 |
assert src.last_thresholds[1] == 0.0 |
| 193 |
src.update() |
| 194 |
assert src.last_thresholds[1] is None |
| 195 |
|
| 196 |
def test_sysfs_unavailable_graceful(self, mock_cpu_freq, mocker): |
| 197 |
"""When sysfs files don't exist, throttle detection is disabled.""" |
| 198 |
mocker.patch( |
| 199 |
"s_tui.sources.freq_source._read_throttle_count", |
| 200 |
return_value=None, |
| 201 |
) |
| 202 |
mocker.patch( |
| 203 |
"s_tui.sources.freq_source.intel_therm.available", return_value=False |
| 204 |
) |
| 205 |
src = FreqSource() |
| 206 |
assert src._throttle_available is False |
| 207 |
src.update() |
| 208 |
|
| 209 |
summary = src.get_sensors_summary() |
| 210 |
for val in summary.values(): |
| 211 |
assert "Tc" not in val |
| 212 |
assert "Tp" not in val |
| 213 |
|
| 214 |
|
| 215 |
class TestReadThrottleCount: |
| 216 |
"""Tests for the _read_throttle_count helper.""" |
| 217 |
|
| 218 |
def test_reads_sysfs_value(self, tmp_path): |
| 219 |
"""Reads an integer from a sysfs-like file.""" |
| 220 |
throttle_dir = tmp_path / "cpu0" / "thermal_throttle" |
| 221 |
throttle_dir.mkdir(parents=True) |
| 222 |
(throttle_dir / "core_throttle_count").write_text("42\n") |
| 223 |
|
| 224 |
import s_tui.sources.freq_source as mod |
| 225 |
|
| 226 |
orig = mod.SYSFS_THERMAL_THROTTLE |
| 227 |
mod.SYSFS_THERMAL_THROTTLE = str(tmp_path / "cpu{}" / "thermal_throttle") |
| 228 |
try: |
| 229 |
assert _read_throttle_count(0, "core_throttle_count") == 42 |
| 230 |
finally: |
| 231 |
mod.SYSFS_THERMAL_THROTTLE = orig |
| 232 |
|
| 233 |
def test_returns_none_on_missing(self): |
| 234 |
"""Returns None for non-existent files.""" |
| 235 |
assert _read_throttle_count(999, "core_throttle_count") is None |
| 236 |
|
| 237 |
|
| 238 |
class TestFreqSourceMsrThrottle: |
| 239 |
"""Tests for MSR-based throttle detection in FreqSource.""" |
| 240 |
|
| 241 |
def test_msr_label_shown_when_available(self, mock_cpu_freq, mocker): |
| 242 |
"""When MSR is available, per-core labels come from IA32_THERM_STATUS.""" |
| 243 |
from s_tui.sources.intel_therm import ThrottleStatus |
| 244 |
|
| 245 |
mocker.patch( |
| 246 |
"s_tui.sources.freq_source.intel_therm.available", return_value=True |
| 247 |
) |
| 248 |
mocker.patch( |
| 249 |
"s_tui.sources.freq_source._read_throttle_count", return_value=None |
| 250 |
) |
| 251 |
src = FreqSource() |
| 252 |
assert src._use_msr is True |
| 253 |
|
| 254 |
|
| 255 |
status_tw = ThrottleStatus(True, False, False, True, False, False) |
| 256 |
status_none = ThrottleStatus(False, False, False, False, False, False) |
| 257 |
|
| 258 |
def fake_read(cpu): |
| 259 |
return status_tw if cpu == 0 else status_none |
| 260 |
|
| 261 |
mocker.patch( |
| 262 |
"s_tui.sources.freq_source.intel_therm.read_therm_status", |
| 263 |
side_effect=fake_read, |
| 264 |
) |
| 265 |
src.update() |
| 266 |
suffixes = src.get_sensor_suffixes() |
| 267 |
assert suffixes[0] == "T/W" |
| 268 |
assert suffixes[1] == "T/W" |
| 269 |
assert suffixes[2] == "" |
| 270 |
|
| 271 |
def test_msr_sets_alerts(self, mock_cpu_freq, mocker): |
| 272 |
"""MSR throttle labels trigger alert coloring.""" |
| 273 |
from s_tui.sources.intel_therm import ThrottleStatus |
| 274 |
|
| 275 |
mocker.patch( |
| 276 |
"s_tui.sources.freq_source.intel_therm.available", return_value=True |
| 277 |
) |
| 278 |
mocker.patch( |
| 279 |
"s_tui.sources.freq_source._read_throttle_count", return_value=None |
| 280 |
) |
| 281 |
src = FreqSource() |
| 282 |
|
| 283 |
status_w = ThrottleStatus(False, False, False, True, False, False) |
| 284 |
mocker.patch( |
| 285 |
"s_tui.sources.freq_source.intel_therm.read_therm_status", |
| 286 |
return_value=status_w, |
| 287 |
) |
| 288 |
src.update() |
| 289 |
alerts = src.get_sensor_alerts() |
| 290 |
assert alerts[0] == "throttle txt" |
| 291 |
assert alerts[1] == "throttle txt" |
| 292 |
|
| 293 |
def test_msr_oserror_clears_label(self, mock_cpu_freq, mocker): |
| 294 |
"""If MSR read fails for a core, its label is cleared.""" |
| 295 |
mocker.patch( |
| 296 |
"s_tui.sources.freq_source.intel_therm.available", return_value=True |
| 297 |
) |
| 298 |
mocker.patch( |
| 299 |
"s_tui.sources.freq_source._read_throttle_count", return_value=None |
| 300 |
) |
| 301 |
src = FreqSource() |
| 302 |
mocker.patch( |
| 303 |
"s_tui.sources.freq_source.intel_therm.read_therm_status", |
| 304 |
side_effect=OSError("permission denied"), |
| 305 |
) |
| 306 |
src.update() |
| 307 |
assert all(label == "" for label in src._throttle_labels) |
| 308 |
|
| 309 |
def test_falls_back_to_sysfs(self, mock_cpu_freq, mocker): |
| 310 |
"""When MSR unavailable, sysfs detection still works.""" |
| 311 |
mocker.patch( |
| 312 |
"s_tui.sources.freq_source.intel_therm.available", return_value=False |
| 313 |
) |
| 314 |
mocker.patch( |
| 315 |
"s_tui.sources.freq_source._read_throttle_count", |
| 316 |
side_effect=_make_fake_throttle_reader(), |
| 317 |
) |
| 318 |
src = FreqSource() |
| 319 |
assert src._use_msr is False |
| 320 |
src.update() |
| 321 |
suffixes = src.get_sensor_suffixes() |
| 322 |
assert "Tc" in suffixes[1] |