Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions src/workos/feature_flags/runtime/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ async def wait_until_ready(self, timeout: Optional[float] = None) -> None:
return
# Nothing can make the client ready in a forked child that cannot poll.
self._raise_if_fork_blocked()
try:
await asyncio.wait_for(self._ready.wait(), timeout)
except asyncio.TimeoutError:
# The readiness timeout names the last refresh error as its cause.
raise self._readiness_timeout(timeout)
# A zero timeout cancels Event.wait() before it can observe a set event.
if not self._ready.is_set():
try:
await asyncio.wait_for(self._ready.wait(), timeout)
except asyncio.TimeoutError:
# The readiness timeout names the last refresh error as its cause.
raise self._readiness_timeout(timeout)
if not self._status.ready:
raise self._readiness_failure()

Expand Down
14 changes: 11 additions & 3 deletions tests/feature_flags_runtime/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ async def test_bootstrap_is_ready_without_a_request(

async def test_wait_times_out_without_a_configuration(make_async_runtime: Make) -> None:
with pytest.raises(TimeoutError):
await make_async_runtime().wait_until_ready(timeout=0.01)
await make_async_runtime().wait_until_ready(timeout=0)


async def test_wait_with_zero_timeout_after_close(make_async_runtime: Make) -> None:
runtime = make_async_runtime()
await runtime.close()
with pytest.raises(RuntimeError, match="closed before it became ready"):
await runtime.wait_until_ready(timeout=0)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


async def test_a_wait_timeout_names_the_last_refresh_error(
Expand All @@ -106,8 +113,9 @@ async def test_a_wait_timeout_names_the_last_refresh_error(
assert raised.value.__cause__ is runtime.status.last_error


@pytest.mark.parametrize("timeout", [0, 1])
async def test_each_wait_after_a_401_raises_a_fresh_error(
server: FlagServer, make_async_runtime: Make
server: FlagServer, make_async_runtime: Make, timeout: float
) -> None:
server.respond(401, {"message": "Unauthorized"})
runtime = make_async_runtime()
Expand All @@ -116,7 +124,7 @@ async def test_each_wait_after_a_401_raises_a_fresh_error(
errors: List[BaseException] = []
for _ in range(4):
try:
await runtime.wait_until_ready(timeout=1)
await runtime.wait_until_ready(timeout=timeout)
except AuthenticationError as error:
errors.append(error)
assert len({id(error) for error in errors}) == 4
Expand Down
Loading