Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error when "query" is of shape %{opts: opts}. Should fix #250. #255

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mariaex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,8 @@ defmodule Mariaex.Protocol do
{:error, error, clean_state(s, nil)}
nil ->
{:error, error, clean_state(s, nil)}
%{opts: _opts} ->
{:error, error, clean_state(s, nil)}
end
end
end
46 changes: 46 additions & 0 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule ConnectionTest do
use ExUnit.Case
import Mariaex.TestHelper

@opts [
database: "mariaex_test",
username: "mariaex_user",
password: "mariaex_pass",
cache_size: 2,
max_restarts: 0
]

setup context do
connection_opts = context[:connection_opts] || []
{:ok, pid} = Mariaex.Connection.start_link(connection_opts ++ @opts)

# remove all modes for this session to have the same behaviour on different versions of mysql/mariadb
{:ok, _} = Mariaex.Connection.query(pid, "SET SESSION sql_mode = \"\";")

{:ok, _} = Mariaex.Connection.query(pid, "set GLOBAL max_connections = 1;")

{:ok, [pid: pid]}
end

test "query fails with connection not available", %{pid: orig_pid} do
# my version of mariadb only allows setting max_connections to 10, so we have to start up a few connections to saturate this.
assert Enum.find(1..15, fn _x ->
{:ok, pid} = Mariaex.Connection.start_link(@opts)
context = [pid: pid]

case query("SELECT 1", []) do
[[1]] ->
false

%DBConnection.ConnectionError{
message:
"connection not available and request was dropped from queue after " <> rest
} ->
true
end
end)

# cleanup
{:ok, _} = Mariaex.Connection.query(orig_pid, "set GLOBAL max_connections = 500;")
end
end