Skip to content

Commit 4ff94a2

Browse files
authored
Merge pull request #1034 from code-corps/1027-fix-issues-with-undead-async-tasks
Fix issue with undead async tasks
2 parents 44503a8 + 2fe5404 commit 4ff94a2

4 files changed

Lines changed: 14 additions & 29 deletions

File tree

test/lib/code_corps/github/user_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule CodeCorps.GitHub.UserTest do
22
@moduledoc false
33

4+
use CodeCorps.BackgroundProcessingCase
45
use CodeCorps.DbAccessCase
56
import CodeCorps.GitHub.TestHelpers
67

test/lib/code_corps_web/controllers/stripe_connect_events_controller_test.exs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule CodeCorpsWeb.StripeConnectEventsControllerTest do
2+
use CodeCorps.BackgroundProcessingCase
23
use CodeCorpsWeb.ConnCase
34

45
alias CodeCorps.StripeEvent
@@ -12,20 +13,6 @@ defmodule CodeCorpsWeb.StripeConnectEventsControllerTest do
1213
{:ok, conn: conn}
1314
end
1415

15-
defp wait_for_supervisor(), do: wait_for_children(:background_processor)
16-
17-
# used to have the test wait for or the children of a supervisor to exit
18-
19-
defp wait_for_children(supervisor_ref) do
20-
supervisor_ref |> Task.Supervisor.children |> Enum.each(&wait_for_child/1)
21-
end
22-
23-
defp wait_for_child(pid) do
24-
# Wait until the pid is dead
25-
ref = Process.monitor(pid)
26-
assert_receive {:DOWN, ^ref, _, _, _}
27-
end
28-
2916
test "responds with 200 when the event will be processed", %{conn: conn} do
3017
event = %{"id" => "evt_123", "livemode" => false}
3118

test/lib/code_corps_web/controllers/stripe_platform_events_controller_test.exs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule CodeCorpsWeb.StripePlatformEventsControllerTest do
2+
use CodeCorps.BackgroundProcessingCase
23
use CodeCorpsWeb.ConnCase
34

45
alias CodeCorps.StripeEvent
@@ -12,21 +13,6 @@ defmodule CodeCorpsWeb.StripePlatformEventsControllerTest do
1213
{:ok, conn: conn}
1314
end
1415

15-
defp wait_for_supervisor(), do: wait_for_children(:background_processor)
16-
17-
# used to have the test wait for or the children of a supervisor to exit
18-
19-
defp wait_for_children(supervisor_ref) do
20-
Task.Supervisor.children(supervisor_ref)
21-
|> Enum.each(&wait_for_child/1)
22-
end
23-
24-
defp wait_for_child(pid) do
25-
# Wait until the pid is dead
26-
ref = Process.monitor(pid)
27-
assert_receive {:DOWN, ^ref, _, _, _}
28-
end
29-
3016
test "responds with 200 when the event will be processed", %{conn: conn} do
3117
event = %{"id" => "evt_123", "livemode" => false, "type" => "any.event"}
3218

test/support/background_processing_case.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ defmodule CodeCorps.BackgroundProcessingCase do
2727
end
2828
end
2929
end
30+
31+
setup do
32+
on_exit fn ->
33+
Task.Supervisor.children(:background_processor)
34+
|> Enum.map(&terminate_child/1)
35+
end
36+
end
37+
38+
defp terminate_child(child) do
39+
Task.Supervisor.terminate_child(:background_processor, child)
40+
end
3041
end

0 commit comments

Comments
 (0)