[erlang-questions] Idly wondering why this code in common test suite, init_per_suite/1 works

Ola Andersson A ola.a.andersson@REDACTED
Wed Jul 4 16:39:32 CEST 2018


Hi Bengt,

I have a vague memory of that the process executing the init_per_suite function exits with another reason than normal. Don't know why though. This will terminate the gen_server.
In the other example where the gen_server is started by a spawned process that exits with reason normal, it will be ignored by the gen_server.
BR,
/OLA. 

> -----Original Message-----
> From: erlang-questions-bounces@REDACTED <erlang-questions-
> bounces@REDACTED> On Behalf Of bengt
> Sent: den 3 juli 2018 20:52
> To: Erlang <erlang-questions@REDACTED>
> Subject: [erlang-questions] Idly wondering why this code in common test
> suite, init_per_suite/1 works
> 
> Greetings,
> 
> If somebody happens to know, otherwise ignore. I can read the source myself.
> 
> 1) If I use start_link() from my test suites init_per_suite/1,then the gen_server
> is dead in the test cases.
> 
> 2) This made me either use gen_server:start(), or if the coding guidelines insist
> upon placing the start_link() inside the gen_server callback module, the
> following:
> 
> init_per_suite(Config) ->
> P = erlang:spawn( fun() ->
>         {ok, _} = mm:start_link(),
> 	receive
> 	end_per_suite -> ok
> 	end
> end ),
> [{mm_pid, P} | Config].
> 
> In end_per_suite/1 I then send end_per_suite to Pid and the process exits.
> Probably taking the gen_server with it. Unless it is trapping exists etc… But
> this one is not.
> 
> Today I forgot the receive statement . Like this:
> 
> init_per_suite(Config) ->
> P = erlang:spawn( fun() ->
>         {ok, _} = mm:start_link( ),
> end ),
> [{mm_pid, P}|Config].
> 
> And it still worked. Why?
> Is this not the same thing when the spawned fun returns, as when
> init_per_suite/1 returned without spawning (in 1)?
> 
> 
> Surprised,
> bengt
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions


More information about the erlang-questions mailing list