Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

test(partners): lock outbound dispatch timeout semantics (#1763)

evan188199-tech58b3f54c361c
diff --git a/deeptutor/partners/channels/manager.py b/deeptutor/partners/channels/manager.py
index 7fd2d2f79..fc79862c0 100644
--- a/deeptutor/partners/channels/manager.py
+++ b/deeptutor/partners/channels/manager.py
@@ -454,6 +454,10 @@ class ChannelManager:
await self._send_with_retry(channel, msg)
except asyncio.TimeoutError:
+ # Idle-poll timeout on an empty queue: the cancelled consume
+ # popped no message (a consume finishing despite the timeout
+ # is returned by wait_for), so nothing is skipped. Channel
+ # send timeouts are retried and logged in _send_with_retry.
continue
except asyncio.CancelledError:
break
diff --git a/tests/services/partners/test_channel_manager.py b/tests/services/partners/test_channel_manager.py
index 57d5f0d3b..c6a613b0c 100644
--- a/tests/services/partners/test_channel_manager.py
+++ b/tests/services/partners/test_channel_manager.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
+from contextlib import suppress
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -317,6 +318,73 @@ class TestSendRetry:
assert channel.send.await_count == 2
+class TestDispatchTimeoutSemantics:
+ """_dispatch_outbound's TimeoutError handler is an idle-poll timeout.
+
+ ``except asyncio.TimeoutError: continue`` only observes the 1s poll
+ timeout of ``wait_for(consume_outbound(), 1.0)``. No message is in
+ hand when it fires: a cancelled ``Queue.get()`` pops nothing, and a
+ consume that finishes despite the timeout has its result returned by
+ ``wait_for``. Channel send timeouts are ``Exception`` subclasses and
+ are retried inside ``_send_with_retry`` instead.
+ """
+
+ @pytest.mark.asyncio
+ async def test_idle_poll_timeout_does_not_skip_later_message(self):
+ from deeptutor.partners.bus.queue import MessageBus
+
+ msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
+ channel = _DummyChannel()
+ bus = MessageBus()
+ manager = ChannelManager(ChannelsConfig(), bus)
+ manager.channels = {msg.channel: channel} # type: ignore[dict-item]
+
+ task = asyncio.create_task(manager._dispatch_outbound())
+ # Cross at least one 1.0s poll timeout with an empty queue.
+ await asyncio.sleep(1.3)
+ await bus.publish_outbound(msg)
+
+ loop = asyncio.get_running_loop()
+ deadline = loop.time() + 2.0
+ while channel.send.await_count == 0 and loop.time() < deadline:
+ await asyncio.sleep(0.05)
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+
+ channel.send.assert_awaited_once_with(msg)
+
+ @pytest.mark.asyncio
+ async def test_channel_send_timeout_is_retried_by_dispatcher(self, monkeypatch):
+ monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
+ msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
+ channel = _DummyChannel()
+ channel.send.side_effect = [asyncio.TimeoutError(), None]
+ manager = ChannelManager(ChannelsConfig(), _OneShotBus(msg)) # type: ignore[arg-type]
+ manager.channels = {msg.channel: channel} # type: ignore[dict-item]
+
+ await manager._dispatch_outbound()
+
+ assert channel.send.await_count == 2
+ channel.send.assert_awaited_with(msg)
+
+ @pytest.mark.asyncio
+ async def test_channel_send_timeout_exhausts_retries(self, monkeypatch):
+ monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
+ msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
+ channel = _DummyChannel()
+ channel.send.side_effect = asyncio.TimeoutError
+ manager = ChannelManager(
+ ChannelsConfig(send_max_retries=3),
+ _OneShotBus(msg), # type: ignore[arg-type]
+ )
+ manager.channels = {msg.channel: channel} # type: ignore[dict-item]
+
+ await manager._dispatch_outbound()
+
+ assert channel.send.await_count == 3
+
+
class TestDuplicateSuppression:
@pytest.mark.asyncio
async def test_same_reply_to_same_origin_suppressed(self):