Skip to content

Commit 5494b63

Browse files
authored
eventemitter should raise TypeError when there isn't enough arguments passed on emit (#293)
1 parent 1fe7b85 commit 5494b63

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

.github/workflows/check-types.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: python -m pip install --upgrade mypy
3030

3131
- name: Install packages
32-
run: python -m pip install ./livekit-api ./livekit-protocol ./livekit-rtc
32+
run: python -m pip install pytest ./livekit-api ./livekit-protocol ./livekit-rtc
3333

3434
- name: Check Types
35-
run: python -m mypy --install-type --non-interactive -p 'livekit-protocol' -p 'livekit-api' -p 'livekit-rtc'
35+
run: python -m mypy --install-type --non-interactive -p 'livekit-protocol' -p 'livekit-api' -p 'livekit-rtc'

livekit-rtc/livekit/rtc/event_emitter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def greet(name):
5555
callback_args = args[:num_args]
5656

5757
callback(*callback_args)
58+
except TypeError:
59+
raise
5860
except Exception:
5961
logger.exception(f"failed to emit event {event}")
6062

livekit-rtc/tests/test_emitter.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from livekit.rtc import EventEmitter
2+
from typing import Literal
3+
import pytest
4+
5+
6+
def test_events():
7+
EventTypes = Literal["connected", "reconnected", "disconnected"]
8+
emitter = EventEmitter[EventTypes]()
9+
10+
connected_calls = []
11+
12+
@emitter.once("connected")
13+
def on_connected():
14+
connected_calls.append(True)
15+
16+
emitter.emit("connected")
17+
emitter.emit("connected")
18+
assert len(connected_calls) == 1
19+
20+
emitter.emit("unknown_event") # type: ignore
21+
22+
reconnected_calls = []
23+
24+
@emitter.on("reconnected")
25+
def on_reconnected():
26+
reconnected_calls.append(True)
27+
28+
emitter.emit("reconnected")
29+
emitter.emit("reconnected")
30+
assert len(reconnected_calls) == 2
31+
32+
disconnected_calls = []
33+
34+
@emitter.on("disconnected")
35+
def on_disconnected():
36+
disconnected_calls.append(True)
37+
38+
@emitter.on("disconnected")
39+
def on_disconnected_another():
40+
disconnected_calls.append(True)
41+
42+
emitter.emit("disconnected")
43+
emitter.emit("disconnected")
44+
emitter.off("disconnected", on_disconnected)
45+
emitter.emit("disconnected")
46+
assert len(disconnected_calls) == 5
47+
48+
49+
def test_args():
50+
EventTypes = Literal["whatever"]
51+
52+
emitter = EventEmitter[EventTypes]()
53+
54+
calls = []
55+
56+
@emitter.on("whatever")
57+
def on_whatever(first, second, third):
58+
calls.append((first, second, third))
59+
60+
emitter.emit("whatever", 1, 2, 3)
61+
emitter.emit("whatever", 1, 2, 3, 4, 5) # only 3 arguments will be passed
62+
63+
assert len(calls) == 2
64+
assert calls[0] == (1, 2, 3)
65+
assert calls[1] == (1, 2, 3)
66+
67+
calls = []
68+
69+
with pytest.raises(TypeError):
70+
emitter.emit("whatever", 1, 2)
71+
72+
assert len(calls) == 0
73+
74+
@emitter.on("whatever")
75+
def on_whatever_varargs(*args):
76+
calls.append(args)
77+
78+
emitter.emit("whatever", 1, 2, 3, 4, 5)
79+
80+
assert len(calls) == 2
81+
assert calls[0] == (1, 2, 3)
82+
assert calls[1] == (1, 2, 3, 4, 5)
83+
84+
85+
def test_throw():
86+
EventTypes = Literal["error"]
87+
88+
emitter = EventEmitter[EventTypes]()
89+
90+
calls = []
91+
92+
@emitter.on("error")
93+
def on_error():
94+
calls.append(True)
95+
raise ValueError("error")
96+
97+
@emitter.on("error")
98+
def on_error_another():
99+
calls.append(True)
100+
101+
emitter.emit("error")
102+
103+
assert len(calls) == 2

0 commit comments

Comments
 (0)