Repository navigation
test(tutorials): poll task state instead of a fixed 1s sleep #548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for wait_for_state_messages in the tutorials' test_utils. | ||
|
|
||
| The tutorials' `test_utils` package shares its name with `tests/test_utils`, so the | ||
| module is loaded from its file path instead of imported by name. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import time | ||
| import importlib.util | ||
| from types import ModuleType, SimpleNamespace | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| _ASYNC_UTILS = Path(__file__).resolve().parents[2] / "examples" / "tutorials" / "test_utils" / "async_utils.py" | ||
|
|
||
|
|
||
| def _load_async_utils() -> ModuleType: | ||
| spec = importlib.util.spec_from_file_location("tutorial_async_utils", _ASYNC_UTILS) | ||
| assert spec is not None and spec.loader is not None | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| wait_for_state_messages = _load_async_utils().wait_for_state_messages | ||
|
|
||
|
|
||
| class _FakeStates: | ||
| """Returns one state whose message count grows from 1 to 3 once `write_after` seconds pass.""" | ||
|
|
||
| def __init__(self, write_after: float | None): | ||
| self._start = time.monotonic() | ||
| self._write_after = write_after | ||
| self.calls = 0 | ||
|
|
||
| async def list(self, agent_id: str, task_id: str) -> list[SimpleNamespace]: | ||
| del agent_id, task_id | ||
| self.calls += 1 | ||
| written = self._write_after is not None and time.monotonic() - self._start >= self._write_after | ||
| return [SimpleNamespace(state={"messages": [{"role": "system"}] * (3 if written else 1)})] | ||
|
|
||
|
|
||
| def _client(write_after: float | None) -> SimpleNamespace: | ||
| return SimpleNamespace(states=_FakeStates(write_after)) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_waits_for_a_state_write_that_lands_after_the_reply() -> None: | ||
| client = _client(write_after=0.3) | ||
|
|
||
| messages = await wait_for_state_messages(client, "agent", "task", expected_count=3, timeout=5, sleep_interval=0.05) | ||
|
|
||
| assert len(messages) == 3 | ||
| assert client.states.calls > 1 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_returns_the_real_state_at_the_deadline_when_it_never_updates() -> None: | ||
| client = _client(write_after=None) | ||
| start = time.monotonic() | ||
|
|
||
| messages = await wait_for_state_messages(client, "agent", "task", expected_count=3, timeout=0.3, sleep_interval=0.05) | ||
|
|
||
| # It kept polling until the deadline instead of returning early. No upper bound: | ||
| # a busy worker may wake late, and returning at all already proves it stopped. | ||
| assert len(messages) == 1 | ||
| assert time.monotonic() - start >= 0.3 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_returns_on_the_first_poll_when_the_state_is_already_written() -> None: | ||
| client = _client(write_after=0) | ||
|
|
||
| messages = await wait_for_state_messages(client, "agent", "task", expected_count=3) | ||
|
|
||
| assert len(messages) == 3 | ||
| assert client.states.calls == 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.