Reputation: 593
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.
Thanks!
Upvotes: 2
Views: 244
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
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