diff --git a/test/e2e/albedo_stream_pressure_probe.erl b/test/e2e/albedo_stream_pressure_probe.erl index 7670c3dc6..04a98f8df 100644 --- a/test/e2e/albedo_stream_pressure_probe.erl +++ b/test/e2e/albedo_stream_pressure_probe.erl @@ -228,39 +228,49 @@ maintenance_json(Id) -> try await_run_slot(Module, erlang:monotonic_time(millisecond) + 15000), erlang:suspend_process(Owner), - Worker = receive + First = receive {trace, Pid, call, {Module, run, [_]}} -> Pid after 3000 -> error(maintenance_not_started) end, - Monitor = monitor(process, Worker), Deadline = erlang:monotonic_time(millisecond) + 2000, - Workers = maintenance_calls(Module, Deadline, [Worker]), - exit(Owner, kill), - Outcome = receive - {'DOWN', Monitor, process, Worker, normal} -> <<"completed">>; - {'DOWN', Monitor, process, Worker, Reason} -> error({sweep_failed, Reason}) - after 7000 -> error(maintenance_still_blocked) + {Workers, Admitted} = maintenance_calls(Module, Deadline, [First], 1), + %% The worker still running is the one blocked on the held owner. + Blocked = case [W || W <- Workers, is_process_alive(W)] of + [] -> error(no_worker_blocked_on_the_owner); + Live -> [{monitor(process, W), W} || W <- Live] end, - json:encode(#{admitted => length(lists:usort(Workers)), outcome => Outcome}) + exit(Owner, kill), + lists:foreach(fun({Monitor, Worker}) -> + receive + {'DOWN', Monitor, process, Worker, normal} -> ok; + {'DOWN', Monitor, process, Worker, Reason} -> error({sweep_failed, Reason}) + after 7000 -> error(maintenance_still_blocked) + end + end, Blocked), + json:encode(#{admitted => Admitted, outcome => <<"completed">>}) after erlang:trace(all, false, [call]), erlang:trace_pattern({Module, run, 1}, false, [local]), try erlang:resume_process(Owner) catch _:_ -> ok end end. -maintenance_calls(Module, Deadline, Workers) -> +%% Admitted is the most workers inside run/1 at once. A worker whose call +%% reached the owner just before the suspend finishes, and the next sweep +%% starts another one: two workers in the window, but one at a time. +maintenance_calls(Module, Deadline, Workers, Admitted) -> Remaining = max(0, Deadline - erlang:monotonic_time(millisecond)), receive {trace, Pid, call, {Module, run, [_]}} -> - maintenance_calls(Module, Deadline, [Pid | Workers]) - after Remaining -> Workers + Running = 1 + length([W || W <- Workers, is_process_alive(W)]), + maintenance_calls(Module, Deadline, [Pid | Workers], max(Admitted, Running)) + after Remaining -> {Workers, Admitted} end. %% No process is inside maintenance:run/1 when the probe suspends the owner. %% Calls traced while waiting belong to workers that finished before the %% suspend and are drained uncounted: the admission window starts at the -%% suspend. A call that slips past the last drain comes from a worker at run -%% entry by construction, so it still meets the held owner. +%% suspend. A call that slips past the last drain may still reach the owner +%% before the suspend and finish; maintenance_calls/4 counts overlap, not calls. await_run_slot(Module, Deadline) -> drain_run_calls(Module), case run_slot_busy(Module) of