Skip to content

Commit 27ceec4

Browse files
committed
Use a float for the tolerance in the timer tests
When using an `int`, we need to do a double conversion, first to `float` and then back to `int`, and due to rounding errors, this means there are inconsistencies between the expected and actual values. This is an example failure: ``` ______________________ test_policy_skip_missed_and_drift _______________________ @hypothesis.given( > tolerance=st.integers(min_value=0, max_value=_max_timedelta_microseconds), **_calculate_next_tick_time_args, ) tests/test_timer.py:148: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ tolerance = 171726190479152817, now = 171726190479152817 scheduled_tick_time = -1, interval = 1 @hypothesis.given( tolerance=st.integers(min_value=0, max_value=_max_timedelta_microseconds), **_calculate_next_tick_time_args, ) def test_policy_skip_missed_and_drift( tolerance: int, now: int, scheduled_tick_time: int, interval: int ) -> None: """Test the SkipMissedAndDrift policy.""" hypothesis.assume(now >= scheduled_tick_time) next_tick_time = SkipMissedAndDrift( delay_tolerance=timedelta(microseconds=tolerance) ).calculate_next_tick_time( now=now, interval=interval, scheduled_tick_time=scheduled_tick_time ) if tolerance < interval: assert next_tick_time > now drift = now - scheduled_tick_time if drift > tolerance: > assert next_tick_time == now + interval E assert 0 == (171726190479152817 + 1) E Falsifying example: test_policy_skip_missed_and_drift( E tolerance=171_726_190_479_152_817, E now=171_726_190_479_152_817, E scheduled_tick_time=-1, E interval=1, # or any other generated value E ) tests/test_timer.py:166: AssertionError ``` Using `float` directly ensures we are comparing the same values in the tests and in the code. Some explicit examples are now included in the hypothesis tests to ensure this issue is not reintroduced. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 18bb37d commit 27ceec4

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

tests/test_timer.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,51 @@ def test_policy_skip_missed_and_resync_examples() -> None:
132132

133133

134134
@hypothesis.given(
135-
tolerance=st.integers(min_value=_min_timedelta_microseconds, max_value=-1)
135+
tolerance=st.floats(
136+
min_value=timedelta.min.total_seconds(),
137+
max_value=-1,
138+
exclude_max=False,
139+
allow_nan=False,
140+
allow_infinity=False,
141+
),
136142
)
137-
def test_policy_skip_missed_and_drift_invalid_tolerance(tolerance: int) -> None:
143+
def test_policy_skip_missed_and_drift_invalid_tolerance(tolerance: float) -> None:
138144
"""Test the SkipMissedAndDrift policy raises an error for invalid tolerances."""
139145
with pytest.raises(ValueError, match="delay_tolerance must be positive"):
140146
SkipMissedAndDrift(delay_tolerance=timedelta(microseconds=tolerance))
141147

142148

143149
@hypothesis.given(
144-
tolerance=st.integers(min_value=0, max_value=_max_timedelta_microseconds),
150+
tolerance=st.floats(
151+
min_value=0,
152+
max_value=timedelta.max.total_seconds(),
153+
allow_nan=False,
154+
allow_infinity=False,
155+
),
145156
**_calculate_next_tick_time_args,
146157
)
158+
# We add some particular tests cases that were problematic in the past. See:
159+
# https://github.com/frequenz-floss/frequenz-channels-python/pull/347
160+
@hypothesis.example(
161+
tolerance=171726190479152832.0,
162+
now=171_726_190_479_152_817,
163+
scheduled_tick_time=-1,
164+
interval=1,
165+
)
166+
@hypothesis.example(
167+
tolerance=171726190479152830.0,
168+
now=171_726_190_479_152_817,
169+
scheduled_tick_time=-1,
170+
interval=1,
171+
)
172+
@hypothesis.example(
173+
tolerance=171726190479152831.0,
174+
now=171_726_190_479_152_817,
175+
scheduled_tick_time=-1,
176+
interval=1,
177+
)
147178
def test_policy_skip_missed_and_drift(
148-
tolerance: int, now: int, scheduled_tick_time: int, interval: int
179+
tolerance: float, now: int, scheduled_tick_time: int, interval: int
149180
) -> None:
150181
"""Test the SkipMissedAndDrift policy."""
151182
hypothesis.assume(now >= scheduled_tick_time)

0 commit comments

Comments
 (0)