kanishka

kanishka

"eventually" check operator exist for unit tests?

I think scala testing libraries has an operator for tests that checks some condition eventuallt evaluates to true (with an implicit timeout). Is there an equivalent function in a common elixir testing utility library?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think one reason this is less common in Elixir is that a lot of things you’d wait for are better accomplished by waiting to receive a message that either is the check or indicates that you can now check instead of polling. In other words if you perform a background task instead of polling to see if the task is complete, monitoring the background process and waiting for an exit message is probably better.

ChrisYammine

ChrisYammine

I’ve used ExUnit.Assertions — ExUnit v1.12.3 (assert_receive/3) for times where I’ve needed to do this

LostKobrakai

LostKobrakai

There’s nothing in core ExUnit. There are few third party libraries or you can do:

# try 10 times every 100ms
assert Stream.interval(100) 
       |> Stream.take(10)
       |> Enum.any?(fn _ -> …test end)

Last Post!

m1dnight

m1dnight

I know this post is old, but it came up when I looked for this exact problem, so putting my answer anyway.

I want to test a similar thing, and here is what I eventually came up with.

  1. You extend the api of your log writer process so that any process can subscribe to an event in case a log has been written. That way your test can do an assert_receive on a log being written.
  2. You can use something similar to assert_eventually to try an assertion a few times. This seems to be the easiest approach, but it might also point to a design issue in your code.

In my particular case, I had a work queue that divided tasks via work-stealing-like system. A queue keeps a list of tasks, and workers can request work from the queue. When work is available its sent to the workers. The API looks as follows:

  @doc """
  A process signals the queue that it is ready to ingest a message.
  """
  @spec request_work :: :ok
  def request_work do
    GenServer.cast(__MODULE__, {:request_work, self()})
  end

  @doc """
  Insert a work item in the queue.
  """
  @spec insert_work(message()) :: :ok
  def insert_work(message) do
    GenServer.call(__MODULE__, {:insert_work, message})
  end

I extended the API of my WorkQueue to allow a process to subscribe to events. When a work item is sent, a notification is sent, as well as when a worker reports it needs work.

  @doc """
  Send a message to the process when a worker asks for work, or when a task is
  sent to a worker.
  """
  @spec notify(pid()) :: :ok
  def notify(pid) do
    GenServer.call(__MODULE__, {:notify, pid})
  end

When the application starts, the WorkQueue has n workers waiting for work.
So, executing two work items should notify the test of two things:

  • work_requested when the worker finishes the first task, and is ready for the next one.
  • work_added when the second work item is put in the queue

My test looks as follows.

# let the queue tell us when a job is done, or when a worker asks for work
IngestQueue.notify(self())

Handler.handle_message([device.device_id, "data"], @telegram) |> tap(&IO.inspect(&1, label: ""))

# before the test all workers are waiting for work, so the first event is work being sent 
# when the worker is done, they will ask for new work 
# when the second task is done, that worker will ask for work again
assert_receive :sent_work
assert_receive :request_work

# I am now certain the queue executed at least two tasks.

Obviously this could be done a bit better, but with minimal effort I can now make assertions about the state of an asynchronous system.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement