From 67f54b078d2d750a5265da78596a46a9d7150441 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Fri, 20 Oct 2023 15:26:01 +0200 Subject: [PATCH] Fix bug with Scylla protocol (#346) --- docker-compose.yml | 3 +- lib/xandra/connection.ex | 2 +- mix.exs | 98 ++++++++++++++++++++++++++++++---- test/xandra/cluster_test.exs | 7 ++- test/xandra_toxiproxy_test.exs | 1 + 5 files changed, 97 insertions(+), 14 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8dee4559..8168fb2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,10 +50,9 @@ services: context: ./test/docker dockerfile: scylladb.dockerfile args: - SCYLLA_VERSION: ${SCYLLA_VERSION:-5.1.6} + SCYLLA_VERSION: ${SCYLLA_VERSION:-5.2} ports: - "9062:9042" - - "10000:10000" command: - "--smp" - "1" diff --git a/lib/xandra/connection.ex b/lib/xandra/connection.ex index 0979b7d4..73ced8ef 100644 --- a/lib/xandra/connection.ex +++ b/lib/xandra/connection.ex @@ -384,7 +384,7 @@ defmodule Xandra.Connection do connect_timeout: Keyword.fetch!(options, :connect_timeout), connection_name: Keyword.get(options, :name), cluster_pid: Keyword.get(options, :cluster_pid), - protocol_version: Keyword.get(options, :protocol_version), + protocol_version: data.protocol_version || Keyword.get(options, :protocol_version), options: options, backoff: data.backoff || diff --git a/mix.exs b/mix.exs index 79389e65..d6cf34ac 100644 --- a/mix.exs +++ b/mix.exs @@ -32,8 +32,11 @@ defmodule Xandra.Mixfile do # Testing preferred_cli_env: [ + "test.cassandra": :test, "test.scylladb": :test, "test.native_protocols": :test, + "test.all": :test, + "test.all_with_html_coverage": :test, "coveralls.html": :test ], test_coverage: [tool: ExCoveralls], @@ -75,16 +78,45 @@ defmodule Xandra.Mixfile do defp aliases() do [ test: "test --exclude scylla_specific", - "test.scylladb": [ - fn _args -> - System.put_env("CASSANDRA_PORT", "9062") - System.put_env("CASSANDRA_WITH_AUTH_PORT", "9063") - end, - "test --exclude cassandra_specific --exclude encryption --include scylla_specific" - ], + "test.cassandra": fn args -> + print_header("Running Cassandra tests") + + mix_cmd_with_status_check( + ["test", "--exclude", "scylla_specific", ansi_option() | args], + [ + {"CASSANDRA_PORT", "9052"}, + {"CASSANDRA_WITH_AUTH_PORT", "9053"} + ] + ) + end, + "test.scylladb": fn args -> + print_header("Running ScyllaDB tests") + + mix_cmd_with_status_check( + [ + "test", + "--exclude", + "cassandra_specific", + "--exclude", + "encryption", + "--include", + "scylla_specific", + ansi_option() | args + ], + [ + {"CASSANDRA_PORT", "9062"}, + {"CASSANDRA_WITH_AUTH_PORT", "9063"} + ] + ) + end, "test.all": fn args -> - Mix.Task.run(:test, args) - Mix.Task.run(:"test.scylladb", args) + Mix.Task.run("test.cassandra", args) + Mix.Task.run("test.scylladb", args) + end, + "test.all_with_html_coverage": fn args -> + Mix.Task.run("test.cassandra", ["--cover", "--export-coverage", "cassandra" | args]) + Mix.Task.run("test.scylladb", ["--cover", "--export-coverage", "scylla" | args]) + Mix.Task.run("coveralls.html", ["--exclude", "test", "--import-cover", "cover"]) end, docs: [ "run pages/generate_telemetry_events_page.exs", @@ -109,4 +141,52 @@ defmodule Xandra.Mixfile do {:toxiproxy_ex, github: "whatyouhide/toxiproxy_ex", only: :test} ] end + + defp mix_cmd_with_status_check(args, env) do + port = + Port.open({:spawn_executable, System.find_executable("mix")}, [ + :binary, + :exit_status, + args: args, + env: + Enum.map(env, fn {key, val} -> + {String.to_charlist(key), String.to_charlist(val)} + end) + ]) + + # We want a port so that we can shut down the port if we shut down the system. + receive_loop(port) + end + + defp receive_loop(port) do + receive do + {^port, {:data, data}} -> + :ok = IO.write(data) + receive_loop(port) + + {^port, {:exit_status, 0}} -> + :ok + + {^port, {:exit_status, status}} -> + Mix.raise("Mix failed with exit status #{status}") + after + 10_000 -> + Mix.raise("Timed out waiting for Mix to send back any data (after 10s)") + end + end + + defp ansi_option do + if IO.ANSI.enabled?(), do: "--color", else: "--no-color" + end + + defp print_header(header) do + Mix.shell().info([:cyan, :bright, header, :reset]) + + Mix.shell().info([ + :cyan, + :bright, + String.duplicate("=", String.length(header)) <> "\n", + :reset + ]) + end end diff --git a/test/xandra/cluster_test.exs b/test/xandra/cluster_test.exs index 80c991f7..53330cc3 100644 --- a/test/xandra/cluster_test.exs +++ b/test/xandra/cluster_test.exs @@ -341,16 +341,19 @@ defmodule Xandra.ClusterTest do host: %Host{address: {198, 0, 0, 2}, port: @port, data_center: "local_dc"} } + node1_address = "198.0.0.1:#{@port}" + node2_address = "198.0.0.2:#{@port}" + assert_receive {^test_ref, PoolMock, :init_called, %{ pool_size: 1, - connection_options: %{nodes: ["198.0.0.1:9052"], cluster_pid: ^pid} + connection_options: %{nodes: [^node1_address], cluster_pid: ^pid} }} assert_receive {^test_ref, PoolMock, :init_called, %{ pool_size: 1, - connection_options: %{nodes: ["198.0.0.2:9052"], cluster_pid: ^pid} + connection_options: %{nodes: [^node2_address], cluster_pid: ^pid} }} refute_receive {[:xandra, :cluster, :pool, :started], ^telemetry_ref, %{}, diff --git a/test/xandra_toxiproxy_test.exs b/test/xandra_toxiproxy_test.exs index 2a91e882..64574005 100644 --- a/test/xandra_toxiproxy_test.exs +++ b/test/xandra_toxiproxy_test.exs @@ -6,6 +6,7 @@ defmodule XandraToxiproxyTest do @moduletag :toxiproxy + @tag :cassandra_specific test "execute/3,4 supports a network that slices packets", %{start_options: opts, keyspace: keyspace} do ToxiproxyEx.get!(:xandra_test_cassandra)