Debian Codegraph @

tests/test_builtin_stresser.py

raw symbols SCIP
1 """Tests for BuiltinStresser: worker lifecycle and strategy selection."""
2
3 import time
4
5 from s_tui.builtin_stresser import (
6 _HAS_NUMPY,
7 STRATEGIES,
8 STRATEGY_HASHLIB,
9 STRATEGY_LABELS,
10 STRATEGY_NUMPY,
11 BuiltinStresser,
12 get_default_strategy,
13 strategy_available,
14 )
15
16
17 class TestBuiltinStresserLifecycle:
18 def test_start_and_stop(self):
19 """Workers start and are cleaned up after stop."""
20 stresser = BuiltinStresser()
21 stresser.start(2)
22 assert stresser.is_running()
23 stresser.stop(timeout=3)
24 assert not stresser.is_running()
25
26 def test_stop_when_not_started(self):
27 """Calling stop without start is safe (no-op)."""
28 stresser = BuiltinStresser()
29 stresser.stop() # should not raise
30 assert not stresser.is_running()
31
32 def test_double_stop(self):
33 """Calling stop twice is safe."""
34 stresser = BuiltinStresser()
35 stresser.start(1)
36 stresser.stop(timeout=3)
37 stresser.stop(timeout=3) # should not raise
38 assert not stresser.is_running()
39
40 def test_restart(self):
41 """Starting after a stop works correctly."""
42 stresser = BuiltinStresser()
43 stresser.start(1)
44 stresser.stop(timeout=3)
45 stresser.start(1)
46 assert stresser.is_running()
47 stresser.stop(timeout=3)
48 assert not stresser.is_running()
49
50 def test_start_cleans_previous(self):
51 """Calling start while running stops previous workers first."""
52 stresser = BuiltinStresser()
53 stresser.start(1)
54 stresser.start(2) # should stop first, then start 2
55 # Allow brief time for processes to spawn
56 time.sleep(0.1)
57 assert stresser.is_running()
58 stresser.stop(timeout=3)
59
60
61 class TestStrategySelection:
62 def test_default_strategy_is_valid(self):
63 """get_default_strategy returns one of the known keys."""
64 assert get_default_strategy() in STRATEGIES
65
66 def test_default_strategy_matches_numpy_availability(self):
67 """Default is numpy when available, hashlib otherwise."""
68 if _HAS_NUMPY:
69 assert get_default_strategy() == STRATEGY_NUMPY
70 else:
71 assert get_default_strategy() == STRATEGY_HASHLIB
72
73 def test_hashlib_always_available(self):
74 """hashlib strategy is always available."""
75 assert strategy_available(STRATEGY_HASHLIB)
76
77 def test_numpy_availability_matches_import(self):
78 """numpy strategy availability matches whether numpy can be imported."""
79 assert strategy_available(STRATEGY_NUMPY) == _HAS_NUMPY
80
81 def test_all_strategies_have_labels(self):
82 """Every strategy key has a human-readable label."""
83 for key in STRATEGIES:
84 assert key in STRATEGY_LABELS
85 assert len(STRATEGY_LABELS[key]) > 0
86
87 def test_start_with_explicit_hashlib(self):
88 """Can start workers with explicit hashlib strategy."""
89 stresser = BuiltinStresser()
90 stresser.start(1, strategy=STRATEGY_HASHLIB)
91 assert stresser.is_running()
92 stresser.stop(timeout=3)
93
94 def test_start_with_unavailable_strategy_falls_back(self, mocker):
95 """Requesting unavailable strategy falls back to hashlib."""
96 mocker.patch("s_tui.builtin_stresser.strategy_available", return_value=False)
97 mock_warn = mocker.patch("s_tui.builtin_stresser.logging.warning")
98 stresser = BuiltinStresser()
99 stresser.start(1, strategy=STRATEGY_NUMPY)
100 assert stresser.is_running()
101 mock_warn.assert_called_once()
102 assert "falling back to hashlib" in mock_warn.call_args[0][0]
103 stresser.stop(timeout=3)