Skip to content

Commit 1da4886

Browse files
add gemini's suggestions
1 parent d5ef1c4 commit 1da4886

File tree

2 files changed

+5
-40
lines changed

2 files changed

+5
-40
lines changed

src/a2a/server/events/event_queue.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,19 @@ async def close(self, immediate: bool = False) -> None:
172172
return
173173
# Graceful: prevent further gets/puts via shutdown, then wait for drain and children
174174
self.queue.shutdown(False)
175-
tasks = [asyncio.create_task(self.queue.join())]
176-
tasks.extend(
177-
asyncio.create_task(child.close()) for child in self._children
175+
await asyncio.gather(
176+
self.queue.join(), *(child.close() for child in self._children)
178177
)
179-
await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
180178
# Otherwise, join the queue
181179
else:
182180
if immediate:
183181
await self.clear_events(True)
184182
for child in self._children:
185183
await child.close(immediate)
186184
return
187-
tasks = [asyncio.create_task(self.queue.join())]
188-
tasks.extend(
189-
asyncio.create_task(child.close()) for child in self._children
185+
await asyncio.gather(
186+
self.queue.join(), *(child.close() for child in self._children)
190187
)
191-
await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
192188

193189
def is_closed(self) -> bool:
194190
"""Checks if the queue is closed."""

tests/server/events/test_event_queue.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,7 @@ async def test_tap_creates_child_queue(event_queue: EventQueue) -> None:
271271

272272

273273
@pytest.mark.asyncio
274-
@patch(
275-
'asyncio.wait'
276-
) # To monitor calls to asyncio.wait for older Python versions
277-
@patch(
278-
'asyncio.create_task'
279-
) # To monitor calls to asyncio.create_task for older Python versions
280274
async def test_close_sets_flag_and_handles_internal_queue_old_python(
281-
mock_create_task: MagicMock,
282-
mock_asyncio_wait: AsyncMock,
283275
event_queue: EventQueue,
284276
) -> None:
285277
"""Test close behavior on Python < 3.13 (using queue.join)."""
@@ -290,9 +282,7 @@ async def test_close_sets_flag_and_handles_internal_queue_old_python(
290282
await event_queue.close()
291283

292284
assert event_queue.is_closed() is True
293-
event_queue.queue.join.assert_called_once() # specific to <3.13
294-
mock_create_task.assert_called_once() # create_task for join
295-
mock_asyncio_wait.assert_called_once() # wait for join
285+
event_queue.queue.join.assert_awaited_once() # waited for drain
296286

297287

298288
@pytest.mark.asyncio
@@ -311,11 +301,7 @@ async def test_close_sets_flag_and_handles_internal_queue_new_python(
311301

312302

313303
@pytest.mark.asyncio
314-
@patch('asyncio.wait')
315-
@patch('asyncio.create_task')
316304
async def test_close_graceful_py313_waits_for_join_and_children(
317-
mock_create_task: AsyncMock,
318-
mock_asyncio_wait: AsyncMock,
319305
event_queue: EventQueue,
320306
) -> None:
321307
"""For Python >=3.13 and immediate=False, close should shutdown(False), then wait for join and children."""
@@ -330,29 +316,12 @@ async def test_close_graceful_py313_waits_for_join_and_children(
330316
child = event_queue.tap()
331317
child.close = AsyncMock()
332318

333-
# Ensure created tasks actually run their coroutines
334-
async def _runner(coro):
335-
await coro
336-
337-
def _create_task_side_effect(coro):
338-
loop = asyncio.get_running_loop()
339-
return loop.create_task(_runner(coro))
340-
341-
mock_create_task.side_effect = _create_task_side_effect
342-
343-
async def _wait_side_effect(tasks, return_when=None):
344-
await asyncio.gather(*tasks, return_exceptions=True)
345-
return (set(tasks), set())
346-
347-
mock_asyncio_wait.side_effect = _wait_side_effect
348-
349319
# Act
350320
await event_queue.close(immediate=False)
351321

352322
# Assert
353323
event_queue.queue.join.assert_awaited_once()
354324
child.close.assert_awaited_once()
355-
mock_asyncio_wait.assert_called()
356325

357326

358327
@pytest.mark.asyncio

0 commit comments

Comments
 (0)