Idly wondering why this code in common test suite, init_per_suite/1 works

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Idly wondering why this code in common test suite, init_per_suite/1 works

bengt e
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Idly wondering why this code in common test suite, init_per_suite/1 works

Ola Andersson A (AL/EAB)
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: [hidden email] <erlang-questions-
> [hidden email]> On Behalf Of bengt
> Sent: den 3 juli 2018 20:52
> To: Erlang <[hidden email]>
> 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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions