I am trying to start a few named GenServers and then try to stop them. Before terminating the terminate/2 is called where the state of GenServer gets stored in :ets relation.
describe "sites" do
setup do
# some sites that will be used for naming GenServer
sites = [:s1, :s2, :s3, :s4]
# starting a new GenServer with site names. this will also create an :ets table for storing cache
Sites.new(sites)
IO.inspect(:ets.whereis(:s1_cache), label: :setup)
#Reference<0.3131894567.490864646.163685>
Enum.each(sites, fn site ->
on_exit(fn ->
IO.inspect(:ets.whereis(:s1_cache), label: :on_exit)
# :undefined ?
Sites.stop(site)
end)
end)
{:ok, sites: sites}
end
test "checking state of a site", %{sites: [:s1, _, _, _]} do
IO.inspect(:ets.whereis(:s1_cache), label: :test)
#Reference<0.3131894567.490864646.163685>
assert :s1 = Sites.state(:s1).site_name
end
end
Is there any way that I can access the :ets table when calling on_exit callback ??
How do you start Sites? If you start it linked, then the process might already be stopped at the time on_exit callbacks are executed. The setup code runs in the test process, just like the body of the test. But once the test is done the test process exits. on_exit callbacks run in a separate process.
That only affects async behaviour between different test files. Tests within a single file are always executed sequentially.
This is not about being a supervisor, but about children of supervisors. I doubt your process cannot be used with start_supervised, unless you’ve build your own process loop and such.
This might be a race condition, as after a test is done the next one is started immediatly, as there’s nothing waiting for the external process to first shutdown correctly.