diff --git a/nano/core_test/network.cpp b/nano/core_test/network.cpp index 7d8698f36..a9c5c1b8d 100644 --- a/nano/core_test/network.cpp +++ b/nano/core_test/network.cpp @@ -1091,9 +1091,9 @@ TEST (network, reconnect_cached) /* * Tests that channel and channel container removes channels with dead local sockets */ -TEST (network, purge_dead_channel_outgoing) +TEST (network, purge_dead_channel) { - nano::test::system system{}; + nano::test::system system; nano::node_flags flags; // Disable non realtime sockets @@ -1106,36 +1106,14 @@ TEST (network, purge_dead_channel_outgoing) auto & node1 = *system.add_node (flags); - // We expect one incoming and one outgoing connection - std::shared_ptr outgoing; - std::shared_ptr incoming; - - std::atomic connected_count{ 0 }; - node1.observers.socket_connected.add ([&] (nano::transport::socket & socket) { - connected_count++; - outgoing = socket.shared_from_this (); - - std::cout << "connected: " << socket.remote_endpoint () << std::endl; - }); - - std::atomic accepted_count{ 0 }; - node1.observers.socket_accepted.add ([&] (nano::transport::socket & socket) { - accepted_count++; - incoming = socket.shared_from_this (); - - std::cout << "accepted: " << socket.remote_endpoint () << std::endl; + node1.observers.socket_connected.add ([&] (nano::transport::socket & sock) { + system.logger.debug (nano::log::type::test, "Connected: {}", sock); }); auto & node2 = *system.add_node (flags); - ASSERT_TIMELY_EQ (5s, connected_count, 1); - ASSERT_ALWAYS_EQ (1s, connected_count, 1); - - ASSERT_TIMELY_EQ (5s, accepted_count, 1); - ASSERT_ALWAYS_EQ (1s, accepted_count, 1); - ASSERT_EQ (node1.network.size (), 1); - ASSERT_ALWAYS_EQ (1s, node1.network.size (), 1); + ASSERT_ALWAYS_EQ (500ms, node1.network.size (), 1); // Store reference to the only channel auto channels = node1.network.list (); @@ -1143,29 +1121,29 @@ TEST (network, purge_dead_channel_outgoing) auto channel = channels.front (); ASSERT_TRUE (channel); + auto sockets = node1.tcp_listener.sockets (); + ASSERT_EQ (sockets.size (), 1); + auto socket = sockets.front (); + ASSERT_TRUE (socket); + // When socket is dead ensure channel knows about that ASSERT_TRUE (channel->alive ()); - outgoing->close (); - ASSERT_TIMELY (5s, !channel->alive ()); + socket->close (); + ASSERT_TIMELY (10s, !channel->alive ()); - // Shortly after that a new channel should be established - ASSERT_TIMELY_EQ (5s, connected_count, 2); - ASSERT_ALWAYS_EQ (1s, connected_count, 2); - - // Check that a new channel is healthy - auto channels2 = node1.network.list (); - ASSERT_EQ (channels2.size (), 1); - auto channel2 = channels2.front (); - ASSERT_TRUE (channel2); - ASSERT_TRUE (channel2->alive ()); + auto channel_exists = [] (auto & node, auto & channel) { + auto channels = node.network.list (); + return std::find (channels.begin (), channels.end (), channel) != channels.end (); + }; + ASSERT_TIMELY (5s, !channel_exists (node1, channel)); } /* * Tests that channel and channel container removes channels with dead remote sockets */ -TEST (network, purge_dead_channel_incoming) +TEST (network, purge_dead_channel_remote) { - nano::test::system system{}; + nano::test::system system; nano::node_flags flags; // Disable non realtime sockets @@ -1177,37 +1155,15 @@ TEST (network, purge_dead_channel_incoming) flags.disable_wallet_bootstrap = true; auto & node1 = *system.add_node (flags); - - // We expect one incoming and one outgoing connection - std::shared_ptr outgoing; - std::shared_ptr incoming; - - std::atomic connected_count{ 0 }; - node1.observers.socket_connected.add ([&] (nano::transport::socket & socket) { - connected_count++; - outgoing = socket.shared_from_this (); - - std::cout << "connected: " << socket.remote_endpoint () << std::endl; - }); - - std::atomic accepted_count{ 0 }; - node1.observers.socket_accepted.add ([&] (nano::transport::socket & socket) { - accepted_count++; - incoming = socket.shared_from_this (); - - std::cout << "accepted: " << socket.remote_endpoint () << std::endl; - }); - auto & node2 = *system.add_node (flags); - ASSERT_TIMELY_EQ (5s, connected_count, 1); - ASSERT_ALWAYS_EQ (1s, connected_count, 1); - - ASSERT_TIMELY_EQ (5s, accepted_count, 1); - ASSERT_ALWAYS_EQ (1s, accepted_count, 1); + node2.observers.socket_connected.add ([&] (nano::transport::socket & sock) { + system.logger.debug (nano::log::type::test, "Connected: {}", sock); + }); + ASSERT_EQ (node1.network.size (), 1); ASSERT_EQ (node2.network.size (), 1); - ASSERT_ALWAYS_EQ (1s, node2.network.size (), 1); + ASSERT_ALWAYS_EQ (500ms, std::min (node1.network.size (), node2.network.size ()), 1); // Store reference to the only channel auto channels = node2.network.list (); @@ -1215,19 +1171,19 @@ TEST (network, purge_dead_channel_incoming) auto channel = channels.front (); ASSERT_TRUE (channel); + auto sockets = node1.tcp_listener.sockets (); + ASSERT_EQ (sockets.size (), 1); + auto socket = sockets.front (); + ASSERT_TRUE (socket); + // When remote socket is dead ensure channel knows about that ASSERT_TRUE (channel->alive ()); - incoming->close (); + socket->close (); ASSERT_TIMELY (5s, !channel->alive ()); - // Shortly after that a new channel should be established - ASSERT_TIMELY_EQ (5s, accepted_count, 2); - ASSERT_ALWAYS_EQ (1s, accepted_count, 2); - - // Check that a new channel is healthy - auto channels2 = node2.network.list (); - ASSERT_EQ (channels2.size (), 1); - auto channel2 = channels2.front (); - ASSERT_TRUE (channel2); - ASSERT_TRUE (channel2->alive ()); + auto channel_exists = [] (auto & node, auto & channel) { + auto channels = node.network.list (); + return std::find (channels.begin (), channels.end (), channel) != channels.end (); + }; + ASSERT_TIMELY (5s, !channel_exists (node2, channel)); }