Skip to content

Commit

Permalink
Add more and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Oct 9, 2023
1 parent dafc5a0 commit 20b07e1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
24 changes: 1 addition & 23 deletions lib/xandra/options_validators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,8 @@ defmodule Xandra.OptionsValidators do
end
end

def validate_node(%Host{address: address, port: port}) when is_tuple(address) do
case :inet.ntoa(address) do
{:error, :einval} ->
{:error,
"expected valid address tuple, got: address: #{inspect(address)} and port: #{inspect(port)}, with error: :einval"}

_valid_address ->
{:ok, {address, port}}
end
end

def validate_node(%Host{address: address, port: port}) when is_list(address) do
case :inet.parse_address(address) do
{:ok, valid_address} ->
{:ok, {valid_address, port}}

error ->
{:error,
"expected valid address char list, got: address: #{inspect(address)} and port: #{inspect(port)}, with error: #{inspect(error)}"}
end
end

def validate_node(other) do
{:error, "expected node to be a string or a {ip, port} tuple, got: #{inspect(other)}"}
{:error, "expected node to be a \"<host>:<port>\" string, got: #{inspect(other)}"}
end

@spec validate_contact_node(term()) :: {:ok, Host.t()} | {:error, String.t()}
Expand Down
20 changes: 20 additions & 0 deletions test/xandra/connection_error_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ defmodule Xandra.ConnectionErrorTest do
~s(action "connect" failed with reason: not connected to any of the nodes)
end

test "with reason :disconnected" do
assert message("connect", :disconnected) ==
~s(action "connect" failed with reason: connection dropped in the middle of a request)
end

test "with reason :not_connected" do
assert message("connect", :not_connected) ==
~s(action "connect" failed with reason: there is currently no connection established with the server)
end

test "with reason {:connection_process_crashed, reason}" do
assert message("connect", {:connection_process_crashed, :banana}) ==
~s(action "connect" failed with reason: connection process crashed before sending a response with reason: :banana)
end

test "with POSIX reason from :inet" do
assert message("connect", :enomem) ==
~s(action "connect" failed with reason: not enough memory)
Expand All @@ -35,6 +50,11 @@ defmodule Xandra.ConnectionErrorTest do
~s(action "connect" failed with reason: connection refused)
end

test "with reason :timeout" do
assert message("connect", :timeout) ==
~s(action "connect" failed with reason: request timeout)
end

test "with any other reason" do
message = message("connect", :banana)

Expand Down
40 changes: 40 additions & 0 deletions test/xandra/transport_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Xandra.TransportTest do
use ExUnit.Case, async: true

alias Xandra.Transport

require Xandra.Transport

describe "close/1" do
test "returns an updated transport" do
assert {:ok, listen_socket} = :gen_tcp.listen(0, [])
assert {:ok, port} = :inet.port(listen_socket)

transport = %Transport{module: :gen_tcp, options: []}
assert {:ok, transport} = Transport.connect(transport, ~c"localhost", port, 5000)
assert transport.socket != nil

assert %Transport{} = transport = Transport.close(transport)
assert transport.socket == nil
end
end

describe "is_* macros" do
test "returns an updated transport" do
assert {:ok, listen_socket} = :gen_tcp.listen(0, [])
assert {:ok, port} = :inet.port(listen_socket)

transport = %Transport{module: :gen_tcp, options: []}
assert {:ok, transport} = Transport.connect(transport, ~c"localhost", port, 5000)

assert Transport.is_data_message(transport, {:tcp, transport.socket, "data"}) == true
assert Transport.is_data_message(transport, {:tcp, nil, "data"}) == false

assert Transport.is_closed_message(transport, {:tcp_closed, transport.socket}) == true
assert Transport.is_closed_message(transport, {:tcp_closed, nil}) == false

assert Transport.is_error_message(transport, {:tcp_error, transport.socket, :econnrefused}) ==
true
end
end
end
21 changes: 21 additions & 0 deletions test/xandra_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ defmodule XandraTest do
Xandra.execute(conn, "USE some_keyspace")
end

test "supports the :name option as an atom", %{start_options: start_options} do
assert {:ok, conn} = start_supervised({Xandra, [name: :my_test_conn] ++ start_options})
assert Process.whereis(:my_test_conn) == conn
end

test "supports the :name option as {:global, name}", %{start_options: start_options} do
name = {:global, :my_global_test_conn}
assert {:ok, conn} = start_supervised({Xandra, [name: name] ++ start_options})
assert GenServer.whereis(name) == conn
end

test "supports the :name option as {:via, mod, term}",
%{start_options: start_options, test: test_name} do
registry_name = :"Registry_#{test_name}"
start_supervised!({Registry, keys: :unique, name: registry_name})

name = {:via, Registry, {registry_name, :my_via_test_conn}}
assert {:ok, conn} = start_supervised({Xandra, [name: name] ++ start_options})
assert GenServer.whereis(name) == conn
end

test "supports the :keyspace option", %{keyspace: keyspace, start_options: start_options} do
assert {:ok, conn} = start_supervised({Xandra, [keyspace: keyspace] ++ start_options})

Expand Down

0 comments on commit 20b07e1

Please sign in to comment.