Asserting exit/logg and state of running process

Im trying to write some tests for a process that uses the Connection behavior. The tests start to get really complicated. When I call RestConnection the start_link function returns imideatly. Then a connection should be estamblised and a login token recied should be set in the process state. Now I want to assert on three things in the test below wich is a negative case where the connection is down.

I want to assert the following:

  1. Check error is logged
  2. Make sure an :EXIT is raised
    3: Check the state process state that the token key is not set

What I have so far is an attempt at this that is not working.

  test "connection down", %{bypass: bypass} = context do
    catch_exit do
      output =
        capture_log(fn ->
          {:ok, _pid} = RestConnection.start_link("user", "pwd", "key")

      pid = context[:pid]
      assert output =~ "[error] Login failed"
      assert_received({:EXIT, ^pid, :login_failed})

catch_exit is about catching an exit raised by the current process itself.

You want to do something like Process.flag(:trap_exit, true) before you start the process so the crash from the other process doesn’t bring the test process down.

TL;DR: you can only catch your own exits, for broken links you need to trap them.

1 Like

Maybe we should add this to catch_exit docs? Could you please send a PR?

Thanks got it working now, PR opened

1 Like