Debian Codegraph @

test/duct_main/test_duct_utils.py

raw symbols SCIP
1 """Tests for utility functions in _duct_main.py"""
2
3 import pytest
4 from con_duct._utils import (
5 assert_num,
6 etime_to_etimes,
7 is_same_pid,
8 pdcpu_from_pcpu,
9 )
10
11
12 @pytest.mark.parametrize("input_value", [0, 1, 2, -1, 100, 0.001, -1.68])
13 def test_assert_num_green(input_value: int) -> None:
14 assert_num(input_value)
15
16
17 @pytest.mark.parametrize("input_value", ["hi", "0", "one"])
18 def test_assert_num_red(input_value: int) -> None:
19 with pytest.raises(AssertionError):
20 assert_num(input_value)
21
22
23 @pytest.mark.parametrize(
24 "etime,expected",
25 [
26 ("00:42", 42.0),
27 ("01:30", 90.0),
28 ("01:00:00", 3600.0),
29 ("12:34:56", 12 * 3600 + 34 * 60 + 56),
30 ("02-03:04:05", 2 * 86400 + 3 * 3600 + 4 * 60 + 5),
31 ("100-00:00:00", 100 * 86400.0),
32 ],
33 )
34 def test_etime_to_etimes_green(etime: str, expected: float) -> None:
35 assert etime_to_etimes(etime) == expected
36
37
38 @pytest.mark.parametrize("etime", ["", "garbage", "12", "1:2:3:4", "ab:cd"])
39 def test_etime_to_etimes_red(etime: str) -> None:
40 with pytest.raises(ValueError):
41 etime_to_etimes(etime)
42
43
44 @pytest.mark.parametrize(
45 "prev_pcpu,prev_etimes,curr_pcpu,curr_etimes,expected",
46 [
47 # Motivating con/duct#399 case: 100% for 60s then idle for 60s.
48 # Lifetime pcpu still reads 50% at t=120 (60 cputime / 120
49 # etime), but the corrected reading is 0%.
50 (100.0, 60.0, 50.0, 120.0, 0.0),
51 # Constant 84% load across a 10s interval.
52 (80.0, 10.0, 82.0, 20.0, 84.0),
53 # Pid that ramps from 50% lifetime to 75% lifetime over 100s
54 # of new wall time -> 100% during the new interval.
55 (50.0, 100.0, 75.0, 200.0, 100.0),
56 ],
57 )
58 def test_pdcpu_from_pcpu_green(
59 prev_pcpu: float,
60 prev_etimes: float,
61 curr_pcpu: float,
62 curr_etimes: float,
63 expected: float,
64 ) -> None:
65 assert pdcpu_from_pcpu(prev_pcpu, prev_etimes, curr_pcpu, curr_etimes) == expected
66
67
68 @pytest.mark.parametrize(
69 "prev_pcpu,prev_etimes,curr_pcpu,curr_etimes",
70 [
71 # etimes regressed -> suspected pid reuse; defensive guard.
72 (80.0, 100.0, 10.0, 2.0),
73 # Same instant -> interval is zero, no rate definable.
74 (50.0, 100.0, 50.0, 100.0),
75 ],
76 )
77 def test_pdcpu_from_pcpu_returns_none_when_no_interval(
78 prev_pcpu: float,
79 prev_etimes: float,
80 curr_pcpu: float,
81 curr_etimes: float,
82 ) -> None:
83 assert pdcpu_from_pcpu(prev_pcpu, prev_etimes, curr_pcpu, curr_etimes) is None
84
85
86 @pytest.mark.parametrize(
87 "prev_pcpu,prev_etimes,curr_pcpu,curr_etimes",
88 [
89 # Aggregation-timing skew: max pcpu in prev was captured early in
90 # the prev interval (pcpu_max=400 on a 4-core box, then idle).
91 # Curr's max happens late, so curr's pcpu*etime estimate of cputime
92 # is much smaller than prev's. Δcputime goes negative even though
93 # the pid is continuous (Δetime ≈ Δwall).
94 (400.0, 59.0, 100.0, 119.0),
95 # Smaller version of the same effect.
96 (50.0, 60.0, 5.0, 120.0),
97 ],
98 )
99 def test_pdcpu_from_pcpu_returns_none_on_negative_result(
100 prev_pcpu: float,
101 prev_etimes: float,
102 curr_pcpu: float,
103 curr_etimes: float,
104 ) -> None:
105 """Negative pdcpu == aggregation-timing artifact (not pid reuse).
106
107 See is_same_pid for identity; this clamp catches the residual case
108 where identity holds but the max-vs-end-etime mismatch in duct's
109 aggregated records produces a spurious negative delta.
110 """
111 assert pdcpu_from_pcpu(prev_pcpu, prev_etimes, curr_pcpu, curr_etimes) is None
112
113
114 @pytest.mark.parametrize(
115 "prev_etimes,curr_etimes,wall_delta,expected",
116 [
117 # Continuous pid: Δetime exactly matches Δwall.
118 (50.0, 110.0, 60.0, True),
119 # 1-second slack from ps integer rounding -- still same pid.
120 (50.0, 109.0, 60.0, True),
121 # 2-second slack at the tolerance boundary -- still same pid.
122 (50.0, 108.0, 60.0, True),
123 # Concrete con/duct#399 case: pid 3323259 went 49->54 over 60s of
124 # wall time. 5 << 58. Definitely a different physical process.
125 (49.0, 54.0, 60.0, False),
126 # etime regressed -- obvious reuse.
127 (100.0, 5.0, 60.0, False),
128 # Sub-quantum: same instant, no time elapsed for either etime or
129 # wall -- treat as continuous (no reuse signal).
130 (50.0, 50.0, 0.0, True),
131 ],
132 )
133 def test_is_same_pid(
134 prev_etimes: float,
135 curr_etimes: float,
136 wall_delta: float,
137 expected: bool,
138 ) -> None:
139 assert is_same_pid(prev_etimes, curr_etimes, wall_delta) is expected
140
141
142 def test_is_same_pid_tolerance_kwarg() -> None:
143 # 5s gap inside default tolerance? No (default is 2s).
144 assert not is_same_pid(50.0, 105.0, 60.0)
145 # ...but it is inside a 6s tolerance.
146 assert is_same_pid(50.0, 105.0, 60.0, tolerance=6.0)