Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/preview/tmp_dir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
55 changes: 32 additions & 23 deletions test/preview/tmp_dir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -49,26 +51,33 @@ 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)
refute File.exists?(dir)
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")
Expand Down
Loading