|
| 1 | +# Copyright (c) 2026 Code Infinity |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +"""THE WEBSOCKET BACKPLANE PUBLISH MUST NOT BLOCK THE EVENT LOOP. |
| 8 | +
|
| 9 | +The Redis backplane's LISTENER already runs in its own thread |
| 10 | +(RedisBackplane.subscribe -> pubsub.run_in_thread). The PUBLISH side does not: |
| 11 | +`WebSocketManager.broadcast*` (async, on the loop) calls `_publish`, which calls |
| 12 | +the synchronous `redis.publish` inline. So every broadcast - a WS send, a WebRTC |
| 13 | +signalling relay (broadcast_to_room), a chat fan-out - makes a blocking socket |
| 14 | +round-trip ON the loop, freezing every other connection for its duration. |
| 15 | +
|
| 16 | +Proven the ADR-0074 way: a real WebSocketManager, a real clock and threads, and |
| 17 | +a real backplane object whose `publish` carries a measured latency (the sleep is |
| 18 | +the instrument, not a mock of Redis). No local connections, so the broadcast is |
| 19 | +purely the publish. RED before the offload, GREEN after. |
| 20 | +""" |
| 21 | +import asyncio |
| 22 | +import time |
| 23 | + |
| 24 | +from tina4_python.websocket import WebSocketManager |
| 25 | + |
| 26 | +DELAY = 0.3 |
| 27 | +N = 4 |
| 28 | + |
| 29 | + |
| 30 | +class SlowBackplane: |
| 31 | + """A real backplane whose publish blocks for DELAY (stands in for a slow |
| 32 | + Redis round-trip). subscribe is a no-op; the test only broadcasts.""" |
| 33 | + |
| 34 | + def publish(self, channel, message): |
| 35 | + time.sleep(DELAY) |
| 36 | + |
| 37 | + def subscribe(self, channel, callback): |
| 38 | + pass |
| 39 | + |
| 40 | + def close(self): |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +def _manager(): |
| 45 | + mgr = WebSocketManager() |
| 46 | + mgr._backplane_started = True # skip the real wiring in _ensure_backplane |
| 47 | + mgr._backplane = SlowBackplane() |
| 48 | + return mgr |
| 49 | + |
| 50 | + |
| 51 | +def test_backplane_publish_does_not_stall_the_event_loop(): |
| 52 | + """While one broadcast's publish runs, the loop must keep ticking.""" |
| 53 | + mgr = _manager() |
| 54 | + ticks = [] |
| 55 | + |
| 56 | + async def ticker(): |
| 57 | + for _ in range(200): |
| 58 | + ticks.append(time.monotonic()) |
| 59 | + await asyncio.sleep(0.01) |
| 60 | + |
| 61 | + async def drive(): |
| 62 | + t = asyncio.ensure_future(ticker()) |
| 63 | + await asyncio.sleep(0) |
| 64 | + await mgr.broadcast_all("hello") |
| 65 | + t.cancel() |
| 66 | + |
| 67 | + asyncio.run(drive()) |
| 68 | + |
| 69 | + assert len(ticks) > 15, ( |
| 70 | + f"the loop ticked only {len(ticks)} times during one broadcast's " |
| 71 | + f"{DELAY}s publish. The backplane publish ran ON the loop, so every " |
| 72 | + "connection was frozen with it." |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def test_concurrent_broadcasts_do_not_serialize_on_the_publish(): |
| 77 | + """N concurrent broadcasts must overlap their publishes, not queue on them. |
| 78 | +
|
| 79 | + Serial (today): N * DELAY. Concurrent (after offload): ~DELAY. |
| 80 | + """ |
| 81 | + mgr = _manager() |
| 82 | + |
| 83 | + async def drive(): |
| 84 | + start = time.monotonic() |
| 85 | + await asyncio.gather(*(mgr.broadcast_all(f"m{i}") for i in range(N))) |
| 86 | + return time.monotonic() - start |
| 87 | + |
| 88 | + elapsed = asyncio.run(drive()) |
| 89 | + serial = N * DELAY |
| 90 | + assert elapsed < serial * 0.6, ( |
| 91 | + f"{N} concurrent broadcasts took {elapsed:.2f}s; serial would be ~{serial:.2f}s " |
| 92 | + f"and concurrent ~{DELAY:.2f}s. They serialised on the backplane publish." |
| 93 | + ) |
0 commit comments