Named gen_server process created in init_per_suite is not present in tests

173 views Asked by At

I'm learning erlang and I have created a cache_server module implementing gen_server behaviour.

This module is responsible for creating ets tables and has an api to insert, lookup, etc.

I wanted to make test suite for the module and to run test cases for insertion and lookup in one group of tests as a sequence because first function populates the table and other searches for inserted keys.

I tried to call cache_server:start_link([]) in init_per_suite hook function of the suite but in test cases I don't see my cache_server process when I call registered() function.

And I get

{noproc,{gen_server,call,[cache_server,{lookup,numbers,1}]}}

error.

I also tried to move call cache_server:start_link() from the init_per_suite to the first test case but in the subsequent test cases the process gets unavailable.

When I test my code by hands using rebar3 shell, everything works as expected.

Is it possible to share a named gen_server process between test cases in common test test suite?

1

There are 1 answers

3
Venkatakumar Srinivasan On

Try calling cache_server:start_link() in init_per_testcase. As this function is executed before each test case, you would also need to stop the cache_server process in end_per_testcase.

Another option is to group all cache_server related test cases in one group and call cache_server:start_link() in init_per_group and stop the process in end_per_group