| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
(100.0, 60.0, 50.0, 120.0, 0.0), |
| 51 |
|
| 52 |
(80.0, 10.0, 82.0, 20.0, 84.0), |
| 53 |
|
| 54 |
|
| 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 |
|
| 72 |
(80.0, 100.0, 10.0, 2.0), |
| 73 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
h |
| 93 |
ll). |
| 94 |
(400.0, 59.0, 100.0, 119.0), |
| 95 |
|
| 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 |
l. |
| 118 |
(50.0, 110.0, 60.0, True), |
| 119 |
|
| 120 |
(50.0, 109.0, 60.0, True), |
| 121 |
|
| 122 |
(50.0, 108.0, 60.0, True), |
| 123 |
|
| 124 |
|
| 125 |
(49.0, 54.0, 60.0, False), |
| 126 |
|
| 127 |
(100.0, 5.0, 60.0, False), |
| 128 |
|
| 129 |
|
| 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 |
|
| 144 |
assert not is_same_pid(50.0, 105.0, 60.0) |
| 145 |
|
| 146 |
assert is_same_pid(50.0, 105.0, 60.0, tolerance=6.0) |