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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "testConnection.h"
#include "stubServer.h"
#include "testHarness.h"
#include <unistd.h> // for usleep()
#include <Atlas/Message/Element.h>
#include <Atlas/Objects/Operation/Login.h>
#include <Atlas/Objects/Operation/Info.h>
#include <Atlas/Objects/Operation/Sight.h>
#include <Atlas/Objects/Entity/Account.h>
#include <sigc++/bind.h>
#include <sigc++/signal.h>
#include <sigc++/object_slot.h>
#include <sigc++/connection.h>
#include "testUtils.h"
#include <Eris/Utils.h>
#include <Eris/Timestamp.h>
#include <Eris/PollDefault.h>
#include <Eris/SignalDispatcher.h>
using namespace Atlas::Message;
using namespace Atlas::Objects;
using namespace Time;
const short TEST_SERVER_PORT = 21588;
TestConnection::TestConnection()
{
;
}
void TestConnection::setUp()
{
//Time::Init();
m_con = new Eris::Connection("test_connection", false);
m_server = new StubServer(TEST_SERVER_PORT);
// attach to all the signals globaly for monitoring later
m_con->Timeout.connect(SigC::slot(*this, &TestConnection::onTimeout));
m_con->Failure.connect(SigC::slot(*this, &TestConnection::onFailure));
m_con->Connected.connect(SigC::slot(*this, &TestConnection::onConnect));
m_con->Disconnected.connect(SigC::slot(*this, &TestConnection::onDisconnect));
m_gotFailure = m_gotConnect = m_gotTimeout = false;
m_gotDisconnect = false;
}
void TestConnection::tearDown()
{
delete m_con;
delete m_server;
}
void TestConnection::waitFor(int timeoutSeconds, bool &watch1, bool &watch2)
{
Time::Stamp ts(Time::Stamp::now());
ts = ts + (timeoutSeconds * 1000);
while (!watch1 && !watch2) {
if (ts < Time::Stamp::now()) {
ERIS_MESSAGE("timed out in waitFor");
return;
}
m_server->run();
Eris::PollDefault::poll();
usleep(10000); // 10 msec = 1/100 of a second
}
}
void TestConnection::testConnect()
{
m_con->connect("127.0.0.1", TEST_SERVER_PORT);
ERIS_ASSERT(!m_gotFailure);
waitFor(10, m_gotConnect, m_gotFailure);
ERIS_ASSERT(!m_gotFailure);
ERIS_ASSERT(m_gotConnect);
ERIS_ASSERT(m_con->isConnected());
}
void TestConnection::testDisconnect()
{
testConnect();
SigC::Connection ref = m_con->Disconnecting.connect(
SigC::slot(*this, &TestConnection::onDisconnecting));
m_gotDisconnecting = false;
m_con->disconnect();
waitFor(30, m_gotDisconnect, m_gotFailure);
ERIS_ASSERT(!m_gotFailure);
ERIS_ASSERT(m_gotDisconnect);
ERIS_ASSERT(m_gotDisconnecting);
ref.disconnect();
}
void TestConnection::testComplexDisconnect()
{
// same as above, but set a dummy disconnecting op (and the time it out)
}
bool TestConnection::onDisconnecting()
{
ERIS_ASSERT(m_con->getStatus() == Eris::BaseConnection::DISCONNECTING);
m_gotDisconnecting = true;
return false;
}
bool TestConnection::onDisconnecting_delay()
{
m_con->lock();
Atlas::Message::Element::MapType mt;
mt["parents"] = Atlas::Message::Element::ListType(1, "delay");
mt["objtype"] = "op";
Atlas::Message::Element::MapType args;
args["time"] = 1.0; // wait a second
mt["args"] = Atlas::Message::Element::ListType(1, args);
m_con->send(mt);
return true;
}
void TestConnection::testReconnect()
{
testConnect();
// tell the server to pull the plug (mimic network loss / crash)
m_server->disconnect();
ERIS_ASSERT(m_gotDisconnect);
try {
m_con->reconnect();
waitFor(30, m_gotConnect, m_gotFailure);
ERIS_ASSERT(m_gotConnect);
ERIS_ASSERT(!m_gotFailure);
}
catch (...) {
ERIS_MESSAGE("got exception testing Connection::reconnect()");
}
}
void TestConnection::testTimeout()
{
m_server->setNegotiation(false); // tell it not to negotiate ever
m_con->connect("127.0.0.1", TEST_SERVER_PORT);
waitFor(60, m_gotFailure, m_gotTimeout);
ERIS_ASSERT(m_gotTimeout);
}
void TestConnection::testDispatch()
{
testConnect();
// add a sample dispatcher (assumes dispatcher works of course)
Eris::Dispatcher *rd = m_con->getDispatcher();
ERIS_ASSERT(rd);
Eris::Dispatcher *sigD = rd->addSubdispatch(new Eris::SignalDispatcher<Operation::Info>(
"dummy",
SigC::slot(*this, &TestConnection::onAnyDispatch)
));
Eris::Dispatcher *sigD2 = m_con->getDispatcherByPath("dummy");
ERIS_ASSERT(sigD == sigD2);
m_gotArbitraryDispatch = false;
Atlas::Message::Element::MapType testOp;
testOp["parents"] = Atlas::Message::Element::ListType(1, "info");
testOp["from"] = "stub-server";
testOp["to"] = "test-client";
m_server->push(testOp);
waitFor(10, m_gotFailure, m_gotArbitraryDispatch);
ERIS_ASSERT(!m_gotFailure);
ERIS_ASSERT_MESSAGE(m_gotArbitraryDispatch, "dispatch failed");
}
void TestConnection::onAnyDispatch(const Operation::Info &)
{
m_gotArbitraryDispatch = true;
}
void TestConnection::testSend()
{
testConnect();
Operation::Get get;
m_con->send(get);
m_server->waitForMessage(10);
Atlas::Message::Element received;
ERIS_ASSERT(m_server->get(received));
ERIS_ASSERT(getType(received) == "get");
}
void TestConnection::onConnect()
{
m_gotConnect=true;
}
void TestConnection::onFailure(const std::string &)
{
m_gotFailure=true;
}
void TestConnection::onTimeout(Eris::BaseConnection::Status)
{
m_gotTimeout=true;
}
void TestConnection::onDisconnect()
{
m_gotDisconnect = true;
}
|