/
channel_test.ex
370 lines (279 loc) · 10.9 KB
/
channel_test.ex
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
defmodule Phoenix.ChannelTest do
@moduledoc """
Conveniences for testing Phoenix channels.
In channel tests, we interact with channels via process
communication, sending and receiving messages. It is also
common to subscribe to the same topic the channel subscribes
to, allowing us to assert if a given message was broadcast
or not.
## Channel testing
To get started, define the module attribute `@endpoint`
in your test case pointing to your application endpoint.
Then you can directly `subscribe_and_join/3` topics and
channels:
{:ok, _, socket} =
subscribe_and_join(RoomChannel, "rooms:lobby", %{"id" => 3})
The function above will subscribe the current test process
to the "rooms:lobby" topic and start a channel in another
process. It returns `{:ok, reply, socket}` or `{:error, reply}`.
Now, in the same way the channel has a socket representing
communication it will push to the client. Our test has a
socket representing communication to be pushed to the server.
For example, we can use the `push/3` function in the test
to push messages to the channel (it will invoke `handle_in/3`):
push socket, "my_event", %{"some" => "data"}
Similarly, we can broadcast messages from the test itself
on the topic that both test and channel are subscribed to,
triggering `handle_out/3` on the channel:
broadcast_from socket, "my_event", %{"some" => "data"}
> Note only `broadcast_from/3` and `broadcast_from!/3` are
available in tests to avoid broadcast messages to be resent
to the test process.
While the functions above are pushing data to the channel
(server) we can use `assert_push/3` to verify the channel
pushed a message to the client:
assert_push "my_event", %{"some" => "data"}
Or even assert something was broadcast into pubsub:
assert_broadcast "my_event", %{"some" => "data"}
Finally, every time a message is pushed to the channel,
a reference is returned. We can use this reference to
assert a particular reply was sent from the server:
ref = push socket, "counter", %{}
assert_reply ref, :ok, %{"counter" => 1}
## Checking side-effects
Often one may want to do side-effects inside channels,
like writing to the database, and verify those side-effects
during their tests.
Imagine the following `handle_in/3` inside a channel:
def handle_in("publish", %{"id" => id}, socket) do
Repo.get!(Post, id) |> Post.publish() |> Repo.update!()
{:noreply, socket}
end
Because the whole communication is asynchronous, the
following test would be very brittle:
push socket, "publish", %{"id" => 3}
assert Repo.get_by(Post, id: 3, published: true)
The issue is that we have no guarantees the channel has
done processing our message after calling `push/3`. The
best solution is to assert the channel sent us a reply
before doing any other assertion. First change the
channel to send replies:
def handle_in("publish", %{"id" => id}, socket) do
Repo.get!(Post, id) |> Post.publish() |> Repo.update!()
{:reply, :ok, socket}
end
Then expect them in the test:
ref = push socket, "publish", %{"id" => 3}
assert_reply ref, :ok
assert Repo.get_by(Post, id: 3, published: true)
## Leave and close
This module also provides functions to simulate leaving
and closing a channel. Once you leave or close a channel,
because the channel is linked to the test process on join,
it will crash the test process:
leave(socket)
** (EXIT from #PID<...>) {:shutdown, :leave}
You can avoid this by unlinking the channel process in
the test:
Process.unlink(socket.channel_pid)
Notice `leave/1` is async, so it will also return a
reference which you can use to check for a reply:
ref = leave(socket)
assert_reply ref, :ok
On the other hand, close is always sync and it will
return only after the channel process is guaranteed to
have been terminated:
:ok = close(socket)
This mimics the behaviour existing in clients.
"""
alias Phoenix.Socket
alias Phoenix.Socket.Message
alias Phoenix.Socket.Broadcast
alias Phoenix.Socket.Reply
alias Phoenix.Channel.Server
defmodule NoopSerializer do
@behaviour Phoenix.Transports.Serializer
def fastlane!(%Broadcast{} = msg) do
%Message{
topic: msg.topic,
event: msg.event,
payload: msg.payload
}
end
def encode!(%Reply{} = reply), do: reply
def encode!(%Message{} = msg), do: msg
def decode!(message, _opts \\ []), do: message
end
@doc false
defmacro __using__(_) do
quote do
import Phoenix.ChannelTest
end
end
@doc """
Subscribes to the given topic and joins the channel
under the given topic and payload.
By subscribing to the topic, we can use `assert_broadcast/3`
to verify a message has been sent through the pubsub layer.
By joining the channel, we can interact with it directly.
The given channel is joined in a separate process which is
linked to the test process.
It returns `{:ok, reply, socket}` or `{:error, reply}`.
The endpoint is read from the `@endpoint` variable.
"""
defmacro subscribe_and_join(channel, topic, payload \\ Macro.escape(%{})) do
quote do
subscribe_and_join(@endpoint, unquote(channel), unquote(topic), unquote(payload))
end
end
@doc """
Subscribes to the given topic and joins the channel powered
by the pubsub server in endpoint under the given topic and
payload.
This is useful when you need to join a channel in different
enpoints, in practice, `subscribe_and_join/3` is recommended.
"""
def subscribe_and_join(endpoint, channel, topic, payload) do
unless endpoint do
raise "module attribute @endpoint not set for subscribe_and_join/3"
end
endpoint.subscribe(self(), topic)
join(endpoint, channel, topic, payload)
end
@doc """
Joins the channel under the given topic and payload.
The given channel is joined in a separate process
which is linked to the test process.
It returns `{:ok, reply, socket}` or `{:error, reply}`.
The endpoint is read from the `@endpoint` variable.
"""
defmacro join(channel, topic, payload \\ Macro.escape(%{})) do
quote do
join(@endpoint, unquote(channel), unquote(topic), unquote(payload))
end
end
@doc """
Joins the channel powered by the pubsub server in
endpoint under the given topic and payload.
This is useful when you need to join a channel in
different enpoints, in practice, `join/3` is recommended.
"""
def join(endpoint, channel, topic, payload) do
unless endpoint do
raise "module attribute @endpoint not set for join/3"
end
socket = %Socket{serializer: NoopSerializer,
transport_pid: self(),
endpoint: endpoint,
pubsub_server: endpoint.__pubsub_server__(),
topic: topic,
channel: channel,
transport: __MODULE__}
case Server.join(socket, payload) do
{:ok, reply, pid} ->
{:ok, reply, %{socket | channel_pid: pid, joined: true}}
{:error, _} = error ->
error
end
end
@doc """
Pushes a message into the channel.
The triggers the `handle_in/3` callback in the channel.
## Examples
iex> push socket, "new_message", %{id: 1, content: "hello"}
:ok
"""
def push(socket, event, payload \\ %{}) do
ref = make_ref()
send(socket.channel_pid,
%Message{event: event, topic: socket.topic, ref: ref, payload: payload})
ref
end
@doc """
Emulates the client leaving the channel.
"""
def leave(socket) do
ref = make_ref()
Server.leave(socket.channel_pid, ref)
ref
end
@doc """
Emulates the client closing the channel.
Closing channels is synchronous and has a default timeout
of 5000 miliseconds.
"""
def close(socket, timeout \\ 5000) do
Server.close(socket.channel_pid, timeout)
end
@doc """
Broadcast event from pid to all subscribers of the socket topic.
The test process will not receive the published message. This triggers
the `handle_out/3` callback in the channel.
## Examples
iex> broadcast_from socket, "new_message", %{id: 1, content: "hello"}
:ok
"""
def broadcast_from(socket, event, message) do
%{pubsub_server: pubsub_server, topic: topic, transport_pid: transport_pid} = socket
Server.broadcast_from pubsub_server, transport_pid, topic, event, message
end
@doc """
Same as `broadcast_from/3` but raises if broadcast fails.
"""
def broadcast_from!(socket, event, message) do
%{pubsub_server: pubsub_server, topic: topic, transport_pid: transport_pid} = socket
Server.broadcast_from! pubsub_server, transport_pid, topic, event, message
end
@doc """
Asserts the channel has pushed a message back to the client
with the given event and payload under `timeout`.
Notice event and payload are patterns. This means one can write:
assert_push "some_event", %{"data" => _}
In the assertion above, we don't particularly care about
the data being sent, as long as something was sent.
The timeout is in miliseconds and defaults to 100ms.
"""
defmacro assert_push(event, payload, timeout \\ 100) do
quote do
assert_receive %Phoenix.Socket.Message{
event: unquote(event),
payload: unquote(payload)}, unquote(timeout)
end
end
@doc """
Asserts the channel has replies to the given message within
`timeout`.
Notice status and payload are patterns. This means one can write:
ref = push channel, "some_event"
assert_reply ref, :ok, %{"data" => _}
In the assertion above, we don't particularly care about
the data being sent, as long as something was replied.
The timeout is in miliseconds and defaults to 100ms.
"""
defmacro assert_reply(ref, status, payload \\ Macro.escape(%{}), timeout \\ 100) do
quote do
ref = unquote(ref)
assert_receive %Phoenix.Socket.Reply{
ref: ^ref,
status: unquote(status),
payload: unquote(payload)}, unquote(timeout)
end
end
@doc """
Asserts the channel has broadcast a message within `timeout`.
Before asserting anything was broadcast, we must first
subscribe to the topic of the channel in the test process:
@endpoint.subscribe(self(), "foo:ok")
Now we can match on event and payload as patterns:
assert_broadcast "some_event", %{"data" => _}
In the assertion above, we don't particularly care about
the data being sent, as long as something was sent.
The timeout is in miliseconds and defaults to 100ms.
"""
defmacro assert_broadcast(event, payload, timeout \\ 100) do
quote do
assert_receive %Phoenix.Socket.Broadcast{event: unquote(event),
payload: unquote(payload)}, unquote(timeout)
end
end
end