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)
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.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?
Thanks you in advance
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.
I want to test the function output… mabye side effects too. This is for telemetry event handler - I want to know my code has executed.
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
defmodule Hello.TaskWrapper do
def start_link(module, func, args) do
if Mix.env() == :test do
apply(module, func, args)
Task.start_link(module, func, args)
Then in your code, you can use it like
arguments = 
Hello.TaskWrapper.start_link(__MODULE__, :do_work, arguments)