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
36 changes: 30 additions & 6 deletions lib/diff/tmp_dir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,57 @@ defmodule Diff.TmpDir do
dir
end

def await_cleanup(pid) do
GenServer.call(__MODULE__, {:await_cleanup, pid}, 5000)
end

defp track(path) do
pid = self()
:ets.insert(@table, {pid, path})
GenServer.cast(__MODULE__, {:monitor, pid})
GenServer.call(__MODULE__, {:monitor, pid})
end

@impl true
def init(_opts) do
Process.flag(:trap_exit, true)
:ets.new(@table, [:named_table, :duplicate_bag, :public])
{:ok, %{monitors: MapSet.new()}}
{:ok, %{monitors: MapSet.new(), waiters: %{}}}
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

@impl true
def handle_call({:await_cleanup, pid}, from, state) do
if pid in state.monitors do
waiters = Map.update(state.waiters, pid, [from], &[from | &1])
{:noreply, %{state | waiters: waiters}}
else
{:reply, :ok, state}
end
end

@impl true
def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do
cleanup_pid(pid)
{:noreply, %{state | monitors: MapSet.delete(state.monitors, pid)}}

for from <- Map.get(state.waiters, pid, []) do
GenServer.reply(from, :ok)
end

{:noreply,
%{
state
| monitors: MapSet.delete(state.monitors, pid),
waiters: Map.delete(state.waiters, pid)
}}
end

@impl true
Expand Down
15 changes: 3 additions & 12 deletions test/diff/tmp_dir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ defmodule Diff.TmpDirTest do
send(test_pid, {:paths, file, dir})
end)

ref = Process.monitor(task_pid)
assert_receive {:paths, file, dir}
wait_for_cleanup(task_pid, ref)
Diff.TmpDir.await_cleanup(task_pid)

refute File.exists?(file)
refute File.exists?(dir)
Expand All @@ -42,9 +41,8 @@ defmodule Diff.TmpDirTest do
raise "crash"
end)

ref = Process.monitor(task_pid)
assert_receive {:paths, file, dir}
wait_for_cleanup(task_pid, ref)
Diff.TmpDir.await_cleanup(task_pid)

refute File.exists?(file)
refute File.exists?(dir)
Expand All @@ -65,9 +63,8 @@ defmodule Diff.TmpDirTest do
send(test_pid, {:paths, paths})
end)

ref = Process.monitor(task_pid)
assert_receive {:paths, paths}
wait_for_cleanup(task_pid, ref)
Diff.TmpDir.await_cleanup(task_pid)

for {file, dir} <- paths do
refute File.exists?(file)
Expand All @@ -82,10 +79,4 @@ defmodule Diff.TmpDirTest do
assert File.exists?(file)
assert File.dir?(dir)
end

defp wait_for_cleanup(task_pid, ref) do
assert_receive {:DOWN, ^ref, :process, ^task_pid, _}, 5000
# Sync with the GenServer to ensure the :DOWN cleanup has been processed
:sys.get_state(Diff.TmpDir)
end
end
6 changes: 6 additions & 0 deletions test/diff_web/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ defmodule DiffWeb.IntegrationTest do
{:error, :not_found}
end)

Diff.HexMock
|> stub(:diff, fn "phoenix", "1.4.5", "1.4.9" -> :error end)

{:ok, _view, html} = live(conn, "/diff/phoenix/1.4.5..")

# Should show generating state since we're resolving to latest version
Expand All @@ -46,6 +49,9 @@ defmodule DiffWeb.IntegrationTest do
{:error, :not_found}
end)

Diff.HexMock
|> stub(:diff, fn "nonexistent", "1.0.0", "2.0.0" -> :error end)

{:ok, _view, html} = live(conn, "/diff/nonexistent/1.0.0..2.0.0")

assert html =~ "Generating diffs"
Expand Down
3 changes: 3 additions & 0 deletions test/diff_web/live/diff_live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ defmodule DiffWeb.DiffLiveViewTest do
{:error, :not_found}
end)

Diff.HexMock
|> stub(:diff, fn "phoenix", "1.4.5", "1.4.9" -> :error end)

{:ok, _view, html} = live(conn, "/diff/phoenix/1.4.5..1.4.9")

# Should show generating state when metadata is not found
Expand Down
Loading