In the project I’m currently working on, I have a piece of code responsible for calculating some statistics and sending it via Phoenix Channels after changing adding or removing some records in the API call.
Since don’t want to wait for this to render the response, I’m using
Task.Supervisor.async_nolink to do this.
There is one problem though when I run my tests. Since I’m using Ecto Repo in the async task, its process have to share a connection with the test process, which gives a following error:
(If you want to find out more about connection ownership, you can read this article)
Disabling the task in tests wasn’t an option, so I figured out that I can solve this problem by running the task synchronously in tests.
I’m using the technique described here to replace Task.Supervisor while running tests.
Then, I define TestTaskSupervisor to be used in tests:
The last thing to do is to set:
config/test.exs file. Now, I’m sure that the task will run synchronously and finish before the test process without causing the