From 81c7347cc17cfd29f4dd34e9c632dcdd2e0e802b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= Date: Mon, 15 Apr 2024 15:56:32 +0200 Subject: [PATCH 1/4] Update client --- jellyfish/_openapi_client/models/room_config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/jellyfish/_openapi_client/models/room_config.py b/jellyfish/_openapi_client/models/room_config.py index 6f0985e..6916d29 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 disconnects. 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, From 2e402bdb552d2e276c0ea6ee907da36bb38093c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= Date: Tue, 16 Apr 2024 08:18:49 +0200 Subject: [PATCH 2/4] Add test for peer_disconnected_timeout --- jellyfish/api/_room_api.py | 4 ++++ tests/support/peer_socket.py | 10 +++++++--- tests/test_notifier.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/jellyfish/api/_room_api.py b/jellyfish/api/_room_api.py index a746cb9..dff2e16 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/peer_socket.py b/tests/support/peer_socket.py index 768e015..99df779 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,10 @@ async def connect(self, token): if self._ready_event: self._ready_event.set() - await websocket.wait_closed() + if self._auto_close: + websocket.close() + else: + await websocket.wait_closed() async def wait_ready(self): # pylint: disable=duplicate-code @@ -51,4 +55,4 @@ async def wait_ready(self): if self._ready_event is None: self._ready_event = asyncio.Event() - await self._ready_event.wait() + await self._ready_event.wait() \ No newline at end of file diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 1ac62de..8145078 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -150,6 +150,38 @@ 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=1, 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 From d0f422df03a9452319fbeac86bcbe9b7bec1244d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= Date: Tue, 16 Apr 2024 08:26:35 +0200 Subject: [PATCH 3/4] Run formatter --- jellyfish/api/_room_api.py | 2 +- tests/support/peer_socket.py | 4 ++-- tests/test_notifier.py | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/jellyfish/api/_room_api.py b/jellyfish/api/_room_api.py index dff2e16..5ae754c 100644 --- a/jellyfish/api/_room_api.py +++ b/jellyfish/api/_room_api.py @@ -89,7 +89,7 @@ def create_room( video_codec=video_codec, webhook_url=webhook_url, peerless_purge_timeout=peerless_purge_timeout, - peer_disconnected_timeout=peer_disconnected_timeout + peer_disconnected_timeout=peer_disconnected_timeout, ) resp = self._request(room_create_room, json_body=room_config) diff --git a/tests/support/peer_socket.py b/tests/support/peer_socket.py index 99df779..d9e411e 100644 --- a/tests/support/peer_socket.py +++ b/tests/support/peer_socket.py @@ -14,7 +14,7 @@ class PeerSocket: - def __init__(self, server_address, auto_close = False): + def __init__(self, server_address, auto_close=False): self._server_address = server_address self._ready = False @@ -55,4 +55,4 @@ async def wait_ready(self): if self._ready_event is None: self._ready_event = asyncio.Event() - await self._ready_event.wait() \ No newline at end of file + await self._ready_event.wait() diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 8145078..4a91884 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -166,7 +166,11 @@ async def test_peer_connected_disconnected_deleted( notifier_task = asyncio.create_task(notifier.connect()) await notifier.wait_ready() - _, room = room_api.create_room(webhook_url=WEBHOOK_URL, peerless_purge_timeout=1, peer_disconnected_timeout=1) + _, room = room_api.create_room( + webhook_url=WEBHOOK_URL, + peerless_purge_timeout=1, + peer_disconnected_timeout=1, + ) peer_token, peer = room_api.add_peer(room.id, options=PeerOptionsWebRTC()) From b86887409a091cad184a0b092b8e2ac1b163e249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= Date: Wed, 17 Apr 2024 12:36:25 +0200 Subject: [PATCH 4/4] Fix tests --- jellyfish/_openapi_client/models/room_config.py | 2 +- tests/support/asyncio_utils.py | 2 +- tests/support/peer_socket.py | 4 +--- tests/test_notifier.py | 2 +- tests/test_room_api.py | 4 ++++ 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/jellyfish/_openapi_client/models/room_config.py b/jellyfish/_openapi_client/models/room_config.py index 6916d29..58eee29 100644 --- a/jellyfish/_openapi_client/models/room_config.py +++ b/jellyfish/_openapi_client/models/room_config.py @@ -16,7 +16,7 @@ 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 disconnects. If not provided, this feature is disabled.""" + """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 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 d9e411e..c6d401b 100644 --- a/tests/support/peer_socket.py +++ b/tests/support/peer_socket.py @@ -42,9 +42,7 @@ async def connect(self, token): if self._ready_event: self._ready_event.set() - if self._auto_close: - websocket.close() - else: + if not self._auto_close: await websocket.wait_closed() async def wait_ready(self): diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 4a91884..260f91a 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -168,7 +168,7 @@ async def test_peer_connected_disconnected_deleted( _, room = room_api.create_room( webhook_url=WEBHOOK_URL, - peerless_purge_timeout=1, + peerless_purge_timeout=2, peer_disconnected_timeout=1, ) 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)