Skip to content

Commit

Permalink
Work in Progress on SocketTestSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
Gei0r committed May 8, 2017
1 parent c6079f4 commit 1188477
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions tests/SocketTestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,42 @@
void SocketTestSuite::unixSockets()
{
const Path unixPathToUse("test_unixSocket");

// need to delete the file if it already exists
unixPathToUse.rm();

DeferredAsserter da;
std::mutex m;

// We need an event loop to receive signals.
EventLoop::SharedPtr loop(new EventLoop);
loop->init(EventLoop::MainEventLoop);

// create the server
SocketServer::SharedPtr s(new SocketServer);

CPPUNIT_ASSERT(!s->isListening());


// the following variables count how often certain slots are called:
int server_newConnection = 0;
int server_recv = 0;
int client_connected = 0;
int client_recv = 0;

// This will hold the client connection that the server gets once a client
// connects.
SocketClient::SharedPtr serverSocket;

// the slot lambdas will be called from the serverThread's context.
s->newConnection().connect([&](SocketServer *s)
{
server_newConnection++;

// important: serverSocket needs to survive beyond this lambda,
// otherwise the connection will be closed in its dtor.
// otherwise the connection will be closed on scope exit.
serverSocket = s->nextConnection();

DEFERRED_COMPARE(da, serverSocket->mode(), SocketClient::Unix);
DEFERRED_COMPARE(da, serverSocket->state(), SocketClient::Connected);

// by this point, the client will already have gotten the HUP (hang
// up), so they don't receive this message :(
serverSocket->write("msg frm server");

serverSocket->readyRead().connect(
Expand All @@ -58,6 +61,12 @@ void SocketTestSuite::unixSockets()
DEFERRED_COMPARE(da, b.size(), 15u);
DEFERRED_COMPARE(da, recv, "msg from client");
});

serverSocket->error().connect([&](SocketClient::SharedPtr ptr,
SocketClient::Error e)
{
debug() << "Error on serverSocket " << ptr << ": " << e;
});
});

s->error().connect([&](SocketServer*, SocketServer::Error e)
Expand All @@ -68,7 +77,6 @@ void SocketTestSuite::unixSockets()
});

CPPUNIT_ASSERT(s->listen(unixPathToUse));
CPPUNIT_ASSERT(s->isListening());

std::thread serverThread([&](){loop->exec(300);});

Expand All @@ -79,16 +87,14 @@ void SocketTestSuite::unixSockets()

client->connected().connect([&](SocketClient::SharedPtr c)
{
std::lock_guard<std::mutex> g(m);
debug() << "Client " << c.get()
<< " connected. Send message...";
client_connected++;
c->write("msg from client");
});
client->readyRead().connect([&](SocketClient::SharedPtr, Buffer &&b)
{
debug() << "client recv";
std::lock_guard<std::mutex> g(m);
debug() << "client received something";
client_recv++;
std::string receivedData(b.data(), b.end());
DEFERRED_COMPARE(da, receivedData, "msg from server");
Expand Down

0 comments on commit 1188477

Please sign in to comment.