mirror of
https://github.com/debauchee/barrier.git
synced 2026-07-09 13:26:16 +08:00
finished ipc connect with unit test.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user