pid = spawn(fn ->
receive do
{:call, {pid, ref}, msg} ->
Process.sleep(Enum.random(0..10_000))
send(pid, {:reply, ref, msg}
end
end)
ref = Process.monitor(pid)
send(pid, {:call, {self(), ref}, msg})
receive do
{^ref, reply} ->
Process.demonitor(ref, [:flush])
reply
{:DOWN, ^ref, :process, ^pid, status} ->
exit(status)
after
5_000 ->
Process.demonitor(ref, [:flush])
raise "timeout"
end