/** * Test a short preamble. */ void TCPTransportTest::testShortPreamble() { uint8_t bogus_data[] = { 1, 2, 3, 4, 1, 2, 3, 4}; m_loopback.Send(bogus_data, sizeof(bogus_data)); m_ss->RunOnce(TimeInterval(1, 0)); m_loopback.CloseClient(); m_ss->RunOnce(TimeInterval(1, 0)); OLA_ASSERT(m_stream_ok); OLA_ASSERT_EQ(0u, m_pdus_received); }
SimpleE133Device::SimpleE133Device(const Options &options) : m_controller(options.controller), m_message_builder(ola::acn::CID::Generate(), "E1.33 Device"), m_tcp_socket_factory(NewCallback(this, &SimpleE133Device::OnTCPConnect)), m_connector(&m_ss, &m_tcp_socket_factory, TimeInterval(FLAGS_tcp_connect_timeout_ms / 1000, (FLAGS_tcp_connect_timeout_ms % 1000) * 1000)), m_backoff_policy(TimeInterval( FLAGS_tcp_retry_interval_ms / 1000, (FLAGS_tcp_retry_interval_ms % 1000) * 1000)), m_root_inflator(NewCallback(this, &SimpleE133Device::RLPDataReceived)) { m_connector.AddEndpoint(options.controller, &m_backoff_policy); }
/** * 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 the exponential backoff policy. */ void AdvancedTCPConnectorTest::testExponentialBackoffPolicy() { // start with 10s, up to 170s. ExponentialBackoffPolicy policy(TimeInterval(10, 0), TimeInterval(170, 0)); CPPUNIT_ASSERT_EQUAL(TimeInterval(10, 0), policy.BackOffTime(1)); CPPUNIT_ASSERT_EQUAL(TimeInterval(20, 0), policy.BackOffTime(2)); CPPUNIT_ASSERT_EQUAL(TimeInterval(40, 0), policy.BackOffTime(3)); CPPUNIT_ASSERT_EQUAL(TimeInterval(80, 0), policy.BackOffTime(4)); CPPUNIT_ASSERT_EQUAL(TimeInterval(160, 0), policy.BackOffTime(5)); CPPUNIT_ASSERT_EQUAL(TimeInterval(170, 0), policy.BackOffTime(6)); CPPUNIT_ASSERT_EQUAL(TimeInterval(170, 0), policy.BackOffTime(7)); }
/** * Test the exponential backoff policy. */ void BackoffTest::testExponentialBackoffPolicy() { // start with 10s, up to 170s. ExponentialBackoffPolicy policy(TimeInterval(10, 0), TimeInterval(170, 0)); OLA_ASSERT_EQ(TimeInterval(10, 0), policy.BackOffTime(1)); OLA_ASSERT_EQ(TimeInterval(20, 0), policy.BackOffTime(2)); OLA_ASSERT_EQ(TimeInterval(40, 0), policy.BackOffTime(3)); OLA_ASSERT_EQ(TimeInterval(80, 0), policy.BackOffTime(4)); OLA_ASSERT_EQ(TimeInterval(160, 0), policy.BackOffTime(5)); OLA_ASSERT_EQ(TimeInterval(170, 0), policy.BackOffTime(6)); OLA_ASSERT_EQ(TimeInterval(170, 0), policy.BackOffTime(7)); }
/** * 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()); ConfirmState(__LINE__, connector, target, 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, target, 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, target, AdvancedTCPConnector::DISCONNECTED, 2); // run once more to clean up m_ss->RunOnce(0, 10000); // clean up connector.RemoveEndpoint(target); OLA_ASSERT_EQ(0u, connector.EndpointCount()); }
/* * 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 don't leak memory when the AdvancedTCPConnector is destored * while a connecting is pending. */ 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()); } }
/** * Test the linear backoff policy. */ void AdvancedTCPConnectorTest::testLinearBackoffPolicy() { // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); CPPUNIT_ASSERT_EQUAL(TimeInterval(5, 0), policy.BackOffTime(1)); CPPUNIT_ASSERT_EQUAL(TimeInterval(10, 0), policy.BackOffTime(2)); CPPUNIT_ASSERT_EQUAL(TimeInterval(15, 0), policy.BackOffTime(3)); CPPUNIT_ASSERT_EQUAL(TimeInterval(30, 0), policy.BackOffTime(6)); CPPUNIT_ASSERT_EQUAL(TimeInterval(30, 0), policy.BackOffTime(7)); }
/* * Check for data loss. * TODO(simon): move to the ola server */ bool DmxMonitor::CheckDataLoss() { if (m_last_data.IsSet()) { TimeStamp now; Clock clock; clock.CurrentTime(&now); TimeInterval diff = now - m_last_data; if (diff > TimeInterval(2, 5000000)) { // loss of data DrawDataLossWindow(); } } return true; }
/* * Test that a TCP Connect works. */ void AdvancedTCPConnectorTest::testConnect() { 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)); connector.AddEndpoint(m_server_address, &policy); OLA_ASSERT_EQ(1u, connector.EndpointCount()); m_ss->Run(); OLA_ASSERT_EQ(1u, connector.EndpointCount()); // confirm the status is correct 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; OLA_INFO << "disconnecting"; connector.Disconnect(m_server_address, true); // state should be updated ConfirmState(__LINE__, connector, m_server_address, AdvancedTCPConnector::PAUSED, 0); // remove & shutdown connector.RemoveEndpoint(m_server_address); OLA_ASSERT_EQ(0u, connector.EndpointCount()); m_ss->RemoveReadDescriptor(&listening_socket); }
/** * Test the linear backoff policy. */ void BackoffTest::testLinearBackoffPolicy() { // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); OLA_ASSERT_EQ(TimeInterval(5, 0), policy.BackOffTime(1)); OLA_ASSERT_EQ(TimeInterval(10, 0), policy.BackOffTime(2)); OLA_ASSERT_EQ(TimeInterval(15, 0), policy.BackOffTime(3)); OLA_ASSERT_EQ(TimeInterval(30, 0), policy.BackOffTime(6)); OLA_ASSERT_EQ(TimeInterval(30, 0), policy.BackOffTime(7)); }
/* * Check RegisterSingleTimeout works. */ void TimeoutManagerTest::testSingleTimeouts() { MockClock clock; TimeoutManager timeout_manager(&m_map, &clock); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); TimeInterval timeout_interval(1, 0); timeout_id id1 = timeout_manager.RegisterSingleTimeout( timeout_interval, NewSingleCallback(this, &TimeoutManagerTest::HandleEvent, 1u)); OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT); TimeStamp last_checked_time; clock.AdvanceTime(0, 1); // Small offset to work around timer precision clock.CurrentTime(&last_checked_time); TimeInterval next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, timeout_interval); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, TimeInterval(0, 500000)); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_TRUE(next.IsZero()); OLA_ASSERT_EQ(1u, GetEventCounter(1)); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); // now add another timeout and then remove it timeout_id id2 = timeout_manager.RegisterSingleTimeout( timeout_interval, NewSingleCallback(this, &TimeoutManagerTest::HandleEvent, 2u)); OLA_ASSERT_NE(id2, ola::thread::INVALID_TIMEOUT); OLA_ASSERT_TRUE(timeout_manager.EventsPending()); OLA_ASSERT_EQ(0u, GetEventCounter(2)); timeout_manager.CancelTimeout(id2); clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); OLA_ASSERT_EQ(0u, GetEventCounter(2)); }
/* * Check RegisterRepeatingTimeout works. */ void TimeoutManagerTest::testRepeatingTimeouts() { MockClock clock; TimeoutManager timeout_manager(&m_map, &clock); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); TimeInterval timeout_interval(1, 0); timeout_id id1 = timeout_manager.RegisterRepeatingTimeout( timeout_interval, NewCallback(this, &TimeoutManagerTest::HandleRepeatingEvent, 1u)); OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT); TimeStamp last_checked_time; clock.AdvanceTime(0, 1); // Small offset to work around timer precision clock.CurrentTime(&last_checked_time); TimeInterval next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, timeout_interval); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, TimeInterval(0, 500000)); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_LTE(next, timeout_interval); OLA_ASSERT_EQ(1u, GetEventCounter(1)); OLA_ASSERT_TRUE(timeout_manager.EventsPending()); // fire the event again clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_LTE(next, timeout_interval); OLA_ASSERT_EQ(2u, GetEventCounter(1)); // cancel the event timeout_manager.CancelTimeout(id1); clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_TRUE(next.IsZero()); OLA_ASSERT_EQ(2u, GetEventCounter(1)); }
/* * Test the time based checks */ void DmxSourceTest::testIsActive() { DmxBuffer buffer("123456789"); TimeStamp timestamp; m_clock.CurrentTime(×tamp); DmxSource source(buffer, timestamp, 100); OLA_ASSERT(source.IsSet()); OLA_ASSERT(source.IsActive(timestamp)); TimeInterval interval(1000000); TimeStamp later = timestamp + interval; OLA_ASSERT(source.IsActive(later)); later = timestamp + TimeInterval(2500000); OLA_ASSERT_FALSE(source.IsActive(later)); }
/** * Test the BackoffGenerator */ void BackoffTest::testBackoffGenerator() { BackoffGenerator generator( new LinearBackoffPolicy(TimeInterval(5, 0), TimeInterval(30, 0))); OLA_ASSERT_EQ(TimeInterval(5, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(10, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(15, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(20, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(25, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(30, 0), generator.Next()); generator.Reset(); OLA_ASSERT_EQ(TimeInterval(5, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(10, 0), generator.Next()); OLA_ASSERT_EQ(TimeInterval(15, 0), generator.Next()); }