|
| 1 | +"""Unit tests for BroadcastQueue subscriber removal. |
| 2 | +
|
| 3 | +``Room._listen_task`` calls ``BroadcastQueue.join()`` after every event, so a |
| 4 | +subscriber that goes away (for example a cancelled ``publish_track``) must not |
| 5 | +leave that join waiting on events nobody will finish. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import asyncio |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from livekit.rtc._utils import BroadcastQueue |
| 15 | + |
| 16 | + |
| 17 | +async def _join_finishes(queue: BroadcastQueue, timeout: float = 1.0) -> bool: |
| 18 | + try: |
| 19 | + await asyncio.wait_for(queue.join(), timeout) |
| 20 | + except asyncio.TimeoutError: |
| 21 | + return False |
| 22 | + return True |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_join_waits_for_a_subscriber_that_has_not_finished() -> None: |
| 27 | + broadcast: BroadcastQueue[int] = BroadcastQueue() |
| 28 | + subscriber = broadcast.subscribe() |
| 29 | + broadcast.put_nowait(1) |
| 30 | + |
| 31 | + assert not await _join_finishes(broadcast, timeout=0.1) |
| 32 | + |
| 33 | + await subscriber.get() |
| 34 | + subscriber.task_done() |
| 35 | + assert await _join_finishes(broadcast) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_unsubscribe_releases_a_join_waiting_on_a_queued_event() -> None: |
| 40 | + broadcast: BroadcastQueue[int] = BroadcastQueue() |
| 41 | + subscriber = broadcast.subscribe() |
| 42 | + broadcast.put_nowait(1) |
| 43 | + |
| 44 | + joiner = asyncio.create_task(broadcast.join()) |
| 45 | + await asyncio.sleep(0) |
| 46 | + assert not joiner.done() |
| 47 | + |
| 48 | + broadcast.unsubscribe(subscriber) |
| 49 | + |
| 50 | + await asyncio.wait_for(joiner, 1.0) |
| 51 | + assert broadcast.len_subscribers() == 0 |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_unsubscribe_releases_a_join_waiting_on_an_event_that_was_taken() -> None: |
| 56 | + broadcast: BroadcastQueue[int] = BroadcastQueue() |
| 57 | + subscriber = broadcast.subscribe() |
| 58 | + broadcast.put_nowait(1) |
| 59 | + await subscriber.get() # taken, but the subscriber never calls task_done() |
| 60 | + |
| 61 | + joiner = asyncio.create_task(broadcast.join()) |
| 62 | + await asyncio.sleep(0) |
| 63 | + assert not joiner.done() |
| 64 | + |
| 65 | + broadcast.unsubscribe(subscriber) |
| 66 | + |
| 67 | + await asyncio.wait_for(joiner, 1.0) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_unsubscribe_keeps_other_subscribers_in_the_join() -> None: |
| 72 | + broadcast: BroadcastQueue[int] = BroadcastQueue() |
| 73 | + gone = broadcast.subscribe() |
| 74 | + staying = broadcast.subscribe() |
| 75 | + broadcast.put_nowait(1) |
| 76 | + |
| 77 | + joiner = asyncio.create_task(broadcast.join()) |
| 78 | + await asyncio.sleep(0) |
| 79 | + broadcast.unsubscribe(gone) |
| 80 | + await asyncio.sleep(0.05) |
| 81 | + assert not joiner.done() |
| 82 | + |
| 83 | + await staying.get() |
| 84 | + staying.task_done() |
| 85 | + await asyncio.wait_for(joiner, 1.0) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_unsubscribe_after_every_event_is_finished_is_a_no_op() -> None: |
| 90 | + broadcast: BroadcastQueue[int] = BroadcastQueue() |
| 91 | + subscriber = broadcast.subscribe() |
| 92 | + broadcast.put_nowait(1) |
| 93 | + await subscriber.get() |
| 94 | + subscriber.task_done() |
| 95 | + |
| 96 | + broadcast.unsubscribe(subscriber) |
| 97 | + |
| 98 | + assert await _join_finishes(broadcast) |
0 commit comments