Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock time to ensure reliable results on macOS CI #1668

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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