Skip to content

Commit 0e9baaa

Browse files
authored
test: Add missing tests for reject method (#328)
# Description This update addresses the need to improve test coverage for the `reject` method in `TaskUpdater`. While basic tests for the `reject` method already existed, this PR adds a crucial test case to handle race conditions, ensuring the reliability and robustness of the task rejection functionality. **Changes:** * Added `test_reject_concurrently_with_complete` to `tests/server/tasks/test_task_updater.py`. This test simulates a race condition by calling `reject()` and `complete()` concurrently on the same task. * It verifies that only one of the terminal state updates succeeds and that only one event is sent, preventing inconsistent states. **Value:** * Improves test coverage by testing for race conditions. * Ensures the reliability and consistency of the `TaskUpdater` when multiple terminal state updates are attempted simultaneously.
1 parent 0756d23 commit 0e9baaa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/server/tasks/test_task_updater.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,33 @@ async def test_concurrent_updates_race_condition(event_queue):
535535
assert len(successes) == 1
536536
assert len(failures) == 1
537537
assert event_queue.enqueue_event.call_count == 1
538+
539+
540+
@pytest.mark.asyncio
541+
async def test_reject_concurrently_with_complete(event_queue):
542+
"""Test for race conditions when reject and complete are called concurrently."""
543+
task_updater = TaskUpdater(
544+
event_queue=event_queue,
545+
task_id='concurrent-task',
546+
context_id='concurrent-context',
547+
)
548+
549+
tasks = [
550+
task_updater.reject(),
551+
task_updater.complete(),
552+
]
553+
554+
results = await asyncio.gather(*tasks, return_exceptions=True)
555+
556+
successes = [r for r in results if not isinstance(r, Exception)]
557+
failures = [r for r in results if isinstance(r, RuntimeError)]
558+
559+
assert len(successes) == 1
560+
assert len(failures) == 1
561+
562+
assert event_queue.enqueue_event.call_count == 1
563+
564+
event = event_queue.enqueue_event.call_args[0][0]
565+
assert isinstance(event, TaskStatusUpdateEvent)
566+
assert event.final is True
567+
assert event.status.state in [TaskState.rejected, TaskState.completed]

0 commit comments

Comments
 (0)