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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement