Testing supervision trees

My friends and I built a fun little app to keep track of our company’s bus and it’s been super fun and works great! I’m sold on elixir! But I’m having trouble finding the right way to test its supervision tree. Does anybody have an example of testing that say, if a GenServer crashes, its state gets saved by another process? I looked through the process docs and am not really finding the right way to kill the GenServer between each test. Does anybody have a good example of testing that out in the wild?

Elixir ~> 1.1
Phoenix ~> 1.0.3

1 Like

Check out the OTP docs on Elixir’s website. You can kill the process using Process.exit(pid, :shutdown) and watch it restart using :observer.start from IEx.


Thanks @uranther! I should’ve clarified, it works great in practice. I’m just trying to get a feel for writing automated tests for this. I’ve played around with killing the process in the test env, but it seems clunky. Any examples I could glean from?

1 Like

There are automated tests within the OTP docs on Elixir’s website. It’s a little tricky, because you have to wait for the process to actually die in order to check that it died.

For example, see this test from Simple One-for-one Supervisors

test "removes bucket on crash", %{registry: registry} do
    KV.Registry.create(registry, "shopping")
    {:ok, bucket} = KV.Registry.lookup(registry, "shopping")

    # Stop the bucket with non-normal reason
    Process.exit(bucket, :shutdown)

    # Wait until the bucket is dead
    ref = Process.monitor(bucket)
    assert_receive {:DOWN, ^ref, _, _, _}

    assert KV.Registry.lookup(registry, "shopping") == :error