vrod
Receive from Task.start_link?
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?
Thanks you in advance
First Post!
ityonemo
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.
Last Post!
h9e
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
Then in your code, you can use it like
arguments = []
Hello.TaskWrapper.start_link(__MODULE__, :do_work, arguments)
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










