我正在学习在 Elixir 中进行测试,并且出现了这个问题:
当我运行以下测试时,有时它通过,有时没有,我认为这是主管没有时间重新启动 GenServer 的事实:
test "Supervisor will revive the dead gensever" do
{:ok, pid} = KV.Supervisor.start_link([])
KV.RegistryClient.create KV.RegistryClient, "wallet"
[h | _] = Supervisor.which_children(pid)
{KV.RegistryClient, pid_reg, _, _} = h
send(pid_reg, :insta_kill)
assert %{active: 1} = Supervisor.count_children(pid)
end
发生时,这是错误:
1) test Supervisor will revive the dead gensever (KV.RegistryTest)
test/kv/registry_test.exs:35
match (=) failed
code: assert %{active: 1} = Supervisor.count_children(pid)
right: %{active: 0, specs: 1, supervisors: 0, workers: 1}
stacktrace:
test/kv/registry_test.exs:41: (test)
我该如何防止这种情况发生?超时是一个好方法吗?