Skip to content

Commit bbf9552

Browse files
author
José Valim
committed
Add tests for add_process_handler/2
Signed-off-by: José Valim <[email protected]>
1 parent e621b58 commit bbf9552

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

lib/elixir/lib/gen_event.ex

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -601,19 +601,15 @@ defmodule GenEvent do
601601
reply(tag, reply)
602602
loop(parent, name, handlers, debug, hib)
603603
{_from, tag, {:add_handler, handler, args}} ->
604-
{hib, reply, handlers} = server_add_handler(handler, args, handlers, nil)
605-
reply(tag, reply)
606-
loop(parent, name, handlers, debug, hib)
607-
{_from, tag, {:add_handler, handler, args, notify}} ->
608-
{hib, reply, handlers} = server_add_handler(handler, args, handlers, notify)
604+
{hib, reply, handlers} = server_add_handler(handler, args, handlers)
609605
reply(tag, reply)
610606
loop(parent, name, handlers, debug, hib)
611607
{_from, tag, {:add_mon_handler, handler, args, notify}} ->
612608
{hib, reply, handlers} = server_add_mon_handler(handler, args, handlers, notify)
613609
reply(tag, reply)
614610
loop(parent, name, handlers, debug, hib)
615-
{_from, tag, {:add_process_handler, pid, notify, ref}} ->
616-
{hib, reply, handlers} = server_add_process_handler(pid, handlers, notify, ref)
611+
{_from, tag, {:add_process_handler, pid, notify}} ->
612+
{hib, reply, handlers} = server_add_process_handler(pid, handlers, notify)
617613
reply(tag, reply)
618614
loop(parent, name, handlers, debug, hib)
619615
{_from, tag, {:delete_handler, handler, args}} ->
@@ -739,13 +735,13 @@ defmodule GenEvent do
739735
IO.puts dev, "*DBG* #{inspect name}: #{inspect dbg}"
740736
end
741737

742-
defp server_add_handler({module, id}, args, handlers, notify) do
743-
handler = handler(module: module, id: {module, id}, pid: notify)
738+
defp server_add_handler({module, id}, args, handlers) do
739+
handler = handler(module: module, id: {module, id})
744740
do_add_handler(module, handler, args, handlers, :ok)
745741
end
746742

747-
defp server_add_handler(module, args, handlers, notify) do
748-
handler = handler(module: module, id: module, pid: notify)
743+
defp server_add_handler(module, args, handlers) do
744+
handler = handler(module: module, id: module)
749745
do_add_handler(module, handler, args, handlers, :ok)
750746
end
751747

@@ -761,8 +757,8 @@ defmodule GenEvent do
761757
do_add_handler(module, handler, args, handlers, :ok)
762758
end
763759

764-
defp server_add_process_handler(pid, handlers, notify, ref) do
765-
ref = ref || Process.monitor(pid)
760+
defp server_add_process_handler(pid, handlers, notify) do
761+
ref = Process.monitor(pid)
766762
handler = handler(module: GenEvent.Stream, id: {self(), ref},
767763
pid: notify, ref: ref)
768764
do_add_handler(GenEvent.Stream, handler, {pid, ref}, handlers, {self(), ref})
@@ -779,7 +775,7 @@ defmodule GenEvent do
779775
if sup do
780776
server_add_mon_handler(module2, {args2, state}, handlers, sup)
781777
else
782-
server_add_handler(module2, {args2, state}, handlers, nil)
778+
server_add_handler(module2, {args2, state}, handlers)
783779
end
784780
end
785781

lib/elixir/lib/gen_event/stream.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defimpl Enumerable, for: GenEvent.Stream do
8181
defp start(%{manager: manager} = stream) do
8282
try do
8383
{:ok, {pid, ref}} = :gen.call(manager, self(),
84-
{:add_process_handler, self(), self(), nil}, :infinity)
84+
{:add_process_handler, self(), self()}, :infinity)
8585
mon_ref = Process.monitor(pid)
8686
{pid, ref, mon_ref}
8787
catch

lib/elixir/test/elixir/gen_event_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,23 @@ defmodule GenEventTest do
432432
Logger.add_backend(:console, flush: true)
433433
end
434434

435+
test "add_process_handler/2 with GenEvent" do
436+
{:ok, snd} = GenEvent.start_link()
437+
GenEvent.add_handler(snd, ReplyHandler, {self(), false})
438+
439+
{:ok, fst} = GenEvent.start_link()
440+
:gen.call(fst, self(), {:add_process_handler, snd, snd})
441+
442+
assert GenEvent.notify(fst, :hello) == :ok
443+
assert_receive {:event, :hello}
444+
445+
assert GenEvent.ack_notify(fst, :hello) == :ok
446+
assert_receive {:event, :hello}
447+
448+
assert GenEvent.sync_notify(fst, :hello) == :ok
449+
assert_received {:event, :hello}
450+
end
451+
435452
test ":sys.get_status/2" do
436453
{:ok, pid} = GenEvent.start(name: :my_gen_event_name)
437454
:ok = GenEvent.add_handler(pid, {ReplyHandler, :ok}, {self(), true})

0 commit comments

Comments
 (0)