Skip to content

Commit

Permalink
Test database connection restart - 9.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpursley committed Feb 4, 2024
1 parent fcb3734 commit e13a9cb
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions test/walex/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,46 +213,51 @@ defmodule WalEx.DatabaseTest do
postgres_bin_path = pg_bin_path(postgres_path, version)
data_directory = pg_data_directory(version)

pg_stop(postgres_bin_path, data_directory)
Logger.debug("Waiting after pg_stop")
:timer.sleep(2000)
pg_isready?()

pg_start(postgres_bin_path, data_directory)
Logger.debug("Waiting after pg_start")
:timer.sleep(2000)
pg_isready?()

:ok
end

defp pg_stop(postgres_bin_path, data_directory) do
Logger.debug("PostgreSQL stopping.")

case Rambo.run(postgres_bin_path, [
"stop",
"-m",
"immediate",
"-D",
"#{data_directory}"
]) do
case pg_stop(postgres_bin_path, data_directory) do
{:ok,
%Rambo{
status: 0,
out: "waiting for server to shut down.... done\nserver stopped\n",
err: ""
}} ->
Logger.debug("PostgreSQL stopped.")

error ->
unless pg_isready?() do
pg_start(postgres_bin_path, data_directory)
Logger.debug("Waiting after pg_start")
:timer.sleep(4000)
pg_isready?()
end

{:error,
%Rambo{
status: 1,
out: "",
err: error
}} ->
Logger.debug("PostgreSQL not stopped: " <> inspect(error))

_ ->
Logger.debug("PostgreSQL not stopped.")
end

:ok
end

defp pg_stop(postgres_bin_path, data_directory) do
Logger.debug("PostgreSQL stopping.")

Rambo.run(postgres_bin_path, [
"stop",
"-m",
"immediate",
"-D",
"#{data_directory}"
])
end

defp pg_start(postgres_bin_path, data_directory) do
Logger.debug("PostgreSQL starting.")

# No case match here - for some reason starting pg hangs...
# For some reason starting pg hangs so we run in async as not to block...
Task.async(fn ->
Rambo.run(
postgres_bin_path,
Expand All @@ -268,15 +273,15 @@ defmodule WalEx.DatabaseTest do
defp pg_isready? do
case Rambo.run("pg_isready", ["-h", "localhost", "-p", "5432"]) do
{:ok, %Rambo{status: 0, out: "localhost:5432 - accepting connections\n", err: ""}} ->
Logger.debug("PostgreSQL is ready.")
Logger.debug("PostgreSQL is running.")
true

{:error, %Rambo{status: 2, out: "localhost:5432 - no response\n", err: ""}} ->
Logger.debug("PostgreSQL is not ready.")
Logger.debug("PostgreSQL is not running.")
false

error ->
Logger.debug("PostgreSQL is not ready: " <> inspect(error))
Logger.debug("PostgreSQL is not running: " <> inspect(error))
false
end
end
Expand Down

0 comments on commit e13a9cb

Please sign in to comment.