arami
arami

Reputation: 593

Writing tests that don't fail when Supervised processes die

I'm writing a few tests in ExUnit to illustrate how different Supervisor strategies work. I had planned to test results by intentionally causing spawned processes to fail, and then testing the restarted processes' output.

Thus far I have been unsuccessful in creating passing tests, as the initial process failure causes the test to fail. I have tried capturing the errors (try/catch) in both the Supervisor/GenServer implementation and the test implementation, but I have not been able to capture any and avoid the test failure.

  1. Is there any way to capture these errors so that they do not trigger a test failure?
  2. Is there a better/different means of testing different supervisor strategies?

Thanks!

Upvotes: 2

Views: 244

Answers (2)

arami
arami

Reputation: 593

Because I was intentionally causing a process to fail and wanted to ignore this failure within the ExUnit test, I ended up using catch_exit/1 to prevent the test process from failing.

Upvotes: 1

José Valim
José Valim

Reputation: 51369

You need to careful with your links. When you start a supervisor, it is linked to the current process, so if you crash the supervisor (or any other linked process), it will cause the test to crash too.

You can change this behaviour by setting Process.flag(:trap_exit, true) now links won't trigger crashes and instead you will be able to find messages of format {:EXIT, pid, reason} in your mailbox.

This is a fine approach for testing but for production or in general you likely want to setup some sort of monitor.

Upvotes: 1

Related Questions