Skip to content

Commit 8480d29

Browse files
authored
Add peer_disconnected_timeout option in Room.create (#62)
1 parent d71c89d commit 8480d29

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

lib/jellyfish/room.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,16 @@ defmodule Jellyfish.Room do
7878
notifications from the room.
7979
* `:peerless_purge_timeout` - duration (in seconds) after which the room will be removed
8080
if no peers are connected. If not provided, this feature is disabled.
81+
* `:peer_disconnected_timeout` - duration (in seconds) after which the peer will be removed
82+
if it is disconnected. If not provided, this feature is disabled.
8183
"""
8284
@type options :: [
8385
room_id: String.t() | nil,
8486
max_peers: non_neg_integer() | nil,
8587
video_codec: :h264 | :vp8 | nil,
8688
webhook_url: String.t() | nil,
87-
peerless_purge_timeout: pos_integer() | nil
89+
peerless_purge_timeout: pos_integer() | nil,
90+
peer_disconnected_timeout: pos_integer() | nil
8891
]
8992

9093
@typedoc """
@@ -136,7 +139,8 @@ defmodule Jellyfish.Room do
136139
"maxPeers" => Keyword.get(opts, :max_peers),
137140
"videoCodec" => Keyword.get(opts, :video_codec),
138141
"webhookUrl" => Keyword.get(opts, :webhook_url),
139-
"peerlessPurgeTimeout" => Keyword.get(opts, :peerless_purge_timeout)
142+
"peerlessPurgeTimeout" => Keyword.get(opts, :peerless_purge_timeout),
143+
"peerDisconnectedTimeout" => Keyword.get(opts, :peer_disconnected_timeout)
140144
}),
141145
room_json <- Map.fetch!(data, "room"),
142146
jellyfish_address <- Map.fetch!(data, "jellyfish_address"),

test/jellyfish/notifier_test.exs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ defmodule Jellyfish.NotifierTest do
3030

3131
@max_peers 10
3232
@video_codec :vp8
33+
@peerless_purge_timeout_s 1
34+
@peer_disconnected_timeout_s 1
3335
@webhook_port 4000
3436
@webhook_host Application.compile_env!(:jellyfish_server_sdk, :webhook_address)
3537
@webhook_address "http://#{@webhook_host}:#{@webhook_port}/"
@@ -140,6 +142,45 @@ defmodule Jellyfish.NotifierTest do
140142
:ok = Room.delete(client, room_id)
141143
end
142144

145+
test "when peer connects and then disconnects peer is removed with timeout", %{
146+
client: client
147+
} do
148+
{:ok, %Jellyfish.Room{id: room_id}, jellyfish_address} =
149+
Room.create(client,
150+
max_peers: @max_peers,
151+
video_codec: @video_codec,
152+
webhook_url: @webhook_address,
153+
peerless_purge_timeout: @peerless_purge_timeout_s,
154+
peer_disconnected_timeout_s: @peer_disconnected_timeout_s
155+
)
156+
157+
{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = Room.add_peer(client, room_id, @peer_opts)
158+
159+
{:ok, peer_ws} = WS.start_link("ws://#{jellyfish_address}/socket/peer/websocket")
160+
161+
auth_request = %PeerMessage{content: {:auth_request, %AuthRequest{token: peer_token}}}
162+
:ok = WS.send_frame(peer_ws, auth_request)
163+
{room_id, peer_id, peer_ws}
164+
165+
assert_receive {:jellyfish,
166+
%PeerConnected{peer_id: ^peer_id, room_id: ^room_id} = peer_connected}
167+
168+
assert_receive {:webhook, ^peer_connected}, 2_500
169+
170+
GenServer.stop(peer_ws)
171+
172+
assert_receive {:jellyfish,
173+
%PeerDisconnected{peer_id: ^peer_id, room_id: ^room_id} = peer_disconnected},
174+
1_000
175+
176+
assert_receive {:webhook, ^peer_disconnected}, 2_500
177+
178+
assert_receive {:jellyfish, %RoomDeleted{room_id: ^room_id} = room_deleted},
179+
2_500
180+
181+
assert_receive {:webhook, ^room_deleted}, 2_500
182+
end
183+
143184
@tag :file_component_sources
144185
test "TrackAdded and TrackRemoved are sent when adding and removing FileComponent", %{
145186
client: client

test/jellyfish/room_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ defmodule Jellyfish.RoomTest do
186186
assert {:error, "Request failed: Expected videoCodec to be 'h264' or 'vp8', got: opus"} =
187187
Room.create(client, video_codec: @invalid_video_codec)
188188
end
189+
190+
test "when request is invalid, peerless purge timeout", %{client: client} do
191+
assert {:error,
192+
"Request failed: Expected peerlessPurgeTimeout to be a positive integer, got: -25"} =
193+
Room.create(client, peerless_purge_timeout: -25)
194+
end
195+
196+
test "when request is invalid, peer disconnected timeout", %{client: client} do
197+
assert {:error,
198+
"Request failed: Expected peerDisconnectedTimeout to be a positive integer, got: -25"} =
199+
Room.create(client, peer_disconnected_timeout: -25)
200+
end
189201
end
190202

191203
describe "Room.delete/2" do

0 commit comments

Comments
 (0)