Thanks for the proposal. The difference between assert_receive to assert_wait, if my understanding is correct, is that assert_receive is based on messages, while assert_wait is a busy loop (i.e. you keep retrying after some time until it succeeds). And the reason we haven't added something similar to Elixir is because you should prefer message driven loops as much as possible, and we should strongly nudge people towards solutions like assert_receive.
*José Valimhttps://dashbit.co/ <https://dashbit.co/>* On Fri, Mar 28, 2025 at 1:10 PM David Pavlík <d.pa...@gmail.com> wrote: > 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 > <https://groups.google.com/d/msgid/elixir-lang-core/d0e3cadf-50fb-4c66-a7d8-31feda17b9cfn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAGnRm4%2BbHTPdcxJ9t-jzcGmyWw%2BL3bGHOhZxrkDNwdHqwRDJ0g%40mail.gmail.com.