Skip to content

Commit

Permalink
Clean up logs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Aug 6, 2023
1 parent 339e784 commit c5b758e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
2 changes: 0 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ excluded =
end

ExUnit.start(exclude: excluded, assert_receive_timeout: 1_000)

Xandra.Telemetry.attach_default_handler()
62 changes: 40 additions & 22 deletions test/xandra/cluster/control_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,53 @@ defmodule Xandra.Cluster.ControlConnectionTest do

test "trying all the nodes in the contact points",
%{mirror_ref: mirror_ref, start_options: start_options} do
log =
capture_log(fn ->
start_control_connection!(start_options, contact_points: ["127.0.0.1:9039", "127.0.0.1"])
assert_receive {^mirror_ref, {:discovered_hosts, [local_peer]}}
assert %Host{address: {127, 0, 0, 1}} = local_peer
end)
telemetry_ref =
:telemetry_test.attach_event_handlers(self(), [
[:xandra, :cluster, :control_connection, :failed_to_connect],
[:xandra, :cluster, :control_connection, :connected]
])

start_control_connection!(start_options, contact_points: ["127.0.0.1:9039", "127.0.0.1"])
assert_receive {^mirror_ref, {:discovered_hosts, [local_peer]}}
assert %Host{address: {127, 0, 0, 1}} = local_peer

assert log =~ "Control connection failed to connect"
assert log =~ "xandra_address=127.0.0.1"
assert log =~ "xandra_port=9039"
assert_receive {[:xandra, :cluster, :control_connection, :failed_to_connect], ^telemetry_ref,
%{}, metadata}

assert %{reason: :econnrefused, host: %Host{address: ~c"127.0.0.1", port: 9039}} = metadata

assert_receive {[:xandra, :cluster, :control_connection, :connected], ^telemetry_ref, %{},
%{}}
end

test "when all contact points are unavailable",
%{mirror_ref: mirror_ref, start_options: start_options} do
log =
capture_log(fn ->
ctrl_conn =
start_control_connection!(start_options,
contact_points: ["127.0.0.1:9098", "127.0.0.1:9099"]
)
telemetry_ref =
:telemetry_test.attach_event_handlers(self(), [
[:xandra, :cluster, :control_connection, :failed_to_connect],
[:xandra, :cluster, :control_connection, :connected]
])

refute_receive {^mirror_ref, _}, 500
assert {:disconnected, _data} = :sys.get_state(ctrl_conn)
end)
ctrl_conn =
start_control_connection!(start_options,
contact_points: ["127.0.0.1:9098", "127.0.0.1:9099"]
)

refute_receive {^mirror_ref, _}, 500
assert {:disconnected, _data} = :sys.get_state(ctrl_conn)

assert_received {[:xandra, :cluster, :control_connection, :failed_to_connect], ^telemetry_ref,
%{}, metadata}

assert %{reason: :econnrefused, host: %Host{address: ~c"127.0.0.1", port: 9098}} = metadata

assert_received {[:xandra, :cluster, :control_connection, :failed_to_connect], ^telemetry_ref,
%{}, metadata}

assert %{reason: :econnrefused, host: %Host{address: ~c"127.0.0.1", port: 9099}} = metadata

assert log =~ "Control connection failed to connect"
assert log =~ "xandra_address=127.0.0.1"
assert log =~ "xandra_port=9098"
assert log =~ "xandra_port=9099"
refute_received {[:xandra, :cluster, :control_connection, :connected], ^telemetry_ref, %{},
%{}}
end

test "reconnecting after the node closes its socket",
Expand Down

0 comments on commit c5b758e

Please sign in to comment.