diff --git a/lib/preview/tmp_dir.ex b/lib/preview/tmp_dir.ex index 6c049e2..9842fac 100644 --- a/lib/preview/tmp_dir.ex +++ b/lib/preview/tmp_dir.ex @@ -35,7 +35,7 @@ defmodule Preview.TmpDir do defp track(path) do pid = self() :ets.insert(@table, {pid, path}) - GenServer.cast(__MODULE__, {:monitor, pid}) + GenServer.call(__MODULE__, {:monitor, pid}) end @impl true @@ -46,12 +46,12 @@ defmodule Preview.TmpDir do end @impl true - def handle_cast({:monitor, pid}, state) do + def handle_call({:monitor, pid}, _from, state) do if pid in state.monitors do - {:noreply, state} + {:reply, :ok, state} else Process.monitor(pid) - {:noreply, %{state | monitors: MapSet.put(state.monitors, pid)}} + {:reply, :ok, %{state | monitors: MapSet.put(state.monitors, pid)}} end end diff --git a/test/preview/tmp_dir_test.exs b/test/preview/tmp_dir_test.exs index aa18bdd..8ae9de4 100644 --- a/test/preview/tmp_dir_test.exs +++ b/test/preview/tmp_dir_test.exs @@ -15,14 +15,15 @@ defmodule Preview.TmpDirTest do test "cleanup on normal process exit" do test_pid = self() - Task.start(fn -> - file = Preview.TmpDir.tmp_file("test") - dir = Preview.TmpDir.tmp_dir("test") - send(test_pid, {:paths, file, dir}) - end) + {:ok, pid} = + Task.start(fn -> + file = Preview.TmpDir.tmp_file("test") + dir = Preview.TmpDir.tmp_dir("test") + send(test_pid, {:paths, file, dir}) + end) assert_receive {:paths, file, dir} - Process.sleep(100) + await_cleanup(pid) refute File.exists?(file) refute File.exists?(dir) @@ -32,15 +33,16 @@ defmodule Preview.TmpDirTest do test "cleanup on process crash" do test_pid = self() - Task.start(fn -> - file = Preview.TmpDir.tmp_file("test") - dir = Preview.TmpDir.tmp_dir("test") - send(test_pid, {:paths, file, dir}) - raise "crash" - end) + {:ok, pid} = + Task.start(fn -> + file = Preview.TmpDir.tmp_file("test") + dir = Preview.TmpDir.tmp_dir("test") + send(test_pid, {:paths, file, dir}) + raise "crash" + end) assert_receive {:paths, file, dir} - Process.sleep(100) + await_cleanup(pid) refute File.exists?(file) refute File.exists?(dir) @@ -49,19 +51,20 @@ defmodule Preview.TmpDirTest do test "multiple paths for one process" do test_pid = self() - Task.start(fn -> - paths = - for i <- 1..5 do - file = Preview.TmpDir.tmp_file("test-#{i}") - dir = Preview.TmpDir.tmp_dir("test-#{i}") - {file, dir} - end + {:ok, pid} = + Task.start(fn -> + paths = + for i <- 1..5 do + file = Preview.TmpDir.tmp_file("test-#{i}") + dir = Preview.TmpDir.tmp_dir("test-#{i}") + {file, dir} + end - send(test_pid, {:paths, paths}) - end) + send(test_pid, {:paths, paths}) + end) assert_receive {:paths, paths} - Process.sleep(100) + await_cleanup(pid) for {file, dir} <- paths do refute File.exists?(file) @@ -69,6 +72,12 @@ defmodule Preview.TmpDirTest do end end + defp await_cleanup(pid) do + ref = Process.monitor(pid) + assert_receive {:DOWN, ^ref, _, _, _}, 5000 + :sys.get_state(Preview.TmpDir) + end + test "paths persist while process is alive" do file = Preview.TmpDir.tmp_file("test") dir = Preview.TmpDir.tmp_dir("test")