Skip to content

Commit

Permalink
Minimized SocketTestSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
Gei0r committed May 8, 2017
1 parent 1188477 commit 5a766d2
Showing 1 changed file with 9 additions and 77 deletions.
86 changes: 9 additions & 77 deletions tests/SocketTestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <rct/SocketServer.h>
#include <rct/Log.h>

#include <iostream>
#include <thread>
#include <sstream>

void SocketTestSuite::unixSockets()
{
Expand All @@ -17,116 +15,50 @@ void SocketTestSuite::unixSockets()
// need to delete the file if it already exists
unixPathToUse.rm();

DeferredAsserter da;

// 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);

// 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;
SocketClient::SharedPtr serverConnection;

s->newConnection().connect([&](SocketServer *s)
{
server_newConnection++;

// important: serverSocket needs to survive beyond this lambda,
// important: serverConnection needs to survive beyond this lambda,
// 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);
serverConnection = s->nextConnection();

// 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(
[&](SocketClient::SharedPtr ptr, Buffer &&b)
{
server_recv++;
debug() << "Server received from socket " << ptr.get()
<< " fd=" << ptr->fd;
std::string recv(b.data(), b.end());
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;
});
// up), so they don't receive the message :(
serverConnection->write("msg frm server");
});

s->error().connect([&](SocketServer*, SocketServer::Error e)
{
std::ostringstream oss;
oss << "Server error: " << e;
da.fail(oss.str());
});

CPPUNIT_ASSERT(s->listen(unixPathToUse));

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

realSleep(100);
debug() << "creating client...";
SocketClient::SharedPtr client(new SocketClient(SocketClient::Unix));
debug() << "client created." << client.get();

client->connected().connect([&](SocketClient::SharedPtr c)
{
debug() << "Client " << c.get()
<< " connected. Send message...";
client_connected++;
c->write("msg from client");
});
int numReadsFromClient = 0;
client->readyRead().connect([&](SocketClient::SharedPtr, Buffer &&b)
{
debug() << "client received something";
client_recv++;
std::string receivedData(b.data(), b.end());
DEFERRED_COMPARE(da, receivedData, "msg from server");
});
client->error().connect([&](SocketClient::SharedPtr ptr,
SocketClient::Error e)
{
debug() << "client " << ptr.get() << " got error " << e;
debug() << "client received " << receivedData;
numReadsFromClient++;
});

client->disconnected().connect([&](SocketClient::SharedPtr ptr)
{
debug() << "Client " << ptr.get() << " disconnected";
});

CPPUNIT_ASSERT(client->state() == SocketClient::Disconnected);

debug() << "client about to connect to " << unixPathToUse << "...";
CPPUNIT_ASSERT(client->connect(unixPathToUse));
debug() << "client connect done";

realSleep(100);
// client->close();

serverThread.join();

CPPUNIT_ASSERT(server_newConnection == 1);
CPPUNIT_ASSERT(client_connected == 1);
CPPUNIT_ASSERT(client_recv == 1);
CPPUNIT_ASSERT(server_recv == 1);
CPPUNIT_ASSERT(da.result());

CPPUNIT_ASSERT(numReadsFromClient == 1);
}

void SocketTestSuite::realSleep(int ms)
Expand Down

0 comments on commit 5a766d2

Please sign in to comment.