2

Common test init_per_group/2 terminates gen_server when it is started with gen_server:start_link. But it is ok to start server with gen_server:start.

gen_server can be started with any of these methods (start and start_link) in init_per_suite/1 and init_per_testcase/2.

Why it is not possible to start gen_server in init_per_group/2 with gen_server:start_link?

Galina
  • 75
  • 1
  • 4
  • What's the error code when gen_server:start_link failed? – Dean Song Apr 18 '16 at 09:14
  • 1
    There is no error. My server traps exit flag. So in the end of `init_per_group/2` I get in `gen_server:terminate/2` this reason: `{#Ref<0.0.1.845>,1475, [{server,<0.226.0>}, {watchdog,<0.225.0>}, {tc_logfile, ... [config data]` I use erlang 18.1 with common test v1.12 – Galina Apr 18 '16 at 09:58

1 Answers1

2

This happens because init_per_group is run in a separate process, just like each test case, and that process exits with an exit reason that communicates information about success/failure of group initialisation. From test_server:run_test_case_eval:

exit({Ref,Time,Value,Loc,Opts}).

Since the gen_server is linked to the process that runs init_per_group, and since the exit reason is not normal and the gen_server is not trapping exits, the gen_server process exits with that same exit reason.

On the other hand, init_per_testcase is run in the same process as the test case itself, so this problem does not appear.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • Thank you. Now I get it. But there still something strange: when gen_server does trap exits it won't receive {'EXIT', ...} message and it will terminate anyway. – Galina Apr 19 '16 at 17:53