Skip to content

Commit

Permalink
More and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Oct 13, 2023
1 parent 9d69468 commit dd537cf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ services:

toxiproxy:
image: ghcr.io/shopify/toxiproxy
command: -config=/config/toxiproxy.json -host 0.0.0.0
volumes:
- ./test/toxiproxy_config.json:/config/toxiproxy.json
ports:
- "8474:8474"
- "19052:19052"
- "19052:9042"
- "19152:9043"
1 change: 1 addition & 0 deletions lib/xandra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ defmodule Xandra do
basis through the `:consistency` option in `execute/4`.
"""
],
debug: [type: :any],
encryption: [
type: :boolean,
default: false,
Expand Down
14 changes: 8 additions & 6 deletions lib/xandra/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ defmodule Xandra.Connection do
end
end)
end

{:error, error} ->
{:error, ConnectionError.new("check out connection", error)}
end
end

Expand Down Expand Up @@ -195,8 +198,8 @@ defmodule Xandra.Connection do
{fun.(), telemetry_meta}
end)

{:error, %ConnectionError{} = error} ->
{:error, error}
{:error, error} ->
{:error, ConnectionError.new("check out connection", error)}
end
end

Expand Down Expand Up @@ -237,7 +240,7 @@ defmodule Xandra.Connection do
frame = %Frame{frame | atom_keys?: atom_keys?}
{:ok, frame}

{^req_alias, {:error, %ConnectionError{} = error}} ->
{^req_alias, {:error, error}} ->
{:error, error}

{:DOWN, ^req_alias, _, _, reason} ->
Expand Down Expand Up @@ -338,7 +341,7 @@ defmodule Xandra.Connection do

data =
Enum.reduce(data.in_flight_requests, data, fn {stream_id, req_alias}, data_acc ->
send_reply(req_alias, {:error, ConnectionError.new("request", :disconnected)})
send_reply(req_alias, {:error, :disconnected})
update_in(data_acc.free_stream_ids, &MapSet.put(&1, stream_id))
end)

Expand Down Expand Up @@ -492,8 +495,7 @@ defmodule Xandra.Connection do
end

def disconnected({:call, from}, {:checkout_state_for_next_request, _req_alias}, _data) do
reply = {:error, ConnectionError.new("request", :not_connected)}
{:keep_state_and_data, {:reply, from, reply}}
{:keep_state_and_data, {:reply, from, {:error, :not_connected}}}
end

def disconnected({:call, from}, :get_transport, %__MODULE__{}) do
Expand Down
8 changes: 0 additions & 8 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ if System.get_env("XANDRA_DEBUG") do
Xandra.Telemetry.attach_debug_handler()
end

ToxiproxyEx.populate!([
%{
name: "xandra_test_cassandra",
listen: "0.0.0.0:19052",
upstream: "cassandra:9042"
}
])

Logger.configure(level: String.to_existing_atom(System.get_env("LOG_LEVEL", "info")))

excluded =
Expand Down
14 changes: 14 additions & 0 deletions test/toxiproxy_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "xandra_test_cassandra",
"listen": "0.0.0.0:9042",
"upstream": "cassandra:9042",
"enabled": true
},
{
"name": "xandra_test_cassandra_sliced",
"listen": "0.0.0.0:9043",
"upstream": "cassandra:9042",
"enabled": true
}
]
4 changes: 2 additions & 2 deletions test/xandra_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule XandraTest do
conn = start_supervised!({Xandra, options})
assert_receive {[:xandra, :failed_to_connect], ^telemetry_ref, %{}, %{connection: ^conn}}

assert {:error, %ConnectionError{action: "request", reason: :not_connected}} =
assert {:error, %ConnectionError{action: "check out connection", reason: :not_connected}} =
Xandra.execute(conn, "USE some_keyspace")
end

Expand Down Expand Up @@ -179,7 +179,7 @@ defmodule XandraTest do

assert {:ok, conn} = start_supervised({Xandra, nodes: ["localhost:#{port}"]})

assert {:error, %ConnectionError{action: "request", reason: :not_connected}} =
assert {:error, %ConnectionError{action: "check out connection", reason: :not_connected}} =
Xandra.execute(conn, "SELECT * FROM system.local")

assert :ok = Task.await(task)
Expand Down
44 changes: 44 additions & 0 deletions test/xandra_toxiproxy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is in a separate test file so that we can run the rest of the tests with async: true.
defmodule XandraToxiproxyTest do
use XandraTest.IntegrationCase, async: false

alias Xandra.ConnectionError

@moduletag :toxiproxy

test "execute/3,4 supports a network that slices packets",
%{start_options: opts, keyspace: keyspace} do
ToxiproxyEx.get!(:xandra_test_cassandra_sliced)
|> ToxiproxyEx.toxic(:slicer, average_size: 50, size_variation: 25, delay: _microsec = 50)
|> ToxiproxyEx.apply!(fn ->
opts = Keyword.merge(opts, nodes: ["127.0.0.1:19152"], keyspace: keyspace)
conn = start_supervised!({Xandra, opts})
assert {:ok, prepared} = Xandra.prepare(conn, "SELECT * FROM system.local WHERE key = ?")
assert {:ok, page} = Xandra.execute(conn, prepared, ["local"])
assert [%{}] = Enum.to_list(page)
end)
end

test "prepare/3 when the connection is down", %{start_options: opts} do
opts = Keyword.merge(opts, nodes: ["127.0.0.1:19052"])
conn = start_supervised!({Xandra, opts})

ToxiproxyEx.get!(:xandra_test_cassandra)
|> ToxiproxyEx.down!(fn ->
assert {:error, %ConnectionError{reason: :not_connected}} =
Xandra.prepare(conn, "SELECT * FROM system.local")
end)
end

test "prepare/3 when the connection goes down mid request", %{start_options: opts} do

Check failure on line 33 in test/xandra_toxiproxy_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.15.4, OTP 25.3, C* 4.1, Scylla 5.1.6, Native protocol v3)

test prepare/3 when the connection goes down mid request (XandraToxiproxyTest)
opts = Keyword.merge(opts, nodes: ["127.0.0.1:19052"])
conn = start_supervised!({Xandra, opts})

ToxiproxyEx.get!(:xandra_test_cassandra)
|> ToxiproxyEx.toxic(:limit_data, bytes: 120)
|> ToxiproxyEx.apply!(fn ->
assert {:error, %ConnectionError{reason: :disconnected}} =
Xandra.prepare(conn, "SELECT * FROM system.local")
end)
end
end

0 comments on commit dd537cf

Please sign in to comment.