@@ -34,7 +34,8 @@ defmodule Phoenix.PresenceTest do
3434 Phoenix.Presence . init ( {
3535 __MODULE__ ,
3636 __MODULE__ . TaskSupervisor ,
37- PresPub
37+ PresPub ,
38+ Phoenix.Channel.Server
3839 } )
3940 end
4041
@@ -43,13 +44,27 @@ defmodule Phoenix.PresenceTest do
4344 end
4445 end
4546
47+ defmodule CustomDispatcher do
48+ def dispatch ( entries , from , message ) do
49+ for { pid , _ } <- entries , pid != from , do: send ( pid , { :custom_dispatcher , message } )
50+
51+ :ok
52+ end
53+ end
54+
55+ defmodule CustomDispatcherPresence do
56+ use Phoenix.Presence , otp_app: :phoenix , dispatcher: CustomDispatcher
57+ end
58+
4659 Application . put_env ( :phoenix , MyPresence , pubsub_server: PresPub )
4760 Application . put_env ( :phoenix , MetasPresence , pubsub_server: PresPub )
61+ Application . put_env ( :phoenix , CustomDispatcherPresence , pubsub_server: PresPub )
4862
4963 setup_all do
5064 start_supervised! ( { Phoenix.PubSub , name: PresPub , pool_size: 1 } )
5165 start_supervised! ( MyPresence )
5266 start_supervised! ( MetasPresence )
67+ start_supervised! ( CustomDispatcherPresence )
5368 { :ok , pubsub: PresPub }
5469 end
5570
@@ -174,6 +189,40 @@ defmodule Phoenix.PresenceTest do
174189 assert MyPresence . list ( topic ) == % { }
175190 end
176191
192+ test "handle_diff with custom dispatcher" , % { topic: topic } = config do
193+ pid = spawn ( fn -> :timer . sleep ( :infinity ) end )
194+ Phoenix.PubSub . subscribe ( config . pubsub , topic )
195+ start_supervised! ( { DefaultPresence , pubsub_server: config . pubsub } )
196+ CustomDispatcherPresence . track ( pid , topic , "u1" , % { name: "u1" } )
197+
198+ assert_receive { :custom_dispatcher ,
199+ % Broadcast {
200+ topic: ^ topic ,
201+ event: "presence_diff" ,
202+ payload: % {
203+ joins: % { "u1" => % { metas: [ % { name: "u1" , phx_ref: u1_ref } ] } } ,
204+ leaves: % { }
205+ }
206+ } }
207+
208+ assert % { "u1" => % { metas: [ % { name: "u1" , phx_ref: ^ u1_ref } ] } } =
209+ CustomDispatcherPresence . list ( topic )
210+
211+ Process . exit ( pid , :kill )
212+
213+ assert_receive { :custom_dispatcher ,
214+ % Broadcast {
215+ topic: ^ topic ,
216+ event: "presence_diff" ,
217+ payload: % {
218+ joins: % { } ,
219+ leaves: % { "u1" => % { metas: [ % { name: "u1" , phx_ref: ^ u1_ref } ] } }
220+ }
221+ } }
222+
223+ assert CustomDispatcherPresence . list ( topic ) == % { }
224+ end
225+
177226 test "untrack with pid" , % { topic: topic } = config do
178227 Phoenix.PubSub . subscribe ( config . pubsub , config . topic )
179228 MyPresence . track ( self ( ) , config . topic , "u1" , % { } )
0 commit comments