Skip to content

Commit

Permalink
Do not rely on all DOWN messages being delivered simultaniously
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 20, 2024
1 parent 4280765 commit 82eb220
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions test/plug/upload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ defmodule Plug.UploadTest do

receive do
{:DOWN, ^ref, :process, ^pid, :normal} ->
{:ok, _} = Plug.Upload.random_file("sample")
refute File.exists?(path)
wait_until(fn -> not File.exists?(path) end)
end
end

defp wait_until(fun) do
if fun.() do
:ok
else
Process.sleep(50)
wait_until(fun)
end
end

Expand Down Expand Up @@ -82,31 +90,17 @@ defmodule Plug.UploadTest do

receive do
{:DOWN, ^ref, :process, ^pid, :normal} ->
{:ok, _} = Plug.Upload.random_file("sample")

wait_until(fn -> not File.exists?(path3) end)
assert File.exists?(path1)
assert File.exists?(path2)
refute File.exists?(path3)
end

send(other_pid, :exit)

receive do
{:DOWN, ^other_ref, :process, ^other_pid, :normal} ->
# force sync by creating file in unknown process
parent = self()

spawn(fn ->
{:ok, _} = Plug.Upload.random_file("sample")
send(parent, :continue)
end)

receive do
:continue -> :ok
end

refute File.exists?(path1)
refute File.exists?(path2)
wait_until(fn -> not File.exists?(path1) end)
wait_until(fn -> not File.exists?(path2) end)
end
end

Expand Down Expand Up @@ -158,20 +152,8 @@ defmodule Plug.UploadTest do

receive do
{:DOWN, ^other_ref, :process, ^other_pid, :normal} ->
# force sync by creating file in unknown process
parent = self()

spawn(fn ->
{:ok, _} = Plug.Upload.random_file("sample")
send(parent, :continue)
end)

receive do
:continue -> :ok
end

refute File.exists?(path)
refute File.exists?(path1)
wait_until(fn -> not File.exists?(path) end)
wait_until(fn -> not File.exists?(path1) end)
end
end
end

0 comments on commit 82eb220

Please sign in to comment.