Skip to content

Commit

Permalink
Mock time to ensure reliable results on macOS CI (#1668)
Browse files Browse the repository at this point in the history
Timing tests are notoriously unreliable (see
python-trio/trio#1851) and were failing during
my tests using macOS on GitHub Actions.
  • Loading branch information
pquentin authored Feb 15, 2023
1 parent 590dad1 commit 8343a17
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions tests/time_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

import time
from unittest import mock

import esrally.time

Expand All @@ -36,30 +37,20 @@ def test_split_time_increases(self):
total_time = stop_watch.total_time()
assert prev_split_time <= total_time

def test_total_time_roughly_in_expected_range(self):
wait_period_seconds = 0.05
acceptable_delta_seconds = 0.03

@mock.patch("esrally.time.StopWatch._now", side_effect=[1, 2])
def test_total_time_roughly_in_expected_range(self, mock_now):
stop_watch = esrally.time.Clock.stop_watch()
stop_watch.start()
time.sleep(wait_period_seconds)
stop_watch.stop()

interval = stop_watch.total_time()
# depending on scheduling accuracy we should end up somewhere in that range
assert interval >= wait_period_seconds - acceptable_delta_seconds
assert interval <= wait_period_seconds + acceptable_delta_seconds

def test_millis_conversion_roughly_in_expected_range(self):
wait_period_millis = 50
acceptable_delta_millis = 30
assert mock_now.call_count == 2
assert interval == 2 - 1

@mock.patch("esrally.time.Clock.now", side_effect=[1, 2])
def test_millis_conversion_roughly_in_expected_range(self, mock_now):
start = esrally.time.to_epoch_millis(esrally.time.Clock.now())
time.sleep(wait_period_millis / 1000.0)
end = esrally.time.to_epoch_millis(esrally.time.Clock.now())

interval_millis = end - start

# depending on scheduling accuracy we should end up somewhere in that range
assert interval_millis >= wait_period_millis - acceptable_delta_millis
assert interval_millis <= wait_period_millis + acceptable_delta_millis
assert end - start == 2000 - 1000
assert mock_now.call_count == 2

0 comments on commit 8343a17

Please sign in to comment.