Stian Håklev
Stian Håklev

Reputation: 1290

Time-based testing in Elixir

Let's say I have an Elixir function that should do something once every 30 minutes... Or not more often than once every 30 seconds, no matter how often it is called. Is there any good way of testing this, without having the test case take hours?

Upvotes: 3

Views: 568

Answers (1)

Gazler
Gazler

Reputation: 84140

It is difficult to give an answer without having a specific use case, however one fairly simple option is to have the timeout be configurable (perhaps as a function argument) and test that.

e.g.

defmodule MyTest do
  use Exunit.Case

  test "message sent every 100 milliseconds" do
    pid = self
    MyModule.report_count_every(100, pid)
    assert_receive({:ok, 1}, 500)
    assert_receive({:ok, 2}, 500)
  end
end

This assumes that MyModule is a GenServer that maintains a counter which it broadcasts based on the first argument passed to report_count_every/2

Upvotes: 5

Related Questions