Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTC-512 add peer_disconnected_timeout option in RoomApi.create_room #35

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions jellyfish/_openapi_client/models/room_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class RoomConfig:

max_peers: Union[Unset, None, int] = UNSET
"""Maximum amount of peers allowed into the room"""
peer_disconnected_timeout: Union[Unset, None, int] = UNSET
"""Duration (in seconds) after which the peer will be removed if it is disconnected. If not provided, this feature is disabled."""
peerless_purge_timeout: Union[Unset, None, int] = UNSET
"""Duration (in seconds) after which the room will be removed if no peers are connected. If not provided, this feature is disabled."""
room_id: Union[Unset, None, str] = UNSET
Expand All @@ -29,6 +31,7 @@ class RoomConfig:
def to_dict(self) -> Dict[str, Any]:
"""@private"""
max_peers = self.max_peers
peer_disconnected_timeout = self.peer_disconnected_timeout
peerless_purge_timeout = self.peerless_purge_timeout
room_id = self.room_id
video_codec: Union[Unset, None, str] = UNSET
Expand All @@ -42,6 +45,8 @@ def to_dict(self) -> Dict[str, Any]:
field_dict.update({})
if max_peers is not UNSET:
field_dict["maxPeers"] = max_peers
if peer_disconnected_timeout is not UNSET:
field_dict["peerDisconnectedTimeout"] = peer_disconnected_timeout
if peerless_purge_timeout is not UNSET:
field_dict["peerlessPurgeTimeout"] = peerless_purge_timeout
if room_id is not UNSET:
Expand All @@ -59,6 +64,8 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
max_peers = d.pop("maxPeers", UNSET)

peer_disconnected_timeout = d.pop("peerDisconnectedTimeout", UNSET)

peerless_purge_timeout = d.pop("peerlessPurgeTimeout", UNSET)

room_id = d.pop("roomId", UNSET)
Expand All @@ -76,6 +83,7 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:

room_config = cls(
max_peers=max_peers,
peer_disconnected_timeout=peer_disconnected_timeout,
peerless_purge_timeout=peerless_purge_timeout,
room_id=room_id,
video_codec=video_codec,
Expand Down
4 changes: 4 additions & 0 deletions jellyfish/api/_room_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def create_room(
max_peers: int = None,
video_codec: Literal["h264", "vp8"] = None,
webhook_url: str = None,
peerless_purge_timeout: int = None,
peer_disconnected_timeout: int = None,
) -> Tuple[str, Room]:
"""
Creates a new room
Expand All @@ -86,6 +88,8 @@ def create_room(
max_peers=max_peers,
video_codec=video_codec,
webhook_url=webhook_url,
peerless_purge_timeout=peerless_purge_timeout,
peer_disconnected_timeout=peer_disconnected_timeout,
)

resp = self._request(room_create_room, json_body=room_config)
Expand Down
2 changes: 1 addition & 1 deletion tests/support/asyncio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jellyfish import Notifier

ASSERTION_TIMEOUT = 2.0
ASSERTION_TIMEOUT = 5.0


async def assert_events(notifier: Notifier, event_checks: list):
Expand Down
6 changes: 4 additions & 2 deletions tests/support/peer_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@


class PeerSocket:
def __init__(self, server_address):
def __init__(self, server_address, auto_close=False):
self._server_address = server_address

self._ready = False
self._ready_event = None
self._auto_close = auto_close

async def connect(self, token):
async with client.connect(
Expand All @@ -41,7 +42,8 @@ async def connect(self, token):
if self._ready_event:
self._ready_event.set()

await websocket.wait_closed()
if not self._auto_close:
await websocket.wait_closed()

async def wait_ready(self):
# pylint: disable=duplicate-code
Expand Down
36 changes: 36 additions & 0 deletions tests/test_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ async def test_peer_connected_disconnected(
for event in event_checks:
self.assert_event(event)

@pytest.mark.asyncio
async def test_peer_connected_disconnected_deleted(
self, room_api: RoomApi, notifier: Notifier
):
event_checks = [
ServerMessageRoomCreated,
ServerMessagePeerConnected,
ServerMessagePeerDisconnected,
ServerMessageRoomDeleted,
]

assert_task = asyncio.create_task(assert_events(notifier, event_checks.copy()))

notifier_task = asyncio.create_task(notifier.connect())
await notifier.wait_ready()

_, room = room_api.create_room(
webhook_url=WEBHOOK_URL,
peerless_purge_timeout=2,
peer_disconnected_timeout=1,
)

peer_token, peer = room_api.add_peer(room.id, options=PeerOptionsWebRTC())

peer_socket = PeerSocket(server_address=SERVER_ADDRESS, auto_close=True)
peer_task = asyncio.create_task(peer_socket.connect(peer_token))

await peer_socket.wait_ready()

await assert_task
await cancel(peer_task)
await cancel(notifier_task)

for event in event_checks:
self.assert_event(event)

@pytest.mark.asyncio
async def test_peer_connected_room_deleted(
self, room_api: RoomApi, notifier: Notifier
Expand Down
4 changes: 4 additions & 0 deletions tests/test_room_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def test_no_params(self, room_api):
video_codec=None,
webhook_url=None,
peerless_purge_timeout=None,
peer_disconnected_timeout=None,
),
id=room.id,
peers=[],
Expand All @@ -172,6 +173,7 @@ def test_valid_params(self, room_api):
video_codec=RoomConfigVideoCodec(CODEC_H264),
webhook_url=None,
peerless_purge_timeout=None,
peer_disconnected_timeout=None,
),
id=room.id,
peers=[],
Expand Down Expand Up @@ -200,6 +202,7 @@ def test_valid_room_id(self, room_api):
video_codec=None,
webhook_url=None,
peerless_purge_timeout=None,
peer_disconnected_timeout=None,
),
id=room_id,
peers=[],
Expand Down Expand Up @@ -254,6 +257,7 @@ def test_valid(self, room_api: RoomApi):
video_codec=None,
webhook_url=None,
peerless_purge_timeout=None,
peer_disconnected_timeout=None,
),
) == room_api.get_room(room.id)

Expand Down