From 4c7bfe9c9c62295583c9a9492c3bf1add5c69b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= <56085570+Rados13@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:36:12 +0200 Subject: [PATCH] RTC-512 add peer_disconnected_timeout option in RoomApi.create_room (#35) * Update client * Add test for peer_disconnected_timeout * Run formatter * Fix tests --- .../_openapi_client/models/room_config.py | 8 +++++ jellyfish/api/_room_api.py | 4 +++ tests/support/asyncio_utils.py | 2 +- tests/support/peer_socket.py | 6 ++-- tests/test_notifier.py | 36 +++++++++++++++++++ tests/test_room_api.py | 4 +++ 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/jellyfish/_openapi_client/models/room_config.py b/jellyfish/_openapi_client/models/room_config.py index 6f0985e..58eee29 100644 --- a/jellyfish/_openapi_client/models/room_config.py +++ b/jellyfish/_openapi_client/models/room_config.py @@ -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 @@ -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 @@ -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: @@ -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) @@ -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, diff --git a/jellyfish/api/_room_api.py b/jellyfish/api/_room_api.py index a746cb9..5ae754c 100644 --- a/jellyfish/api/_room_api.py +++ b/jellyfish/api/_room_api.py @@ -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 @@ -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) diff --git a/tests/support/asyncio_utils.py b/tests/support/asyncio_utils.py index f0844f9..fbdfa06 100644 --- a/tests/support/asyncio_utils.py +++ b/tests/support/asyncio_utils.py @@ -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): diff --git a/tests/support/peer_socket.py b/tests/support/peer_socket.py index 768e015..c6d401b 100644 --- a/tests/support/peer_socket.py +++ b/tests/support/peer_socket.py @@ -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( @@ -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 diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 1ac62de..260f91a 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -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 diff --git a/tests/test_room_api.py b/tests/test_room_api.py index 7aeaece..f0dde83 100644 --- a/tests/test_room_api.py +++ b/tests/test_room_api.py @@ -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=[], @@ -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=[], @@ -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=[], @@ -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)