-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtest_basic.py
More file actions
221 lines (172 loc) · 6.71 KB
/
Copy pathtest_basic.py
File metadata and controls
221 lines (172 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""End-to-end Test for basic scenarios.
The test exercises connect/disconnect lifecycle, participant
visibility, reconnection, and track publish/subscribe between four rooms.
Requires the following environment variables to run:
LIVEKIT_URL
LIVEKIT_API_KEY
LIVEKIT_API_SECRET
"""
from __future__ import annotations
import asyncio
import os
import uuid
from typing import Callable, Optional
import pytest
from livekit import api, rtc
from livekit.rtc.room import EventTypes
WAIT_TIMEOUT = 20.0
WAIT_INTERVAL = 0.1
def skip_if_no_credentials():
required_vars = ["LIVEKIT_URL", "LIVEKIT_API_KEY", "LIVEKIT_API_SECRET"]
missing = [var for var in required_vars if not os.getenv(var)]
return pytest.mark.skipif(
bool(missing), reason=f"Missing environment variables: {', '.join(missing)}"
)
def create_token(identity: str, room_name: str) -> str:
return (
api.AccessToken()
.with_identity(identity)
.with_name(identity)
.with_grants(
api.VideoGrants(
room_join=True,
room=room_name,
)
)
.to_jwt()
)
def unique_room_name(base: str) -> str:
return f"{base}-{uuid.uuid4().hex[:8]}"
async def _wait_until(
predicate: Callable[[], bool],
*,
timeout: float = WAIT_TIMEOUT,
interval: float = WAIT_INTERVAL,
message: str = "condition not met",
) -> None:
loop = asyncio.get_event_loop()
deadline = loop.time() + timeout
while loop.time() < deadline:
if predicate():
return
await asyncio.sleep(interval)
raise AssertionError(f"timeout waiting: {message}")
async def _connect(room: rtc.Room, identity: str, room_name: str) -> str:
"""Mints a token, connects `room`, and returns the token (for reconnect)."""
token = create_token(identity, room_name)
url = os.environ["LIVEKIT_URL"]
await room.connect(url, token)
return token
async def _ensure_all_connected(rooms: list[rtc.Room]) -> None:
await _wait_until(
lambda: all(r.connection_state == rtc.ConnectionState.CONN_CONNECTED for r in rooms),
message="not all rooms reached CONN_CONNECTED",
)
async def _ensure_track_subscribed(room: rtc.Room, track_sid: str) -> None:
def _has_subscribed() -> bool:
for participant in room.remote_participants.values():
pub = participant.track_publications.get(track_sid)
if pub is not None and pub.subscribed:
return True
return False
await _wait_until(
_has_subscribed,
message=f"room did not subscribe to track {track_sid}",
)
def _expect_event(
room: rtc.Room,
event: EventTypes,
predicate: Optional[Callable[..., bool]] = None,
) -> asyncio.Future:
"""Returns a future that resolves when `event` (optionally matching
`predicate`) is fired on `room`."""
loop = asyncio.get_event_loop()
fut: asyncio.Future = loop.create_future()
def _on_event(*args, **kwargs) -> None:
if fut.done():
return
if predicate is None or predicate(*args, **kwargs):
fut.set_result(args)
room.on(event, _on_event)
return fut
async def _await_event(fut: asyncio.Future, timeout: float = WAIT_TIMEOUT) -> None:
try:
await asyncio.wait_for(fut, timeout=timeout)
except asyncio.TimeoutError as e:
raise AssertionError("timed out waiting for event") from e
async def _publish_video(room: rtc.Room, track_name: str) -> rtc.LocalTrackPublication:
source = rtc.VideoSource(320, 240)
track = rtc.LocalVideoTrack.create_video_track(track_name, source)
options = rtc.TrackPublishOptions(source=rtc.TrackSource.SOURCE_CAMERA)
return await room.local_participant.publish_track(track, options)
async def _publish_audio(room: rtc.Room, track_name: str) -> rtc.LocalTrackPublication:
source = rtc.AudioSource(48000, 1)
track = rtc.LocalAudioTrack.create_audio_track(track_name, source)
options = rtc.TrackPublishOptions(source=rtc.TrackSource.SOURCE_MICROPHONE)
return await room.local_participant.publish_track(track, options)
@skip_if_no_credentials()
@pytest.mark.asyncio
async def test_connection_basics() -> None:
room_name = unique_room_name("py-basics")
p1, p2 = rtc.Room(), rtc.Room()
await _connect(p1, "p1", room_name)
await _connect(p2, "p2", room_name)
await _ensure_all_connected([p1, p2])
# p2 should observe p1 leaving
p2_saw_p1_left = _expect_event(
p2,
"participant_disconnected",
predicate=lambda p: p.identity == "p1",
)
await p1.disconnect()
await _await_event(p2_saw_p1_left)
await _wait_until(
lambda: p1.connection_state == rtc.ConnectionState.CONN_DISCONNECTED,
message="p1 did not reach CONN_DISCONNECTED",
)
await p2.disconnect()
await _wait_until(
lambda: p2.connection_state == rtc.ConnectionState.CONN_DISCONNECTED,
message="p2 did not reach CONN_DISCONNECTED",
)
# p3: connect, disconnect, reconnect, disconnect cycle
p3 = rtc.Room()
p3_token = await _connect(p3, "p3", room_name)
p3_url = os.environ["LIVEKIT_URL"]
await p3.disconnect()
assert p3.connection_state == rtc.ConnectionState.CONN_DISCONNECTED, (
f"expected p3 disconnected, got {p3.connection_state}"
)
await p3.connect(p3_url, p3_token)
assert p3.connection_state == rtc.ConnectionState.CONN_CONNECTED, (
f"expected p3 connected, got {p3.connection_state}"
)
await p3.disconnect()
assert p3.connection_state == rtc.ConnectionState.CONN_DISCONNECTED, (
f"expected p3 disconnected, got {p3.connection_state}"
)
# p4 joins, then p3 reconnects to publish to p4
p4 = rtc.Room()
await _connect(p4, "p4", room_name)
await p3.connect(p3_url, p3_token)
assert p3.connection_state == rtc.ConnectionState.CONN_CONNECTED, (
f"expected p3 reconnected, got {p3.connection_state}"
)
# publish camera from p3, expect p4 to see track_published
video_published = _expect_event(p4, "track_published")
video_pub = await _publish_video(p3, "p3-camera")
await _await_event(video_published)
await _ensure_track_subscribed(p4, video_pub.sid)
# publish microphone from p3, expect p4 to see another track_published
audio_published = _expect_event(
p4,
"track_published",
predicate=lambda pub, _p: pub.sid != video_pub.sid,
)
audio_pub = await _publish_audio(p3, "p3-mic")
await _await_event(audio_published)
await _ensure_track_subscribed(p4, audio_pub.sid)
await p3.disconnect()
await p4.disconnect()
assert p3.connection_state == rtc.ConnectionState.CONN_DISCONNECTED
assert p4.connection_state == rtc.ConnectionState.CONN_DISCONNECTED