vrod

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

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

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)

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement