Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active April 5, 2017 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoa/22635d48acb27314869d16722997c1b4 to your computer and use it in GitHub Desktop.
Save thiagoa/22635d48acb27314869d16722997c1b4 to your computer and use it in GitHub Desktop.
# This works for the use case below, but not for every timeout-related task.
defmodule Timeout do
@default_timeout 1_000
@count 0
def wait(cond, on_after, timeout \\ @default_timeout) do
result = call time(), @count, timeout, cond, cond.()
on_after.(result)
end
defp call(_, count, timeout, _, nil) when count >= timeout do
raise "Timed out after #{timeout} milliseconds!"
end
defp call(start_time, _, timeout, cond, nil) do
call start_time, time() - start_time, timeout, cond, cond.()
end
defp call(_, _, _, _, result), do: result
defp time, do: :os.system_time(:millisecond)
end
defmodule Assertions do
use ExUnit.Case
def assert_supervised(pid) do
find_pid = fn -> Process.whereis(pid) end
pid = find_pid.()
assert pid, "Process should be alive but is not"
pid |> Process.monitor
pid |> Process.exit(:kill)
assert_receive {:DOWN, _, _, _, _}
Timeout.wait find_pid, fn result ->
assert Process.alive?(result)
end
end
end
defmodule HealhCheck.SupervisorTest do
use ExUnit.Case
import Assertions
test "process is alive by default" do
assert HealthCheck.Daemon.check(:hc) == :ok
end
test "process is supervised one_for_one" do
assert_supervised :hc
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment