import asyncio
import pytest
from asgi_autoreload.broadcaster import Broadcaster


async def test_subscribe_receives_broadcast():
    b = Broadcaster()
    q = await b.subscribe()
    await b.broadcast("reload")
    assert q.get_nowait() == "reload"


async def test_unsubscribe_no_longer_receives():
    b = Broadcaster()
    q = await b.subscribe()
    await b.unsubscribe(q)
    await b.broadcast("reload")
    assert q.empty()


async def test_multiple_subscribers_all_receive():
    b = Broadcaster()
    q1 = await b.subscribe()
    q2 = await b.subscribe()
    await b.broadcast("reload")
    assert q1.get_nowait() == "reload"
    assert q2.get_nowait() == "reload"


async def test_broadcast_delivers_multiple_events_in_order():
    b = Broadcaster()
    q = await b.subscribe()
    await b.broadcast("reload")
    await b.broadcast("reload")
    assert q.get_nowait() == "reload"
    assert q.get_nowait() == "reload"
    assert q.empty()
