Hi!


In our codebase, we use a macro called assert_wait that behaves like assert but 
retries if it fails until some timeout is exceeded. If the timeout is 
reached, it raises in the same way as a failing assert would.


We've been using it for about a year and are quite happy with it. We find 
it very convenient for testing asynchronous behaviors in integration tests. 
For example, when you tell some process to asynchronously do something, you 
can then use assert_wait to check that the process has done it and done it 
correctly. It can also act as a synchronization mechanism as it “pauses 
execution” of the test until the given condition holds.


Example:


test "input is correctly processed by the processing pipeline" do

input = ...

expected_output = ...


{:ok, ref} = PipelineManager.start_pipeline(input)

assert_wait %{status: :finished, output_file: output_file} = 
PipelineManager.get_info(ref)

assert expected_output == File.read!(output_file)

end



Of course, testing based on timeouts is not ideal. If you set the timeout 
too low or if the machine is currently under heavy load, the timeout may be 
exceeded even when everything is going well so far. However, sometimes 
there are no easy alternatives. It’s similar to assert_receive/3 or 
Phoenix.LiveViewTest.assert_redirect/2 that also have a timeout (though 
based on receive and not retrying).


Do you think it would be generally useful to have something like this in 
ExUnit? If so, I can tidy the code a bit and send a PR.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/elixir-lang-core/d0e3cadf-50fb-4c66-a7d8-31feda17b9cfn%40googlegroups.com.

Reply via email to