mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-09 02:49:43 +00:00
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.
This commit is contained in:
@@ -366,15 +366,21 @@ async def test_throttle_rate_limit(coresys: CoreSys, error: JobException | None)
|
||||
|
||||
test = TestClass(coresys)
|
||||
|
||||
start = utcnow()
|
||||
|
||||
with time_machine.travel(start):
|
||||
await asyncio.gather(*[test.execute(), test.execute()])
|
||||
assert test.call == 2
|
||||
|
||||
with pytest.raises(JobException if error is None else error):
|
||||
with (
|
||||
time_machine.travel(start + timedelta(milliseconds=1)),
|
||||
pytest.raises(JobException if error is None else error),
|
||||
):
|
||||
await test.execute()
|
||||
|
||||
assert test.call == 2
|
||||
|
||||
with time_machine.travel(utcnow() + timedelta(hours=1)):
|
||||
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
|
||||
await test.execute()
|
||||
|
||||
assert test.call == 3
|
||||
@@ -830,7 +836,10 @@ async def test_group_throttle(coresys: CoreSys):
|
||||
test1 = TestClass(coresys, "test1")
|
||||
test2 = TestClass(coresys, "test2")
|
||||
|
||||
start = utcnow()
|
||||
|
||||
# One call of each should work. The subsequent calls will be silently throttled due to period
|
||||
with time_machine.travel(start):
|
||||
await asyncio.gather(
|
||||
test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)
|
||||
)
|
||||
@@ -838,7 +847,7 @@ async def test_group_throttle(coresys: CoreSys):
|
||||
assert test2.call == 1
|
||||
|
||||
# First call to each will work again since period cleared. Second throttled once more as they don't wait
|
||||
with time_machine.travel(utcnow() + timedelta(milliseconds=100)):
|
||||
with time_machine.travel(start + timedelta(milliseconds=100)):
|
||||
await asyncio.gather(
|
||||
test1.execute(0.1),
|
||||
test1.execute(0.1),
|
||||
@@ -878,7 +887,10 @@ async def test_group_throttle_with_queue(coresys: CoreSys):
|
||||
test1 = TestClass(coresys, "test1")
|
||||
test2 = TestClass(coresys, "test2")
|
||||
|
||||
start = utcnow()
|
||||
|
||||
# One call of each should work. The subsequent calls will be silently throttled after waiting due to period
|
||||
with time_machine.travel(start):
|
||||
await asyncio.gather(
|
||||
*[test1.execute(0), test1.execute(0), test2.execute(0), test2.execute(0)]
|
||||
)
|
||||
@@ -886,7 +898,7 @@ async def test_group_throttle_with_queue(coresys: CoreSys):
|
||||
assert test2.call == 1
|
||||
|
||||
# All calls should work as we cleared the period. And tasks take longer then period and are queued
|
||||
with time_machine.travel(utcnow() + timedelta(milliseconds=100)):
|
||||
with time_machine.travel(start + timedelta(milliseconds=100)):
|
||||
await asyncio.gather(
|
||||
*[
|
||||
test1.execute(0.1),
|
||||
@@ -927,12 +939,16 @@ async def test_group_throttle_rate_limit(coresys: CoreSys, error: JobException |
|
||||
test1 = TestClass(coresys, "test1")
|
||||
test2 = TestClass(coresys, "test2")
|
||||
|
||||
start = utcnow()
|
||||
|
||||
with time_machine.travel(start):
|
||||
await asyncio.gather(
|
||||
*[test1.execute(), test1.execute(), test2.execute(), test2.execute()]
|
||||
)
|
||||
assert test1.call == 2
|
||||
assert test2.call == 2
|
||||
|
||||
with time_machine.travel(start + timedelta(milliseconds=1)):
|
||||
with pytest.raises(JobException if error is None else error):
|
||||
await test1.execute()
|
||||
with pytest.raises(JobException if error is None else error):
|
||||
@@ -941,7 +957,7 @@ async def test_group_throttle_rate_limit(coresys: CoreSys, error: JobException |
|
||||
assert test1.call == 2
|
||||
assert test2.call == 2
|
||||
|
||||
with time_machine.travel(utcnow() + timedelta(hours=1)):
|
||||
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
|
||||
await test1.execute()
|
||||
await test2.execute()
|
||||
|
||||
@@ -1285,6 +1301,9 @@ async def test_concurency_reject_and_rate_limit(
|
||||
|
||||
test = TestClass(coresys)
|
||||
|
||||
start = utcnow()
|
||||
|
||||
with time_machine.travel(start):
|
||||
results = await asyncio.gather(
|
||||
*[test.execute(0.1), test.execute(), test.execute()], return_exceptions=True
|
||||
)
|
||||
@@ -1293,12 +1312,15 @@ async def test_concurency_reject_and_rate_limit(
|
||||
assert isinstance(results[2], JobException)
|
||||
assert test.call == 1
|
||||
|
||||
with pytest.raises(JobException if error is None else error):
|
||||
with (
|
||||
time_machine.travel(start + timedelta(milliseconds=1)),
|
||||
pytest.raises(JobException if error is None else error),
|
||||
):
|
||||
await test.execute()
|
||||
|
||||
assert test.call == 1
|
||||
|
||||
with time_machine.travel(utcnow() + timedelta(hours=1)):
|
||||
with time_machine.travel(start + timedelta(hours=1, milliseconds=1)):
|
||||
await test.execute()
|
||||
|
||||
assert test.call == 2
|
||||
@@ -1342,18 +1364,22 @@ async def test_group_concurrency_with_group_throttling(coresys: CoreSys):
|
||||
|
||||
test = TestClass(coresys)
|
||||
|
||||
start = utcnow()
|
||||
|
||||
# First call should work
|
||||
with time_machine.travel(start):
|
||||
await test.main_method()
|
||||
assert test.call_count == 1
|
||||
assert test.nested_call_count == 1
|
||||
|
||||
# Second call should be throttled (not execute due to throttle period)
|
||||
with time_machine.travel(start + timedelta(milliseconds=1)):
|
||||
await test.main_method()
|
||||
assert test.call_count == 1 # Still 1, throttled
|
||||
assert test.nested_call_count == 1 # Still 1, throttled
|
||||
|
||||
# Wait for throttle period to pass and try again
|
||||
with time_machine.travel(utcnow() + timedelta(milliseconds=60)):
|
||||
with time_machine.travel(start + timedelta(milliseconds=60)):
|
||||
await test.main_method()
|
||||
|
||||
assert test.call_count == 2 # Should execute now
|
||||
|
||||
Reference in New Issue
Block a user