Skip to content

Commit

Permalink
Add test cases for receiving broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Jan 22, 2024
1 parent c6bd237 commit 2a635ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/slipstream/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ defmodule Slipstream.IntegrationTest do
@timeout
end

test "a regular broadcast may be received from the server", c do
topic = c.good_topic

:ok = join(c.pid, topic)
assert_receive {@client, :joined, ^topic, _reply}, @timeout

:ok = GenServer.cast(c.pid, {:push, topic, "broadcast", %{}})

assert_receive {@client, :received_message, ^topic, "broadcast event",
%{"hello" => "everyone!"}},
@timeout
end

test "a binary broadcast may be received from the server", c do
topic = c.good_topic

:ok = join(c.pid, topic)
assert_receive {@client, :joined, ^topic, _reply}, @timeout

:ok = GenServer.cast(c.pid, {:push, topic, "binary broadcast", %{}})

assert_receive {@client, :received_message, ^topic, "broadcast event",
{:binary, "🏴‍☠️"}},
@timeout
end

test "a connection may be disconnected", c do
topic = c.good_topic

Expand Down
22 changes: 22 additions & 0 deletions test/support/lib/slipstream_web/channels/test_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ defmodule SlipstreamWeb.TestChannel do
raise "oooooooohnnnnnnnnnnnnnoooooooooooooooooooooooooooooo"
end

def handle_in("broadcast", _params, socket) do
:ok =
SlipstreamWeb.Endpoint.broadcast!(
socket.assigns.topic,
"broadcast event",
%{hello: "everyone!"}
)

{:noreply, socket}
end

def handle_in("binary broadcast", _params, socket) do
:ok =
SlipstreamWeb.Endpoint.broadcast!(
socket.assigns.topic,
"broadcast event",
{:binary, "🏴‍☠️"}
)

{:noreply, socket}
end

def handle_in("stop", _params, socket) do
{:stop, :normal, socket}
end
Expand Down

0 comments on commit 2a635ee

Please sign in to comment.