test: wait on observable state instead of blind sleeps

Replace the fixed Process.sleep waits in the import, members-PDF and
field-visibility tests with event-based / bounded-poll waits on the
observable condition, removing a known flakiness vector.
This commit is contained in:
Moritz 2026-06-16 17:51:43 +02:00
parent ccd1f81e3e
commit 655fd80524
3 changed files with 55 additions and 36 deletions

View file

@ -274,17 +274,38 @@ defmodule Mv.Membership.MembersPDFTest do
assert {:ok, _pdf_binary} = result
# Wait a bit for cleanup (async cleanup might take a moment)
Process.sleep(100)
# Count temp directories after
after_count =
count_export_dirs = fn ->
temp_base
|> File.ls!()
|> Enum.count(fn name -> String.starts_with?(name, "mv_pdf_export_") end)
end
# Poll the observable cleanup condition (temp-dir count returns to the baseline)
# with a bounded deadline instead of a fixed sleep, so the test waits no longer
# than the cleanup actually needs and still fails if cleanup never runs.
after_count = poll_until_cleaned(count_export_dirs, before_count, 100)
# Should have same or fewer temp dirs (cleanup should have run)
assert after_count <= before_count + 1
end
end
# Bounded poll: returns the export-dir count once it drops back to the baseline
# (cleanup done), or the last observed count when the attempt budget is exhausted
# (so the caller's assertion reports the real state on a genuine cleanup stall).
defp poll_until_cleaned(count_fun, baseline, attempts_left) do
current = count_fun.()
cond do
current <= baseline ->
current
attempts_left <= 0 ->
current
true ->
Process.sleep(10)
poll_until_cleaned(count_fun, baseline, attempts_left - 1)
end
end
end