7

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

我们怎样才能重置dets?我们是否错误地认为清除整个目录并重新启动应用程序就足够了?在运行之间删除所有记录会更好吗?

认为:

  • async: true没有为任何测试设置。
  • dets是必须的
  • 大多数已回答的代码应该(如果可能的话)在长生不老药中,而不是在 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

以及拆卸功能的胆量......

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

运行时的示例错误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

4

0 回答 0