/** * Test that backoff works. * This is quite brittle and should be fixed at some stage. */ void AdvancedTCPConnectorTest::testBackoff() { uint16_t port = ReservePort(); OLA_ASSERT_NE(0, port); IPV4SocketAddress target(m_localhost, port); // we advance the clock so remove the timeout closure m_ss->RemoveTimeout(m_timeout_id); m_timeout_id = ola::thread::INVALID_TIMEOUT; AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); // 5s per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); connector.AddEndpoint(target, &policy); OLA_ASSERT_EQ(1u, connector.EndpointCount()); // failed_attempts may be 0 or 1, depending on the platform. AdvancedTCPConnector::ConnectionState state; unsigned int failed_attempts; connector.GetEndpointState(target, &state, &failed_attempts); OLA_ASSERT_EQ(AdvancedTCPConnector::DISCONNECTED, state); OLA_ASSERT_TRUE(failed_attempts == 0 || failed_attempts == 1); // the timeout is 500ms, so we advance by 490 and set a 200ms timeout m_clock.AdvanceTime(0, 490000); m_ss->RunOnce(TimeInterval(0, 200000)); // should have one failure at this point ConfirmState(__LINE__, connector, target, AdvancedTCPConnector::DISCONNECTED, 1); // the next attempt should be in 5 seconds m_clock.AdvanceTime(4, 900000); m_ss->RunOnce(TimeInterval(1, 0)); // wait for the timeout m_clock.AdvanceTime(0, 490000); m_ss->RunOnce(TimeInterval(0, 200000)); ConfirmState(__LINE__, connector, target, AdvancedTCPConnector::DISCONNECTED, 2); // run once more to clean up m_ss->RunOnce(TimeInterval(0, 10000)); // clean up connector.RemoveEndpoint(target); OLA_ASSERT_EQ(0u, connector.EndpointCount()); }
/** * Test that backoff works. * This is quite brittle and should be fixed at some stage. */ void AdvancedTCPConnectorTest::testBackoff() { // we advance the clock so remove the timeout closure m_ss->RemoveTimeout(m_timeout_id); m_timeout_id = ola::thread::INVALID_TIMEOUT; AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); // 5s per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); connector.AddEndpoint(m_localhost, SERVER_PORT, &policy); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::DISCONNECTED, 0); // the timeout is 500ms, so we advance by 490 and set a 200ms timeout m_clock.AdvanceTime(0, 490000); m_ss->RunOnce(0, 200000); // should have one failure at this point ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::DISCONNECTED, 1); // the next attempt should be in 5 seconds m_clock.AdvanceTime(4, 900000); m_ss->RunOnce(1, 0); // wait for the timeout m_clock.AdvanceTime(0, 490000); m_ss->RunOnce(0, 200000); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::DISCONNECTED, 2); // run once more to clean up m_ss->RunOnce(0, 10000); // clean up connector.RemoveEndpoint(m_localhost, SERVER_PORT); CPPUNIT_ASSERT_EQUAL(0u, connector.EndpointCount()); }
/** * Test that pausing a connection works. */ void AdvancedTCPConnectorTest::testPause() { ola::network::TCPSocketFactory socket_factory( ola::NewCallback(this, &AdvancedTCPConnectorTest::AcceptedConnection)); TCPAcceptingSocket listening_socket(&socket_factory); SetupListeningSocket(&listening_socket); AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); // add endpoint, but make sure it's paused connector.AddEndpoint(m_server_address, &policy, true); OLA_ASSERT_EQ(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_server_address, AdvancedTCPConnector::PAUSED, 0); m_ss->RunOnce(TimeInterval(0, 500000)); // now unpause connector.Resume(m_server_address); // The socket may be connected immediately depending on the platform. AdvancedTCPConnector::ConnectionState state; unsigned int failed_attempts; connector.GetEndpointState(m_server_address, &state, &failed_attempts); if (state == AdvancedTCPConnector::DISCONNECTED) { m_ss->Run(); } OLA_ASSERT_EQ(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_server_address, AdvancedTCPConnector::CONNECTED, 0); // check our socket exists OLA_ASSERT_TRUE(m_connected_socket); m_connected_socket->Close(); delete m_connected_socket; connector.Disconnect(m_server_address, true); // state should be updated ConfirmState(__LINE__, connector, m_server_address, AdvancedTCPConnector::PAUSED, 0); // clean up connector.RemoveEndpoint(m_server_address); OLA_ASSERT_EQ(0u, connector.EndpointCount()); m_ss->RemoveReadDescriptor(&listening_socket); }
/** * Test that pausing a connection works. */ void AdvancedTCPConnectorTest::testPause() { ola::network::TCPSocketFactory socket_factory( ola::NewCallback(this, &AdvancedTCPConnectorTest::AcceptedConnection)); TcpAcceptingSocket listening_socket(&socket_factory); SetupListeningSocket(&listening_socket); AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); // add endpoint, but make sure it's paused connector.AddEndpoint(m_localhost, SERVER_PORT, &policy, true); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::PAUSED, 0); m_ss->RunOnce(0, 500000); // now unpause connector.Resume(m_localhost, SERVER_PORT); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::DISCONNECTED, 0); m_ss->Run(); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::CONNECTED, 0); // check our socket exists CPPUNIT_ASSERT(m_connected_socket); m_connected_socket->Close(); delete m_connected_socket; OLA_INFO << "disconnecting"; connector.Disconnect(m_localhost, SERVER_PORT, true); // state should be updated ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::PAUSED, 0); // clean up connector.RemoveEndpoint(m_localhost, SERVER_PORT); CPPUNIT_ASSERT_EQUAL(0u, connector.EndpointCount()); m_ss->RemoveReadDescriptor(&listening_socket); }
/* * Test that we can destroy the Connector and everything will work. */ void AdvancedTCPConnectorTest::testEarlyDestruction() { // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); { AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); connector.AddEndpoint(m_localhost, SERVER_PORT, &policy); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); } m_ss->RunOnce(); }
/* * Test that we can destroy the Connector and everything will work. */ void TCPConnectorTest::testEarlyDestruction() { uint16_t port = ReservePort(); OLA_ASSERT_NE(0, port); IPV4SocketAddress target(m_localhost, port); // attempt a non-blocking connect, hopefully nothing is running on port 9010 TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000); { TCPConnector connector(m_ss); connector.Connect( target, connect_timeout, ola::NewSingleCallback(this, &TCPConnectorTest::OnConnectFailure)); OLA_ASSERT_EQ(1u, connector.ConnectionsPending()); } m_ss->RunOnce(); }
/* * Test that we can destroy the Connector and everything will work. */ void AdvancedTCPConnectorTest::testEarlyDestruction() { uint16_t port = ReservePort(); OLA_ASSERT_NE(0, port); IPV4SocketAddress target(m_localhost, port); // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); { AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); connector.AddEndpoint(target, &policy); OLA_ASSERT_EQ(1u, connector.EndpointCount()); } m_ss->RunOnce(); }