Skip to content

Instantly share code, notes, and snippets.

@TylerPachal
Last active November 9, 2017 19:05
Show Gist options
  • Save TylerPachal/d769b0e48ef64131e0585b2d33877487 to your computer and use it in GitHub Desktop.
Save TylerPachal/d769b0e48ef64131e0585b2d33877487 to your computer and use it in GitHub Desktop.
Testing MySupervisor and verifying that it restarts dying children processes
# Start a supervisor with two child specifications
iex(1)> {:ok, pid} = MySupervisor.start_link([{Parser, :start_link, []}, {Parser, :start_link, []}])
{:ok, #PID<0.112.0>}
# See the two processes that the supervisor started
iex(2)> MySupervisor.list_processes(pid)
%{#PID<0.113.0> => {Parser, :start_link, []},
#PID<0.114.0> => {Parser, :start_link, []}}
# Get a reference to one of the Parser process pid
iex(3)> parser_pid = MySupervisor.list_processes(pid) |> Map.keys |> List.first
# Crash the Parser process
iex(4)> GenServer.call(parser_pid, "not a numbner")
** (exit) exited in: GenServer.call(#PID<0.113.0>, "not a numbner", 5000)
# Stacktrace here
# Make sure that the supervisor removed the dead process and started a new one in its place
iex(5)> MySupervisor.list_processes(pid)
%{#PID<0.114.0> => {Parser, :start_link, []},
#PID<0.118.0> => {Parser, :start_link, []}}
# Get a reference to the new process
iex(6)> new_parser_pid = MySupervisor.list_processes(pid) |> Map.keys |> List.last
#PID<0.118.0>
# Verify the new process is running and working
iex(7)> GenServer.call(new_parser_pid, "99")
99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment