loolwsd: cleanup of unittests and helpers

Change-Id: I03a5cb29b51409f3b8cc51981b522248a29a6ba3
Reviewed-on: https://gerrit.libreoffice.org/28955
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
This commit is contained in:
Ashod Nakashian 2016-09-11 14:29:09 -04:00 committed by Ashod Nakashian
parent 67c05ec319
commit d2872846d8

View file

@ -238,8 +238,9 @@ void getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std
} }
inline inline
std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, const std::string& name = "") std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string name = "")
{ {
name = name + '[' + prefix + "] ";
try try
{ {
int flags = 0; int flags = 0;
@ -256,15 +257,16 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
int bytes = ws.receiveFrame(response.data(), response.size(), flags); int bytes = ws.receiveFrame(response.data(), response.size(), flags);
response.resize(bytes >= 0 ? bytes : 0); response.resize(bytes >= 0 ? bytes : 0);
auto message = LOOLProtocol::getAbbreviatedMessage(response); auto message = LOOLProtocol::getAbbreviatedMessage(response);
std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
{ {
if (message.find(prefix) == 0) if (message.find(prefix) == 0)
{ {
std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
return response; return response;
} }
else if (message.find("nextmessage") == 0) else if (message.find("nextmessage") == 0)
{ {
std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
int size = 0; int size = 0;
if (tokens.count() == 2 && if (tokens.count() == 2 &&
@ -274,9 +276,9 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
bytes = ws.receiveFrame(response.data(), response.size(), flags); bytes = ws.receiveFrame(response.data(), response.size(), flags);
response.resize(bytes >= 0 ? bytes : 0); response.resize(bytes >= 0 ? bytes : 0);
message = LOOLProtocol::getAbbreviatedMessage(response); message = LOOLProtocol::getAbbreviatedMessage(response);
std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
if (bytes > 0 && message.find(prefix) == 0) if (bytes > 0 && message.find(prefix) == 0)
{ {
std::cerr << name << "Got " << bytes << " bytes: " << message << std::endl;
return response; return response;
} }
} }
@ -293,11 +295,13 @@ std::vector<char> getResponseMessage(Poco::Net::WebSocket& ws, const std::string
{ {
break; break;
} }
std::cerr << name << "Ignored: " << message << std::endl;
} }
else else
{ {
std::cerr << name << "Timeout waiting for " << prefix << "\n";
--retries; --retries;
std::cerr << name << "Timeout " << retries << std::endl;
} }
} }
while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
@ -467,51 +471,7 @@ void parseDocSize(const std::string& message, const std::string& type,
inline inline
std::vector<char> getTileMessage(Poco::Net::WebSocket& ws, const std::string& name = "") std::vector<char> getTileMessage(Poco::Net::WebSocket& ws, const std::string& name = "")
{ {
int flags = 0; return getResponseMessage(ws, "tile", name);
int retries = 20;
static const Poco::Timespan waitTime(1000000);
std::vector<char> response(READ_BUFFER_SIZE);
// 5 seconds timeout
ws.setReceiveTimeout(5000000);
do
{
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
{
response.resize(READ_BUFFER_SIZE);
int bytes = ws.receiveFrame(response.data(), response.size(), flags);
response.resize(bytes >= 0 ? bytes : 0);
auto message = LOOLProtocol::getAbbreviatedMessage(response);
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
{
if (message.find("nextmessage") == 0)
{
Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
int size = 0;
if (tokens.count() == 2 &&
tokens[0] == "nextmessage:" && LOOLProtocol::getTokenInteger(tokens[1], "size", size) && size > 0)
{
std::cerr << name << " Got " << message << std::endl;
response.resize(size);
bytes = ws.receiveFrame(response.data(), response.size(), flags);
response.resize(bytes >= 0 ? bytes : 0);
return response;
}
}
}
std::cerr << name << "ignored " << message << std::endl;
retries = 10;
}
else
{
std::cerr << name << "Timeout waiting for nextmessage:\n";
--retries;
}
}
while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
return std::vector<char>();
} }
inline inline