finished ipc connect with unit test.

This commit is contained in:
Nick Bolton
2012-07-01 21:18:21 +00:00
parent d829e854a4
commit 05775bc73d
5 changed files with 171 additions and 14 deletions

View File

@@ -20,17 +20,105 @@
#include "CIpcClient.h"
#include "CSocketMultiplexer.h"
#include "CEventQueue.h"
#include "TMethodEventJob.h"
#include "CThread.h"
#include "TMethodJob.h"
#include "CArch.h"
#include "CLog.h"
TEST(CIpcTests, connectToServer)
class CIpcTests : public ::testing::Test
{
public:
CIpcTests();
virtual ~CIpcTests();
void handleClientConnected(const CEvent&, void* vclient);
void raiseQuitEvent();
private:
void timeoutThread(void*);
public:
bool m_quitOnClientConnect;
bool m_clientConnected;
bool m_timeoutCheck;
double m_timeout;
private:
CThread* m_timeoutThread;
};
TEST_F(CIpcTests, connectToServer)
{
m_quitOnClientConnect = true;
CSocketMultiplexer multiplexer;
CEventQueue eventQueue;
CEventQueue events;
CIpcServer server;
server.listen();
events.adoptHandler(
CIpcServer::getClientConnectedEvent(), &server,
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::handleClientConnected));
CIpcClient client;
client.connect();
eventQueue.loop();
m_timeoutCheck = true;
m_timeout = ARCH->time() + 5; // 5 sec timeout.
events.loop();
EXPECT_EQ(true, m_clientConnected);
}
CIpcTests::CIpcTests() :
m_timeoutThread(nullptr),
m_quitOnClientConnect(false),
m_clientConnected(false),
m_timeoutCheck(false),
m_timeout(0)
{
m_timeoutThread = new CThread(
new TMethodJob<CIpcTests>(
this, &CIpcTests::timeoutThread, nullptr));
}
CIpcTests::~CIpcTests()
{
delete m_timeoutThread;
}
void
CIpcTests::handleClientConnected(const CEvent&, void* vclient)
{
m_clientConnected = true;
if (m_quitOnClientConnect) {
raiseQuitEvent();
}
}
void
CIpcTests::raiseQuitEvent()
{
EVENTQUEUE->addEvent(CEvent(CEvent::kQuit, nullptr));
}
void
CIpcTests::timeoutThread(void*)
{
while (true) {
if (!m_timeoutCheck) {
ARCH->sleep(1);
continue;
}
if (ARCH->time() > m_timeout) {
LOG((CLOG_ERR "timeout"));
raiseQuitEvent();
m_timeoutCheck = false;
}
}
}