【发布时间】:2019-10-09 07:09:33
【问题描述】:
我正在学习用 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)
如何防止这种情况发生?超时是个好方法吗?
【问题讨论】:
-
您最好的选择可能是在终止和断言之间为您的测试添加一个短暂的睡眠。
-
你为什么要测试 OTP?你不相信 40yo 久经考验的解决方案吗?您应该测试 your 代码,而不是 OTP 代码。后者有效,认为这是理所当然的。
ExUnit没有提供一种方便的方法来准确地测试它,因为没有人应该测试 OTP 内部。 -
@AlekseiMatiushkin 我只是想研究 OTP 使用测试的工作方式,例如 TDD
标签: erlang elixir erlang-otp gen-server ex-unit