I have something that takes a long time, so I used Task.start_link/1 in production so the user will not wait for this to complete (it’s ok if it takes a long time)
Task.start_link(fn -> do_work() end)
However, do_work is a private function and I want to test that the output is correct. Is there a way I can read the output of this? I cannot use Task.async and Task.await in production because I cannot block execution.
I think the easy solution is make do_work public, but I want to know more about processes. Is it possible to read return value?
Task.start* is for things where you don’t care about the return.
Are you testing the function return, or function side effects?
Also note that start_link will cause your processes to be linked, so be sure you’re ok with the user process dying resulting in the task being cancelled, potentially in a strange place. Otherwise, use Task.Supervisor.
You can create a simple wrapper module that depends on your mix environment to invoke the function (by call Kernel.apply/3 in test env), or to call Task.start_link (in prod env). Such that when running unit test, you can assert the result of do_work().
For example:
defmodule Hello.TaskWrapper do
def start_link(module, func, args) do
if Mix.env() == :test do
apply(module, func, args)
else
Task.start_link(module, func, args)
end
end
end