I write some code to test simple_one_for_one supervisor, but it can not work, the code is:
-module(test_simple_one_for_one).
-behaviour(supervisor).
%% API
-export([start_link/0, start_fun_test/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%--------------------------------------------------------------------
start_link() ->
{ok, Pid} = supervisor:start_link({local, ?SERVER}, ?MODULE, []).
start_fun_test() ->
supervisor:start_child(test_simple_one_for_one, []).
init([]) ->
RestartStrategy = simple_one_for_one,
MaxRestarts = 1000,
MaxSecondsBetweenRestarts = 3600,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
Restart = permanent,
Shutdown = 2000,
Type = worker,
AChild = {fun_test_sup, {fun_test, run, []},
Restart, Shutdown, Type, [fun_test]},
io:format("start supervisor------ ~n"),
{ok, {SupFlags, [AChild]}}.
When I run
test_simple_one_for_one:start_link().
and
test_simple_one_for_one:start_fun_test().
in erl shell, it gives me error:
test_simple_one_for_one:start_fun_test(). ** exception exit: {noproc,{gen_server,call, [test_simple_one_for_one,{start_child,[]},infinity]}} in function gen_server:call/3 (gen_server.erl, line 188)
If it's all the code you've written for the test, beware that when you register a supervisor child you suplly a {M, F, A} tuple which represent the function called when you start a child.
In you're case, I think it cannot simply find the fun_test:run/1 function.