erlang: supervisor one-for-one (+7 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-25 05:09:41 +00:00
parent 8717094e74
commit 47a59343a1
5 changed files with 137 additions and 6 deletions

View File

@@ -768,6 +768,81 @@
(nm (ev "P = gen_server:start_link(stk, ignored), gen_server:call(P, pop)"))
"empty")
;; ── supervisor (one-for-one) ────────────────────────────────────
(do
(er-load-supervisor!)
(erlang-load-module
"-module(echoer).
start() -> spawn(fun () -> echoer:loop() end).
loop() ->
receive
{ping, From} -> From ! pong, echoer:loop();
die -> exit(killed)
end.")
nil)
(er-eval-test "sup starts children"
(do
(erlang-load-module
"-module(sup1). init(_) -> {ok, [{w1, fun () -> echoer:start() end}]}.")
(ev "Sup = supervisor:start_link(sup1, []), receive after 5 -> ok end, length(supervisor:which_children(Sup))"))
1)
(er-eval-test "sup multiple children"
(do
(erlang-load-module
"-module(sup2).
init(_) -> {ok, [
{w1, fun () -> echoer:start() end},
{w2, fun () -> echoer:start() end},
{w3, fun () -> echoer:start() end}
]}.")
(ev "Sup = supervisor:start_link(sup2, []), receive after 5 -> ok end, length(supervisor:which_children(Sup))"))
3)
(er-eval-test "sup child responds"
(do
(erlang-load-module
"-module(sup3). init(_) -> {ok, [{w1, fun () -> echoer:start() end}]}.")
(nm (ev "Sup = supervisor:start_link(sup3, []), receive after 5 -> ok end, [{_, _, P1} | _] = supervisor:which_children(Sup), P1 ! {ping, self()}, receive pong -> ok end")))
"ok")
(er-eval-test "sup restarts on exit"
(do
(erlang-load-module
"-module(sup4). init(_) -> {ok, [{w1, fun () -> echoer:start() end}]}.")
(nm
(ev "Sup = supervisor:start_link(sup4, []), receive after 5 -> ok end, [{_, _, P1} | _] = supervisor:which_children(Sup), P1 ! die, receive after 5 -> ok end, [{_, _, P2} | _] = supervisor:which_children(Sup), P1 =/= P2")))
"true")
(er-eval-test "sup restarted child works"
(do
(erlang-load-module
"-module(sup5). init(_) -> {ok, [{w1, fun () -> echoer:start() end}]}.")
(nm
(ev "Sup = supervisor:start_link(sup5, []), receive after 5 -> ok end, [{_, _, P1} | _] = supervisor:which_children(Sup), P1 ! die, receive after 5 -> ok end, [{_, _, P2} | _] = supervisor:which_children(Sup), P2 ! {ping, self()}, receive pong -> ok end")))
"ok")
(er-eval-test "sup one-for-one isolates failures"
(do
(erlang-load-module
"-module(sup6).
init(_) -> {ok, [
{w1, fun () -> echoer:start() end},
{w2, fun () -> echoer:start() end}
]}.")
(nm
(ev "Sup = supervisor:start_link(sup6, []), receive after 5 -> ok end, [{_, _, P1}, {_, _, P2}] = supervisor:which_children(Sup), P1 ! die, receive after 5 -> ok end, [{_, _, _NewP1}, {_, _, P2Again}] = supervisor:which_children(Sup), P2 =:= P2Again")))
"true")
(er-eval-test "sup stop"
(nm
(do
(erlang-load-module
"-module(sup7). init(_) -> {ok, [{w1, fun () -> echoer:start() end}]}.")
(ev "Sup = supervisor:start_link(sup7, []), receive after 5 -> ok end, supervisor:stop(Sup)")))
"ok")
(define
er-eval-test-summary
(str "eval " er-eval-test-pass "/" er-eval-test-count))