Skip to content

Commit

Permalink
Wrap sleep calls in "sleep_one_cycle" function
Browse files Browse the repository at this point in the history
  • Loading branch information
matoro committed Nov 10, 2023
1 parent b489a47 commit 9e84584
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
)


def sleep_one_cycle(clock: int) -> None:
time.sleep(time.clock_getres(clock))


@contextmanager
def change_local_timezone(local_tz: str | None) -> typing.Iterator[None]:
orig_tz = os.environ["TZ"]
Expand Down Expand Up @@ -155,10 +159,10 @@ def test_time_clock_gettime_realtime():
@py_have_clock_gettime
def test_time_clock_gettime_monotonic_unaffected():
start = time.clock_gettime(time.CLOCK_MONOTONIC)
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
with time_machine.travel(EPOCH + 180.0):
frozen = time.clock_gettime(time.CLOCK_MONOTONIC)
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert isinstance(frozen, float)
assert frozen > start

Expand All @@ -171,7 +175,7 @@ def test_time_clock_gettime_monotonic_unaffected():
def test_time_clock_gettime_ns_realtime():
with time_machine.travel(EPOCH + 190.0):
first = time.clock_gettime_ns(time.CLOCK_REALTIME)
time.sleep(time.clock_getres(time.CLOCK_REALTIME))
sleep_one_cycle(time.CLOCK_REALTIME)
assert isinstance(first, int)
assert first == int((EPOCH + 190.0) * NANOSECONDS_PER_SECOND)
second = time.clock_gettime_ns(time.CLOCK_REALTIME)
Expand All @@ -185,10 +189,10 @@ def test_time_clock_gettime_ns_realtime():
@py_have_clock_gettime
def test_time_clock_gettime_ns_monotonic_unaffected():
start = time.clock_gettime_ns(time.CLOCK_MONOTONIC)
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
with time_machine.travel(EPOCH + 190.0):
frozen = time.clock_gettime_ns(time.CLOCK_MONOTONIC)
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert isinstance(frozen, int)
assert frozen > start

Expand Down Expand Up @@ -284,7 +288,7 @@ def test_time_strftime_format_t():
def test_time_time():
with time_machine.travel(EPOCH):
first = time.time()
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert isinstance(first, float)
assert first == EPOCH
second = time.time()
Expand All @@ -306,7 +310,7 @@ def test_time_time():
def test_time_time_windows():
with time_machine.travel(EPOCH):
first = time.time()
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert isinstance(first, float)
assert first == windows_epoch_in_posix

Expand All @@ -323,7 +327,7 @@ def test_time_time_no_tick():
def test_time_time_ns():
with time_machine.travel(EPOCH + 150.0):
first = time.time_ns()
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert isinstance(first, int)
assert first == int((EPOCH + 150.0) * NANOSECONDS_PER_SECOND)
second = time.time_ns()
Expand Down Expand Up @@ -569,7 +573,7 @@ def test_method_decorator(self):
@time_machine.travel(EPOCH + 95.0)
class UnitTestClassTests(TestCase):
def test_class_decorator(self):
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert EPOCH + 95.0 < time.time() < EPOCH + 96.0

@time_machine.travel(EPOCH + 25.0)
Expand All @@ -587,7 +591,7 @@ def setUpClass(cls):
cls.custom_setupclass_ran = True

def test_class_decorator(self):
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert EPOCH + 95.0 < time.time() < EPOCH + 96.0
assert self.custom_setupclass_ran

Expand Down Expand Up @@ -649,7 +653,7 @@ def test_move_to_datetime():
traveller.move_to(EPOCH_PLUS_ONE_YEAR_DATETIME)

first = time.time()
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert first == EPOCH_PLUS_ONE_YEAR

second = time.time()
Expand Down Expand Up @@ -717,7 +721,7 @@ def test_move_to_datetime_change_tick_on():
with time_machine.travel(EPOCH, tick=False) as traveller:
traveller.move_to(EPOCH_PLUS_ONE_YEAR_DATETIME, tick=True)
assert time.time() == EPOCH_PLUS_ONE_YEAR
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert time.time() > EPOCH_PLUS_ONE_YEAR


Expand Down Expand Up @@ -768,7 +772,7 @@ def test_fixture_used_tick_false(time_machine):
def test_fixture_used_tick_true(time_machine):
time_machine.move_to(EPOCH, tick=True)
original = time.time()
time.sleep(time.clock_getres(time.CLOCK_MONOTONIC))
sleep_one_cycle(time.CLOCK_MONOTONIC)
assert original == EPOCH
assert original < time.time() < EPOCH + 10.0

Expand Down

0 comments on commit 9e84584

Please sign in to comment.