From 988d4ce2e779a75a0770dbf96803901d8ea46024 Mon Sep 17 00:00:00 2001 From: Dhevenddra Date: Mon, 27 Jul 2026 21:16:17 +0530 Subject: [PATCH] test: stub Slack name resolution instead of relying on a dead port Both relay test modules pinned SLACK_API_URL at http://127.0.0.1:9/ on the stated assumption that lookups "fail instantly at a dead loopback port". That holds on POSIX, where a refused connection returns in microseconds. It does not hold on Windows, where refusing a loopback connect takes about 2.0s: socket.create_connection(("127.0.0.1", 9)) median 2.033s over 5 trials urllib.urlopen("http://127.0.0.1:9/...") median 2.032s over 3 trials RelayHub._run only increments _dispatched after awaiting the frame handler, and _dispatch_slack_event awaits both _display_name and _channel_name, so a single frame cost roughly 4s against the 2.0s wait_dispatched budget. The result was a deterministic "only 0 frames dispatched" across five tests, reproduced identically on three consecutive runs. Stub _slack_get, the single chokepoint both lookups go through, so the tests are hermetic by construction rather than by depending on how fast the host OS refuses a connection. SLACK_API_URL still points at the dead port as a backstop so an unstubbed path can never reach slack.com. The stub goes on the class, so the two tests that exercise real name mapping keep working: they set _slack_get on the instance, which shadows it. On Windows this takes the two modules from 5 failed / 29 passed in 21.2s to 34 passed in 10.2s. Linux is unaffected: 889 passed, 1 skipped. --- tests/test_github_installs.py | 7 +++++++ tests/test_slack_relay.py | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/test_github_installs.py b/tests/test_github_installs.py index 907dd2e6..fd6afc66 100644 --- a/tests/test_github_installs.py +++ b/tests/test_github_installs.py @@ -215,6 +215,13 @@ def _slack_frame(): async def test_one_hub_fans_out_to_both_adapters(monkeypatch): """THE step-3 invariant: slack + github share one relay socket; frames land on their own adapter by provider tag.""" + # Stub name resolution at its chokepoint. A dead port only refuses instantly on + # POSIX; on Windows it takes ~2.0s per call and this frame makes two, blowing the + # 2.0s wait_dispatched budget below. The env var stays as a no-network backstop. + async def _no_slack_api(self, team_id, method, params): + return None + + monkeypatch.setattr(SlackRelayAdapter, "_slack_get", _no_slack_api) monkeypatch.setenv("SLACK_API_URL", "http://127.0.0.1:9/") hub = RelayHub( "wss://relay.test/ws", diff --git a/tests/test_slack_relay.py b/tests/test_slack_relay.py index b05ce807..9894a8c0 100644 --- a/tests/test_slack_relay.py +++ b/tests/test_slack_relay.py @@ -20,9 +20,21 @@ @pytest.fixture(autouse=True) def _no_slack_network(monkeypatch): - """Name/channel resolution is best-effort; unstubbed lookups must fail - instantly at a dead loopback port, never reach slack.com — a slow real - answer was blowing the 2s wait_dispatched window intermittently.""" + """Name/channel resolution is best-effort, so stub it at its single chokepoint. + + A dead loopback port alone is not enough. Refusing a connection is only instant + on POSIX; on Windows it takes ~2.0s, and `_dispatch_slack_event` awaits both + `_display_name` and `_channel_name`, so one frame cost ~4s against the 2.0s + budget and no frame ever dispatched in time. `SLACK_API_URL` still points at a + dead port as a backstop, so an unstubbed path can never reach slack.com. + """ + + async def _no_slack_api(self, team_id, method, params): + return None + + monkeypatch.setattr( + relay_client.SlackRelayAdapter, "_slack_get", _no_slack_api + ) monkeypatch.setenv("SLACK_API_URL", "http://127.0.0.1:9/")