在 elixir 中的每个测试之间设置/拆卸 det 表?

Tom*_*ear 6 elixir dets ex-unit

我们试图在一个完全干净的 dets 环境中运行每个测试,每个测试负责自己的设置。我们遇到了无法在测试之间完全删除目录的问题。

我们如何重置dets?我们是否错误地认为吹走整个目录并重新启动应用程序就足够了?在运行之间删除所有记录是否更好?

认为:

  • async: true 未设置任何测试。
  • dets 是必须的
  • 大多数回答的代码应该(如果可能的话)在 elixir 中,而不是在 erlang 中。

示例代码:

setup do
  #clean up anything that wasn't already clean (failed teardown, etc)
  TestSetup.teardown_dets(:myappatom, "dets")

  on_exit fn ->
    TestSetup.teardown_dets(:myappatom, "dets")
    Application.ensure_all_started(:myappatom)
    Process.sleep(300)
  end
end
Run Code Online (Sandbox Code Playgroud)

和拆卸功能的胆量......

def teardown_dets(application_atom, directory) when is_bitstring(directory) do
  teardown_dets(application_atom, [directory])
end
def teardown_dets(application_atom, directories)
  when is_atom(application_atom) and is_list(directories)
do
  #stop the applications completely
  Application.stop(application_atom)

  #blow away all dets while the application is stopped
  try do
    directories
    |> Enum.map(&File.rm_rf(&1))
  rescue
    error in File.Error -> IO.puts("First file removal failure. Re-attempting.  Initial error information #{error.message}")
  after
      Process.sleep(3000) #wait for whatever it was to get out of the way

      directories
      |> Enum.map(&File.rm_rf!(&1))
  end

end
Run Code Online (Sandbox Code Playgroud)

运行时的示例错误mix test是标准输出显示:

dets: file "dets/event_listener_stream_positions_test" not properly closed, repairing ... 但不是错误,只是常规输出到控制台。

然后是测试中的实际失败: ** (File.Error) could not remove files and directories recursively from "dets": file already exists