Commit 27ceec4
committed
Use a
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]>float for the tolerance in the timer tests1 parent 18bb37d commit 27ceec4
1 file changed
+35
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
136 | 142 | | |
137 | | - | |
| 143 | + | |
138 | 144 | | |
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
142 | 148 | | |
143 | 149 | | |
144 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
145 | 156 | | |
146 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
147 | 178 | | |
148 | | - | |
| 179 | + | |
149 | 180 | | |
150 | 181 | | |
151 | 182 | | |
| |||
0 commit comments