-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_channel_test.exs
68 lines (53 loc) · 1.81 KB
/
demo_channel_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule WebsocketDemoWeb.DemoChannelTest do
use WebsocketDemoWeb.ChannelCase, async: true
alias WebsocketDemoWeb.DemoChannel
describe "join demo:id" do
test "any socket can join the channel" do
{:ok, _, socket} =
socket(nil, %{})
|> subscribe_and_join(DemoChannel, "demo:1")
leave(socket)
end
test "a tick is immediately invoked" do
{:ok, _, _socket} =
socket(nil, %{})
|> subscribe_and_join(DemoChannel, "demo:1")
expected = %{value: 0}
assert_push("tick", ^expected)
end
end
describe "handle_info :tick" do
test "another tick is invoked for 5s from now" do
{:ok, _, socket} =
socket(nil, %{})
|> subscribe_and_join(DemoChannel, "demo:1")
assert :sys.get_state(socket.channel_pid).assigns.current_tick == 1
Process.send(socket.channel_pid, :tick, [])
assert :sys.get_state(socket.channel_pid).assigns.current_tick == 2
assert Process.read_timer(socket.assigns.tick_timer) == 5000
end
end
describe "handle_in ping" do
test "a pong response is returned" do
{:ok, _, socket} =
socket(nil, %{})
|> subscribe_and_join(DemoChannel, "demo:1")
ref = push(socket, "ping", %{})
assert_reply(ref, :ok, %{response: "pong"}, 1000)
leave(socket)
end
end
describe "handle_in ping:ms" do
test "a pong response is returned after the ms time" do
{:ok, _, socket} =
socket(nil, %{})
|> subscribe_and_join(DemoChannel, "demo:1")
ref = push(socket, "ping:300", %{})
expected = %{delay: 300, response: "pong"}
# Give a few ms between when we expect it to receive, to account for async
refute_reply(ref, :ok, ^expected, 299)
assert_reply(ref, :ok, ^expected, 10)
leave(socket)
end
end
end