Skip to content

Commit 3d62c9a

Browse files
authored
Make test_job_decorator tests timezone agnostic (#6153)
Running tests in UTC+2 timezone makes some of the tests fail because the mocked time in the future is actually in the past, as UTC is used as the new reference point. Adjust the tests to mock also the time when the first execution of function happens. Instances where the second execution happened "immediately" were mocked to happen 1ms later. The 1ms delta is also needed to be added when mocking time 1h in the future, otherwise it will be throttled too.
1 parent ef313d1 commit 3d62c9a

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

tests/jobs/test_job_decorator.py

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,21 @@ async def execute(self):
366366

367367
test = TestClass(coresys)
368368

369-
await asyncio.gather(*[test.execute(), test.execute()])
369+
start = utcnow()
370+
371+
with time_machine.travel(start):
372+
await asyncio.gather(*[test.execute(), test.execute()])
370373
assert test.call == 2
371374

372-
with pytest.raises(JobException if error is None else error):
375+
with (
376+
time_machine.travel(start + timedelta(milliseconds=1)),
377+
pytest.raises(JobException if error is None else error),
378+
):
373379
await test.execute()
374380

375381
assert test.call == 2
376382

377-
with time_machine.travel(utcnow() + timedelta(hours=1)):
383+
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
378384
await test.execute()
379385

380386
assert test.call == 3
@@ -830,15 +836,18 @@ async def execute(self, sleep: float):
830836
test1 = TestClass(coresys, "test1")
831837
test2 = TestClass(coresys, "test2")
832838

839+
start = utcnow()
840+
833841
# One call of each should work. The subsequent calls will be silently throttled due to period
834-
await asyncio.gather(
835-
test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)
836-
)
842+
with time_machine.travel(start):
843+
await asyncio.gather(
844+
test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)
845+
)
837846
assert test1.call == 1
838847
assert test2.call == 1
839848

840849
# First call to each will work again since period cleared. Second throttled once more as they don't wait
841-
with time_machine.travel(utcnow() + timedelta(milliseconds=100)):
850+
with time_machine.travel(start + timedelta(milliseconds=100)):
842851
await asyncio.gather(
843852
test1.execute(0.1),
844853
test1.execute(0.1),
@@ -878,15 +887,18 @@ async def execute(self, sleep: float):
878887
test1 = TestClass(coresys, "test1")
879888
test2 = TestClass(coresys, "test2")
880889

890+
start = utcnow()
891+
881892
# One call of each should work. The subsequent calls will be silently throttled after waiting due to period
882-
await asyncio.gather(
883-
*[test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)]
884-
)
893+
with time_machine.travel(start):
894+
await asyncio.gather(
895+
*[test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)]
896+
)
885897
assert test1.call == 1
886898
assert test2.call == 1
887899

888900
# All calls should work as we cleared the period. And tasks take longer then period and are queued
889-
with time_machine.travel(utcnow() + timedelta(milliseconds=100)):
901+
with time_machine.travel(start + timedelta(milliseconds=100)):
890902
await asyncio.gather(
891903
*[
892904
test1.execute(0.1),
@@ -927,21 +939,25 @@ async def execute(self):
927939
test1 = TestClass(coresys, "test1")
928940
test2 = TestClass(coresys, "test2")
929941

930-
await asyncio.gather(
931-
*[test1.execute(), test1.execute(), test2.execute(), test2.execute()]
932-
)
942+
start = utcnow()
943+
944+
with time_machine.travel(start):
945+
await asyncio.gather(
946+
*[test1.execute(), test1.execute(), test2.execute(), test2.execute()]
947+
)
933948
assert test1.call == 2
934949
assert test2.call == 2
935950

936-
with pytest.raises(JobException if error is None else error):
937-
await test1.execute()
938-
with pytest.raises(JobException if error is None else error):
939-
await test2.execute()
951+
with time_machine.travel(start + timedelta(milliseconds=1)):
952+
with pytest.raises(JobException if error is None else error):
953+
await test1.execute()
954+
with pytest.raises(JobException if error is None else error):
955+
await test2.execute()
940956

941957
assert test1.call == 2
942958
assert test2.call == 2
943959

944-
with time_machine.travel(utcnow() + timedelta(hours=1)):
960+
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
945961
await test1.execute()
946962
await test2.execute()
947963

@@ -1285,20 +1301,26 @@ async def execute(self, sleep: float = 0):
12851301

12861302
test = TestClass(coresys)
12871303

1288-
results = await asyncio.gather(
1289-
*[test.execute(0.1), test.execute(), test.execute()], return_exceptions=True
1290-
)
1304+
start = utcnow()
1305+
1306+
with time_machine.travel(start):
1307+
results = await asyncio.gather(
1308+
*[test.execute(0.1), test.execute(), test.execute()], return_exceptions=True
1309+
)
12911310
assert results[0] is None
12921311
assert isinstance(results[1], JobException)
12931312
assert isinstance(results[2], JobException)
12941313
assert test.call == 1
12951314

1296-
with pytest.raises(JobException if error is None else error):
1315+
with (
1316+
time_machine.travel(start + timedelta(milliseconds=1)),
1317+
pytest.raises(JobException if error is None else error),
1318+
):
12971319
await test.execute()
12981320

12991321
assert test.call == 1
13001322

1301-
with time_machine.travel(utcnow() + timedelta(hours=1)):
1323+
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
13021324
await test.execute()
13031325

13041326
assert test.call == 2
@@ -1342,18 +1364,22 @@ async def nested_method(self) -> None:
13421364

13431365
test = TestClass(coresys)
13441366

1367+
start = utcnow()
1368+
13451369
# First call should work
1346-
await test.main_method()
1370+
with time_machine.travel(start):
1371+
await test.main_method()
13471372
assert test.call_count == 1
13481373
assert test.nested_call_count == 1
13491374

13501375
# Second call should be throttled (not execute due to throttle period)
1351-
await test.main_method()
1376+
with time_machine.travel(start + timedelta(milliseconds=1)):
1377+
await test.main_method()
13521378
assert test.call_count == 1 # Still 1, throttled
13531379
assert test.nested_call_count == 1 # Still 1, throttled
13541380

13551381
# Wait for throttle period to pass and try again
1356-
with time_machine.travel(utcnow() + timedelta(milliseconds=60)):
1382+
with time_machine.travel(start + timedelta(milliseconds=60)):
13571383
await test.main_method()
13581384

13591385
assert test.call_count == 2 # Should execute now

0 commit comments

Comments
 (0)