1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* HealthCheckedConnectionTest.cpp
* Test fixture for the HealthCheckedConnection class.
* Copyright (C) 2012 Simon Newton
*/
#include <cppunit/extensions/HelperMacros.h>
#include "ola/Callback.h"
#include "ola/Clock.h"
#include "ola/Logging.h"
#include "ola/io/SelectServer.h"
#include "ola/network/HealthCheckedConnection.h"
#include "ola/network/Socket.h"
#include "ola/testing/TestUtils.h"
using ola::MockClock;
using ola::NewCallback;
using ola::NewSingleCallback;
using ola::TimeInterval;
using ola::network::HealthCheckedConnection;
using ola::io::LoopbackDescriptor;
using ola::io::SelectServer;
class MockHealthCheckedConnection: public HealthCheckedConnection {
public:
struct Options {
uint8_t end_after; // terminate after this many heartbeats
uint8_t send_every; // don't send every N heartbeats
bool validate_heartbeat; // check the heartbeat is what we expect
bool abort_on_failure; // fail if the channel goes down
};
static void InitOptions(Options *options) {
options->end_after = 8;
options->send_every = 0;
options->validate_heartbeat = false;
options->abort_on_failure = true;
}
MockHealthCheckedConnection(ola::io::ConnectedDescriptor *descriptor,
SelectServer *scheduler,
const ola::TimeInterval timeout_interval,
const Options &options,
MockClock *clock)
: HealthCheckedConnection(scheduler, timeout_interval),
m_descriptor(descriptor),
m_ss(scheduler),
m_options(options),
m_next_heartbeat(0),
m_expected_heartbeat(0),
m_channel_ok(true),
m_clock(clock) {
}
void SendHeartbeat() {
if (m_options.send_every == 0 ||
m_next_heartbeat % m_options.send_every == 0) {
m_descriptor->Send(&m_next_heartbeat, sizeof(m_next_heartbeat));
}
m_clock->AdvanceTime(0, 180000);
m_next_heartbeat++;
}
void HeartbeatTimeout() {
if (m_options.abort_on_failure)
CPPUNIT_FAIL("Channel went down");
m_channel_ok = false;
m_ss->Terminate();
}
void ReadData() {
uint8_t data;
unsigned int data_read;
m_descriptor->Receive(&data, sizeof(data), data_read);
if (m_options.validate_heartbeat)
OLA_ASSERT_EQ(m_expected_heartbeat++, data);
HeartbeatReceived();
if (data >= m_options.end_after)
m_ss->Terminate();
}
bool ChannelOk() const { return m_channel_ok; }
private:
ola::io::ConnectedDescriptor *m_descriptor;
SelectServer *m_ss;
Options m_options;
uint8_t m_next_heartbeat;
uint8_t m_expected_heartbeat;
bool m_channel_ok;
MockClock *m_clock;
};
class HealthCheckedConnectionTest: public CppUnit::TestFixture {
public:
HealthCheckedConnectionTest()
: CppUnit::TestFixture(),
m_ss(NULL, &m_clock),
heartbeat_interval(0, 200000) {
}
CPPUNIT_TEST_SUITE(HealthCheckedConnectionTest);
CPPUNIT_TEST(testSimpleChannel);
CPPUNIT_TEST(testChannelWithPacketLoss);
CPPUNIT_TEST(testChannelWithHeavyPacketLoss);
CPPUNIT_TEST(testPauseAndResume);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown() {}
void testSimpleChannel();
void testChannelWithPacketLoss();
void testChannelWithHeavyPacketLoss();
void testPauseAndResume();
void PauseReading(MockHealthCheckedConnection *connection) {
connection->PauseTimer();
m_ss.RemoveReadDescriptor(&socket);
}
void ResumeReading(MockHealthCheckedConnection *connection) {
connection->ResumeTimer();
m_ss.AddReadDescriptor(&socket);
}
private:
MockClock m_clock;
SelectServer m_ss;
LoopbackDescriptor socket;
TimeInterval heartbeat_interval;
MockHealthCheckedConnection::Options options;
};
CPPUNIT_TEST_SUITE_REGISTRATION(HealthCheckedConnectionTest);
void HealthCheckedConnectionTest::setUp() {
socket.Init();
MockHealthCheckedConnection::InitOptions(&options);
}
/*
* Check that the channel stays up when all heartbeats are received.
*/
void HealthCheckedConnectionTest::testSimpleChannel() {
options.validate_heartbeat = true;
MockHealthCheckedConnection connection(&socket,
&m_ss,
heartbeat_interval,
options,
&m_clock);
socket.SetOnData(
NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
connection.Setup();
m_ss.AddReadDescriptor(&socket);
connection.Setup();
m_ss.Run();
OLA_ASSERT_TRUE(connection.ChannelOk());
}
/**
* Check the channel works when every 2nd heartbeat is lost
*/
void HealthCheckedConnectionTest::testChannelWithPacketLoss() {
options.send_every = 2;
MockHealthCheckedConnection connection(&socket,
&m_ss,
heartbeat_interval,
options,
&m_clock);
socket.SetOnData(
NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
connection.Setup();
m_ss.AddReadDescriptor(&socket);
connection.Setup();
m_ss.Run();
OLA_ASSERT_TRUE(connection.ChannelOk());
}
/**
* Check the channel works when every 2nd heartbeat is lost
*/
void HealthCheckedConnectionTest::testChannelWithHeavyPacketLoss() {
options.send_every = 3;
options.abort_on_failure = false;
MockHealthCheckedConnection connection(&socket,
&m_ss,
heartbeat_interval,
options,
&m_clock);
socket.SetOnData(
NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
connection.Setup();
m_ss.AddReadDescriptor(&socket);
connection.Setup();
m_ss.Run();
OLA_ASSERT_FALSE(connection.ChannelOk());
}
/**
* Check pausing doesn't mark the channel as bad.
*/
void HealthCheckedConnectionTest::testPauseAndResume() {
MockHealthCheckedConnection connection(&socket,
&m_ss,
heartbeat_interval,
options,
&m_clock);
socket.SetOnData(
NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
connection.Setup();
m_ss.AddReadDescriptor(&socket);
connection.Setup();
m_ss.RegisterSingleTimeout(
TimeInterval(1, 0),
NewSingleCallback(this,
&HealthCheckedConnectionTest::PauseReading,
&connection));
m_ss.RegisterSingleTimeout(
TimeInterval(3, 0),
NewSingleCallback(this,
&HealthCheckedConnectionTest::ResumeReading,
&connection));
m_ss.Run();
OLA_ASSERT_TRUE(connection.ChannelOk());
}
|