forked from doorgan/channel_handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.exs
282 lines (219 loc) · 8.27 KB
/
router_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
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
defmodule ChannelHandler.RouterTest do
use ExUnit.Case, async: true
test "routes calls to the right handlers" do
defmodule TestRouter do
use ChannelHandler.Router
alias ChannelHandler.Context
event("event", ChannelHandler.RouterTest.TestHandler, :event_fun)
event("catchall_event:*", ChannelHandler.RouterTest.TestHandler, :event_fun_catchall)
delegate("delegate", ChannelHandler.RouterTest.TestHandler)
delegate("plug_delegate:", ChannelHandler.RouterTest.TestHandler)
handle("handler", fn _payload, context, _socket ->
assert %Context{} = context
assert context.event == "handler"
:handler
end)
handle("catchall:*", fn event, _payload, context, _socket ->
assert %Context{} = context
assert event == "handler"
assert context.event == "catchall:handler"
:catchall_handler
end)
scope "scoped:" do
event("event", ChannelHandler.RouterTest.ScopedHandler, :event_fun)
event("catchall_event:*", ChannelHandler.RouterTest.ScopedHandler, :event_fun_catchall)
delegate("delegate", ChannelHandler.RouterTest.ScopedHandler)
handle("handler", fn _payload, context, _socket ->
assert context.event == "scoped:handler"
assert %Context{} = context
:scoped_handler
end)
end
scope "with_plug:" do
plug(&noop_plug/4)
event("event", ChannelHandler.RouterTest.WithPlugHandler, :event_fun)
event(
"catchall_event:*",
ChannelHandler.RouterTest.WithPlugHandler,
:event_fun_catchall
)
delegate("delegate", ChannelHandler.RouterTest.WithPlugHandler)
handle("handler", fn _payload, context, _socket ->
assert context.event == "with_plug:handler"
assert %Context{} = context
:with_plug_handler
end)
end
scope do
plug(&noop_plug/4)
handle("no_scope", fn _payload, context, _socket ->
assert context.event == "no_scope"
assert %Context{} = context
:no_scope
end)
end
delegate(ChannelHandler.RouterTest.DelegateHandler)
def noop_plug(socket, payload, context, _opts) do
assert %Context{} = context
send(self(), :plug_called)
{:cont, socket, payload, context}
end
end
defmodule TestHandler do
use ChannelHandler.Handler
alias ChannelHandler.Context
plug((&noop_plug/4) when event == "event")
def event_fun(_payload, context, _socket) do
assert %Context{} = context
assert context.event == "event"
:event
end
def event_fun_catchall(event, _payload, context, _socket) do
assert %Context{} = context
assert event == "event_name"
assert context.event == "catchall_event:event_name"
:event_catchall
end
def handle_in("event", _payload, context, _socket) do
assert %Context{} = context
assert context.event == "event"
assert context.full_event == "plug_delegate:event"
:delegate_plug
end
def handle_in(event, _payload, context, _socket) do
assert %Context{} = context
assert context.event == event
assert context.full_event == "delegate"
:delegate
end
def noop_plug(socket, payload, context, _opts) do
assert %Context{} = context
send(self(), :plug_called)
{:cont, socket, payload, context}
end
end
defmodule ScopedHandler do
alias ChannelHandler.Context
def event_fun(_payload, context, _socket) do
assert %Context{} = context
assert context.event == "scoped:event"
:scoped_event
end
def event_fun_catchall(event, _payload, context, _socket) do
assert %Context{} = context
assert event == "event_name"
assert context.event == "scoped:catchall_event:event_name"
:scoped_event_catchall
end
def handle_in(event, _payload, context, _socket) do
assert %Context{} = context
assert context.event == event
assert context.full_event == "scoped:delegate"
:scoped_delegate
end
end
defmodule WithPlugHandler do
use ChannelHandler.Handler
alias ChannelHandler.Context
plug((&module_plug/4) when action in [:event_fun, :event_fun_catchall])
def event_fun(_payload, context, _socket) do
assert %Context{} = context
assert context.event == "with_plug:event"
:with_plug_event
end
def event_fun_catchall(event, _payload, context, _socket) do
assert %Context{} = context
assert event == "event_name"
assert context.event == "with_plug:catchall_event:event_name"
:with_plug_event_catchall
end
def handle_in(event, _payload, context, _socket) do
assert %Context{} = context
assert context.event == event
assert context.full_event == "with_plug:delegate"
:with_plug_delegate
end
def module_plug(socket, payload, context, _opts) do
assert %Context{} = context
send(self(), :module_plug_called)
{:cont, socket, payload, context}
end
end
defmodule DelegateHandler do
use ChannelHandler.Handler
alias ChannelHandler.Context
def handle_in(_event, _payload, context, _socket) do
assert %Context{} = context
assert context.event == "fully_delegated"
:fully_delegated
end
end
:telemetry.attach_many(
:test,
[
[:channel_handler, :handle, :stop],
[:channel_handler, :event, :stop]
],
&__MODULE__.assert_telemetry/4,
%{}
)
assert TestRouter.handle_in("event", %{}, :socket) == :event
assert TestRouter.handle_in("catchall_event:event_name", %{}, :socket) == :event_catchall
assert TestRouter.handle_in("delegate", %{}, :socket) == :delegate
assert TestRouter.handle_in("plug_delegate:event", %{}, :socket) == :delegate_plug
assert_receive :plug_called
assert TestRouter.handle_in("handler", %{}, :socket) == :handler
assert TestRouter.handle_in("scoped:event", %{}, :socket) == :scoped_event
assert TestRouter.handle_in("scoped:catchall_event:event_name", %{}, :socket) ==
:scoped_event_catchall
assert TestRouter.handle_in("scoped:delegate", %{}, :socket) == :scoped_delegate
assert TestRouter.handle_in("scoped:handler", %{}, :socket) == :scoped_handler
assert TestRouter.handle_in("with_plug:event", %{}, :socket) == :with_plug_event
assert_receive :plug_called
assert_receive :module_plug_called
assert TestRouter.handle_in("with_plug:catchall_event:event_name", %{}, :socket) ==
:with_plug_event_catchall
assert_receive :plug_called
assert_receive :module_plug_called
assert TestRouter.handle_in("with_plug:delegate", %{}, :socket) == :with_plug_delegate
assert_receive :plug_called
refute_receive :module_plug_called
assert TestRouter.handle_in("with_plug:handler", %{}, :socket) == :with_plug_handler
assert_receive :plug_called
refute_receive :module_plug_called
assert TestRouter.handle_in("no_scope", %{}, :socket) == :no_scope
assert_receive :plug_called
assert TestRouter.handle_in("fully_delegated", %{}, :socket) == :fully_delegated
end
describe "join" do
defmodule JoinTestRouter do
use ChannelHandler.Router
channel("join_test:*")
join(fn topic, payload, socket ->
assert %Phoenix.Socket{} = socket
assert socket.assigns.__channel__ == "join_test:*"
assert topic == "topic"
assert payload == %{}
:ok
end)
end
defmodule JoinTestRouterNoChannel do
use ChannelHandler.Router
join(fn topic, payload, socket ->
assert %Phoenix.Socket{} = socket
assert socket.assigns.__channel__ == nil
assert topic == "topic"
assert payload == %{}
:ok
end)
end
test "defines the join function" do
assert JoinTestRouter.join("topic", %{}, %Phoenix.Socket{}) == :ok
assert JoinTestRouterNoChannel.join("topic", %{}, %Phoenix.Socket{}) == :ok
end
end
def assert_telemetry(_event, meas, meta, _config) do
assert meas.duration > 0
assert is_binary(meta.event)
end
end