|
1 | | -from livekit.rtc import EventEmitter |
| 1 | +import asyncio |
| 2 | +import functools |
| 3 | +import warnings |
2 | 4 | from typing import Any, Literal |
| 5 | +from unittest.mock import AsyncMock |
| 6 | + |
3 | 7 | import pytest |
4 | 8 |
|
| 9 | +from livekit.rtc import EventEmitter |
| 10 | + |
5 | 11 |
|
6 | 12 | def test_events() -> None: |
7 | 13 | EventTypes = Literal["connected", "reconnected", "disconnected"] |
@@ -102,3 +108,79 @@ def on_error_another() -> None: |
102 | 108 | emitter.emit("error") |
103 | 109 |
|
104 | 110 | assert len(calls) == 2 |
| 111 | + |
| 112 | + |
| 113 | +def test_on_does_not_warn() -> None: |
| 114 | + """Registering a callback must not emit a DeprecationWarning. |
| 115 | +
|
| 116 | + `asyncio.iscoroutinefunction` is deprecated in Python 3.14 and slated for removal |
| 117 | + in 3.16; `inspect.iscoroutinefunction` is the supported replacement. |
| 118 | + """ |
| 119 | + EventTypes = Literal["connected"] |
| 120 | + |
| 121 | + emitter = EventEmitter[EventTypes]() |
| 122 | + |
| 123 | + with warnings.catch_warnings(): |
| 124 | + warnings.simplefilter("error", DeprecationWarning) |
| 125 | + |
| 126 | + @emitter.on("connected") |
| 127 | + def on_connected() -> None: |
| 128 | + pass |
| 129 | + |
| 130 | + emitter.once("connected", on_connected) |
| 131 | + |
| 132 | + emitter.emit("connected") |
| 133 | + |
| 134 | + |
| 135 | +def test_on_rejects_async_callback() -> None: |
| 136 | + """`.on()` refuses coroutine functions, however they are spelled.""" |
| 137 | + EventTypes = Literal["connected"] |
| 138 | + |
| 139 | + emitter = EventEmitter[EventTypes]() |
| 140 | + |
| 141 | + async def on_connected() -> None: |
| 142 | + pass |
| 143 | + |
| 144 | + with pytest.raises(ValueError, match="Cannot register an async callback"): |
| 145 | + emitter.on("connected", on_connected) |
| 146 | + |
| 147 | + with pytest.raises(ValueError, match="Cannot register an async callback"): |
| 148 | + emitter.on("connected", functools.partial(on_connected)) |
| 149 | + |
| 150 | + |
| 151 | +def test_on_rejects_sentinel_tagged_callback() -> None: |
| 152 | + """A callable tagged with asyncio's private coroutine sentinel is rejected. |
| 153 | +
|
| 154 | + `asyncio.iscoroutinefunction` honours this tag on every supported version; |
| 155 | + `inspect.iscoroutinefunction` never has. `@asyncio.coroutine` (removed in |
| 156 | + 3.11) applied it, and `unittest.mock.AsyncMock` still does. |
| 157 | + """ |
| 158 | + marker = getattr(asyncio.coroutines, "_is_coroutine", None) |
| 159 | + if marker is None: |
| 160 | + pytest.skip("asyncio.coroutines._is_coroutine is not defined on this Python") |
| 161 | + |
| 162 | + EventTypes = Literal["connected"] |
| 163 | + |
| 164 | + emitter = EventEmitter[EventTypes]() |
| 165 | + |
| 166 | + def on_connected() -> None: |
| 167 | + pass |
| 168 | + |
| 169 | + on_connected._is_coroutine = marker # type: ignore[attr-defined] |
| 170 | + |
| 171 | + with pytest.raises(ValueError, match="Cannot register an async callback"): |
| 172 | + emitter.on("connected", on_connected) |
| 173 | + |
| 174 | + |
| 175 | +def test_on_rejects_async_mock() -> None: |
| 176 | + """`AsyncMock` is rejected on every supported version. |
| 177 | +
|
| 178 | + On Python 3.9, `inspect.iscoroutinefunction(AsyncMock())` is False; only the |
| 179 | + sentinel check catches it. |
| 180 | + """ |
| 181 | + EventTypes = Literal["connected"] |
| 182 | + |
| 183 | + emitter = EventEmitter[EventTypes]() |
| 184 | + |
| 185 | + with pytest.raises(ValueError, match="Cannot register an async callback"): |
| 186 | + emitter.on("connected", AsyncMock()) |
0 commit comments