@@ -56,10 +56,28 @@ class MockHealthCheckedConnection: public HealthCheckedConnection {
5656
5757 MockHealthCheckedConnection (ola::io::ConnectedDescriptor *descriptor,
5858 SelectServer *scheduler,
59+ const ola::TimeInterval heartbeat_interval,
5960 const ola::TimeInterval timeout_interval,
6061 const Options &options,
6162 MockClock *clock)
62- : HealthCheckedConnection(scheduler, timeout_interval),
63+ : HealthCheckedConnection(scheduler,
64+ heartbeat_interval,
65+ timeout_interval),
66+ m_descriptor (descriptor),
67+ m_ss(scheduler),
68+ m_options(options),
69+ m_next_heartbeat(0 ),
70+ m_expected_heartbeat(0 ),
71+ m_channel_ok(true ),
72+ m_clock(clock) {
73+ }
74+
75+ MockHealthCheckedConnection (ola::io::ConnectedDescriptor *descriptor,
76+ SelectServer *scheduler,
77+ const ola::TimeInterval heartbeat_interval,
78+ const Options &options,
79+ MockClock *clock)
80+ : HealthCheckedConnection(scheduler, heartbeat_interval),
6381 m_descriptor(descriptor),
6482 m_ss(scheduler),
6583 m_options(options),
@@ -70,8 +88,10 @@ class MockHealthCheckedConnection: public HealthCheckedConnection {
7088 }
7189
7290 void SendHeartbeat () {
91+ OLA_DEBUG << " Maybe send heartbeat" ;
7392 if (m_options.send_every == 0 ||
7493 m_next_heartbeat % m_options.send_every == 0 ) {
94+ OLA_DEBUG << " Sending heartbeat" ;
7595 m_descriptor->Send (&m_next_heartbeat, sizeof (m_next_heartbeat));
7696 }
7797 m_clock->AdvanceTime (0 , 180000 );
@@ -115,13 +135,18 @@ class HealthCheckedConnectionTest: public CppUnit::TestFixture {
115135 HealthCheckedConnectionTest ()
116136 : CppUnit::TestFixture(),
117137 m_ss (NULL , &m_clock),
118- heartbeat_interval(0 , 200000 ) {
138+ heartbeat_interval(0 , 200000 ),
139+ // Allow a little bit of wiggle room so we don't hit timing issues
140+ // when running the tests
141+ timeout_interval(0 , 650000 ) {
119142 }
120143
121144 CPPUNIT_TEST_SUITE (HealthCheckedConnectionTest);
122145 CPPUNIT_TEST (testSimpleChannel);
123146 CPPUNIT_TEST (testChannelWithPacketLoss);
124147 CPPUNIT_TEST (testChannelWithHeavyPacketLoss);
148+ CPPUNIT_TEST (testChannelWithHeavyPacketLossLongerTimeout);
149+ CPPUNIT_TEST (testChannelWithVeryHeavyPacketLossLongerTimeout);
125150 CPPUNIT_TEST (testPauseAndResume);
126151 CPPUNIT_TEST_SUITE_END ();
127152
@@ -131,6 +156,8 @@ class HealthCheckedConnectionTest: public CppUnit::TestFixture {
131156 void testSimpleChannel ();
132157 void testChannelWithPacketLoss ();
133158 void testChannelWithHeavyPacketLoss ();
159+ void testChannelWithHeavyPacketLossLongerTimeout ();
160+ void testChannelWithVeryHeavyPacketLossLongerTimeout ();
134161 void testPauseAndResume ();
135162
136163 void PauseReading (MockHealthCheckedConnection *connection) {
@@ -148,6 +175,7 @@ class HealthCheckedConnectionTest: public CppUnit::TestFixture {
148175 SelectServer m_ss;
149176 LoopbackDescriptor socket;
150177 TimeInterval heartbeat_interval;
178+ TimeInterval timeout_interval;
151179 MockHealthCheckedConnection::Options options;
152180};
153181
@@ -206,7 +234,7 @@ void HealthCheckedConnectionTest::testChannelWithPacketLoss() {
206234
207235
208236/* *
209- * Check the channel works when every 2nd heartbeat is lost
237+ * Check the channel fails when 2 of every 3 heartbeats are lost
210238 */
211239void HealthCheckedConnectionTest::testChannelWithHeavyPacketLoss () {
212240 options.send_every = 3 ;
@@ -228,6 +256,57 @@ void HealthCheckedConnectionTest::testChannelWithHeavyPacketLoss() {
228256}
229257
230258
259+ /* *
260+ * Check the channel works when 2 of every 3 heartbeats are lost but the
261+ * timeout interval is 3 * heartbeat_interval rather than the default
262+ */
263+ void HealthCheckedConnectionTest::
264+ testChannelWithHeavyPacketLossLongerTimeout () {
265+ options.send_every = 3 ;
266+ MockHealthCheckedConnection connection (&socket,
267+ &m_ss,
268+ heartbeat_interval,
269+ timeout_interval,
270+ options,
271+ &m_clock);
272+
273+ socket.SetOnData (
274+ NewCallback (&connection, &MockHealthCheckedConnection::ReadData));
275+ connection.Setup ();
276+ m_ss.AddReadDescriptor (&socket);
277+ connection.Setup ();
278+
279+ m_ss.Run ();
280+ OLA_ASSERT_TRUE (connection.ChannelOk ());
281+ }
282+
283+
284+ /* *
285+ * Check the channel fails when 3 of every 4 heartbeats are lost even though
286+ * the timeout interval is 3 * heartbeat_interval
287+ */
288+ void HealthCheckedConnectionTest::
289+ testChannelWithVeryHeavyPacketLossLongerTimeout () {
290+ options.send_every = 4 ;
291+ options.abort_on_failure = false ;
292+ MockHealthCheckedConnection connection (&socket,
293+ &m_ss,
294+ heartbeat_interval,
295+ timeout_interval,
296+ options,
297+ &m_clock);
298+
299+ socket.SetOnData (
300+ NewCallback (&connection, &MockHealthCheckedConnection::ReadData));
301+ connection.Setup ();
302+ m_ss.AddReadDescriptor (&socket);
303+ connection.Setup ();
304+
305+ m_ss.Run ();
306+ OLA_ASSERT_FALSE (connection.ChannelOk ());
307+ }
308+
309+
231310/* *
232311 * Check pausing doesn't mark the channel as bad.
233312 */
0 commit comments