Websocket client test with WS=1

This commit is contained in:
Michael Meeks 2017-02-16 14:14:12 +00:00 committed by Jan Holesovsky
parent 2f5d3e4412
commit d9ff82ce83
2 changed files with 46 additions and 22 deletions

View file

@ -25,6 +25,7 @@
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/FilePartSource.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/WebSocket.h>
#include <Poco/Net/KeyConsoleHandler.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/StreamCopier.h>
@ -39,6 +40,7 @@
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::WebSocket;
using Poco::Runnable;
using Poco::Thread;
using Poco::URI;
@ -107,6 +109,15 @@ struct Session
}
return number;
}
std::shared_ptr<WebSocket> getWebSocket()
{
_session->setTimeout(Poco::Timespan(10, 0));
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
HTTPResponse response;
return std::shared_ptr<WebSocket>(
new WebSocket(*_session, request, response));
}
};
struct ThreadWorker : public Runnable
@ -151,25 +162,44 @@ struct Client : public Poco::Util::Application
snakes[i].join();
}
void testWebsocket()
{
Session session("ws");
std::shared_ptr<WebSocket> ws = session.getWebSocket();
for (size_t i = 0; i < 10; i++)
{
ws->sendFrame(&i, sizeof(i), WebSocket::SendFlags::FRAME_BINARY);
size_t back[5];
int flags = 0;
int recvd = ws->receiveFrame((void *)back, sizeof(back), flags);
assert(recvd == sizeof(size_t));
assert(back[0] == i + 1);
}
}
public:
int main(const std::vector<std::string>& /* args */) override
{
Session first("init");
Session second("init");
if (getenv("WS"))
testWebsocket();
else
{
Session first("init");
Session second("init");
int count = 42, back;
first.sendPing(count);
second.sendPing(count + 1);
int count = 42, back;
first.sendPing(count);
second.sendPing(count + 1);
back = first.getResponse();
assert (back == count + 1);
back = first.getResponse();
assert (back == count + 1);
back = second.getResponse();
assert (back == count + 2);
testLadder();
testParallel();
back = second.getResponse();
assert (back == count + 2);
testLadder();
testParallel();
}
return 0;
}
};

View file

@ -210,15 +210,6 @@ void server(SocketPoll& clientPoller)
}
}
/// Poll client sockets and do IO.
void pollAndComm(SocketPoll& poller, std::atomic<bool>& stop)
{
while (!stop)
{
poller.poll(5000);
}
}
int main(int, const char**)
{
// Used to poll client sockets.
@ -227,7 +218,10 @@ int main(int, const char**)
// Start the client polling thread.
Thread threadPoll([&poller](std::atomic<bool>& stop)
{
pollAndComm(poller, stop);
while (!stop)
{
poller.poll(5000);
}
});
// Start the server.