Skip to content

Commit

Permalink
Add tests to the error handling when using multiple endpoints (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaodhib committed May 20, 2021
1 parent 90d5189 commit 6cc68d8
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions test/login_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,55 @@ defmodule LoginTest do
test "server type 'secondary' against two primary instances", context do
message =
capture_log(fn ->
opts = [
opts = [
endpoints: [{"localhost", 5432}, {"localhost", 5432}],
target_server_type: :secondary
]

assert_start_and_killed(opts ++ context[:options])
end)

assert message =~ "** (Postgrex.Error) failed to establish connection to multiple endpoints:\n\n"
assert message =~ " * localhost:5432: (Postgrex.Error) the server type is not as expected. expected: secondary. actual: primary"
assert message =~
"** (Postgrex.Error) failed to establish connection to multiple endpoints:\n\n"

assert message =~
" * localhost:5432: (Postgrex.Error) the server type is not as expected. expected: secondary. actual: primary\n"
end

test "outputs an error message per attempted endpoint when more than one endpoint is used",
context do
message =
capture_log(fn ->
opts = [
endpoints: [{"doesntexist", 5432}, {"localhost", 5555}],
connect_timeout: 100
]

assert_start_and_killed(opts ++ context[:options])
end)

assert message =~
"** (Postgrex.Error) failed to establish connection to multiple endpoints:\n\n"

assert message =~
" * doesntexist:5432: (DBConnection.ConnectionError) tcp connect (doesntexist:5432): non-existing domain - :nxdomain\n"

assert message =~
" * localhost:5555: (DBConnection.ConnectionError) tcp connect (localhost:5555): connection refused - :econnrefused\n"
end

test "outputs a single error message when only one endpoint is used", context do
message =
capture_log(fn ->
opts = [endpoints: [{"doesntexist", 5432}], connect_timeout: 100]
assert_start_and_killed(opts ++ context[:options])
end)

refute message =~
"** (Postgrex.Error) failed to establish connection to multiple endpoints:\n\n"

assert message =~
"** (DBConnection.ConnectionError) tcp connect (doesntexist:5432): non-existing domain - :nxdomain\n"
end

test "translates provided port number to integer" do
Expand Down Expand Up @@ -251,7 +290,7 @@ defmodule LoginTest do
Process.flag(:trap_exit, true)

case P.start_link(opts) do
{:ok, pid} -> assert_receive {:EXIT, ^pid, :killed}
{:ok, pid} -> assert_receive {:EXIT, ^pid, :killed}, 5_000
{:error, :killed} -> :ok
end
end
Expand Down

0 comments on commit 6cc68d8

Please sign in to comment.