Skip to content

Commit f6b1a29

Browse files
authored
chore: Update test_get_auto_saved_value_auto_save to reduce flakiness (#810)
### Description Update test_get_auto_saved_value_auto_save to reduce flakiness. Introduce time tolerance to the test.
1 parent a1e4586 commit f6b1a29

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

tests/unit/storages/test_key_value_store.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4-
from datetime import timedelta
4+
from datetime import datetime, timedelta, timezone
55
from typing import TYPE_CHECKING
66
from unittest.mock import patch
77
from urllib.parse import urlparse
@@ -157,14 +157,24 @@ async def test_get_auto_saved_value_cache_value(key_value_store: KeyValueStore)
157157

158158

159159
async def test_get_auto_saved_value_auto_save(key_value_store: KeyValueStore, mock_event_manager: EventManager) -> None: # noqa: ARG001
160+
# This is not a realtime system and timing constrains can be hard to enforce.
161+
# For the test to avoid flakiness it needs some time tolerance.
162+
autosave_deadline_time = 1
163+
autosave_check_period = 0.01
164+
165+
async def autosaved_within_deadline(key: str, expected_value: dict[str, str]) -> bool:
166+
"""Check if the `key_value_store` of `key` has expected value within `autosave_deadline_time` seconds."""
167+
deadline = datetime.now(tz=timezone.utc) + timedelta(seconds=autosave_deadline_time)
168+
while datetime.now(tz=timezone.utc) < deadline:
169+
await asyncio.sleep(autosave_check_period)
170+
if await key_value_store.get_value(key) == expected_value:
171+
return True
172+
return False
173+
160174
default_value: dict[str, JsonSerializable] = {'hello': 'world'}
161175
key_name = 'state'
162176
value = await key_value_store.get_auto_saved_value(key_name, default_value)
163-
await asyncio.sleep(0.1)
164-
value_one = await key_value_store.get_value(key_name)
165-
assert value_one == {'hello': 'world'}
177+
assert await autosaved_within_deadline(key=key_name, expected_value={'hello': 'world'})
166178

167179
value['hello'] = 'new_world'
168-
await asyncio.sleep(0.1)
169-
value_two = await key_value_store.get_value(key_name)
170-
assert value_two == {'hello': 'new_world'}
180+
assert await autosaved_within_deadline(key=key_name, expected_value={'hello': 'new_world'})

0 commit comments

Comments
 (0)