2012-06-28 07:29:06 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
|
* Copyright (C) 2012 Nick Bolton
|
|
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
|
*
|
|
|
|
|
* This package 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
#include "CIpcServer.h"
|
|
|
|
|
#include "CIpcClient.h"
|
2012-06-29 11:33:21 +00:00
|
|
|
#include "CSocketMultiplexer.h"
|
|
|
|
|
#include "CEventQueue.h"
|
2012-07-01 21:18:21 +00:00
|
|
|
#include "TMethodEventJob.h"
|
|
|
|
|
#include "CThread.h"
|
|
|
|
|
#include "TMethodJob.h"
|
|
|
|
|
#include "CArch.h"
|
|
|
|
|
#include "CLog.h"
|
2012-06-28 07:29:06 +00:00
|
|
|
|
2012-07-01 21:18:21 +00:00
|
|
|
class CIpcTests : public ::testing::Test
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2012-07-01 21:18:21 +00:00
|
|
|
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;
|
|
|
|
|
|
2012-06-29 11:33:21 +00:00
|
|
|
CSocketMultiplexer multiplexer;
|
2012-07-01 21:18:21 +00:00
|
|
|
CEventQueue events;
|
2012-06-28 07:29:06 +00:00
|
|
|
|
|
|
|
|
CIpcServer server;
|
|
|
|
|
server.listen();
|
|
|
|
|
|
2012-07-01 21:18:21 +00:00
|
|
|
events.adoptHandler(
|
|
|
|
|
CIpcServer::getClientConnectedEvent(), &server,
|
|
|
|
|
new TMethodEventJob<CIpcTests>(
|
|
|
|
|
this, &CIpcTests::handleClientConnected));
|
|
|
|
|
|
2012-06-28 07:29:06 +00:00
|
|
|
CIpcClient client;
|
|
|
|
|
client.connect();
|
2012-06-29 11:33:21 +00:00
|
|
|
|
2012-07-01 21:18:21 +00:00
|
|
|
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));
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
2012-07-01 21:18:21 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|