Skip to content

Commit

Permalink
Add block_forever test util
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Nov 26, 2024
1 parent 528af16 commit abc5d29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 0 additions & 1 deletion lib/feeb/db/repo/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ defmodule Feeb.DB.Repo.Manager do

# If `caller_pid` had a normal death, emit a warning so they remember to properly release
# the connection. This is likely an application bug

if reason == :normal do
("Process #{inspect(caller_pid)} died with :normal reason without releasing the " <>
"#{key} connection #{inspect(repo_pid)} -- maybe you forgot to commit/rollback?")
Expand Down
12 changes: 4 additions & 8 deletions test/db/repo/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ defmodule Feeb.DB.Repo.ManagerTest do
spawn_and_wait(fn ->
Manager.fetch_connection(manager, :write, queue_timeout: :infinity)
send(test_pid, :got_connection)

# Keep this process alive until the end of the test
:timer.sleep(999_999)
block_forever()
end)

# Initially, both `spawn_pid_1` and `spawn_pid_2` are in the queue (in that order)
Expand Down Expand Up @@ -249,9 +247,7 @@ defmodule Feeb.DB.Repo.ManagerTest do
request_pid =
spawn_and_wait(fn ->
Manager.fetch_connection(manager, :write)

# Keep this process alive until the test can end its execution
:timer.sleep(999_999)
block_forever()
end)

# The Repo.Manager currently has an active write_1 connection leased to `request_pid`
Expand All @@ -276,14 +272,14 @@ defmodule Feeb.DB.Repo.ManagerTest do
request_pid =
spawn_and_wait(fn ->
Manager.fetch_connection(manager, :write)
:timer.sleep(999_999)
block_forever()
end)

queued_request_pid =
spawn_and_wait(fn ->
# This guy is waiting for `request_pid` to release the connection
Manager.fetch_connection(manager, :write)
:timer.sleep(999_999)
block_forever()
end)

# The write_1 connection is busy (leased to `request_pid`) and there is a non-empty queue
Expand Down
10 changes: 10 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ defmodule Test.Utils do

spawn_pid
end

@doc """
Starts a receive block that will hang indefinitely. This is used for spawned processes that should
remain alive until the end of the test suite to avoid flakes.
"""
def block_forever do
receive do
:will_never_receive -> :ok
end
end
end

0 comments on commit abc5d29

Please sign in to comment.