Skip to content

Instantly share code, notes, and snippets.

@w0rd-driven
Forked from thmsmlr/wait_for.ex
Created December 26, 2024 21:22
Show Gist options
  • Save w0rd-driven/f479cad853b3647fd157a7fc14e0544f to your computer and use it in GitHub Desktop.
Save w0rd-driven/f479cad853b3647fd157a7fc14e0544f to your computer and use it in GitHub Desktop.
ExUnit helper function to wait up to a timeout to determine whether a set of assertions completes. Perfect for testing async code
@default_timeout 100
@check_interval 10
# Test Helpers
defp wait_for(fun, timeout \\ @default_timeout) do
start_time = System.monotonic_time(:millisecond)
ref = make_ref()
try do
do_wait_for(fun, start_time, timeout, ref, nil)
catch
{:wait_for_timeout, ^ref, last_error} ->
message = """
Assertion did not succeed within #{timeout}ms.
Last failure:
#{Exception.format(:error, last_error, [])}
"""
flunk(message)
end
end
defp do_wait_for(fun, start_time, timeout, ref, _last_error) do
try do
fun.()
:ok
rescue
error in [ExUnit.AssertionError] ->
current_time = System.monotonic_time(:millisecond)
if current_time - start_time < timeout do
Process.sleep(@check_interval)
do_wait_for(fun, start_time, timeout, ref, error)
else
throw({:wait_for_timeout, ref, error})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment