Unverified Commit 5b8ec5d5 authored by Kip's avatar Kip Committed by GitHub

Merge pull request #902 from hyperxor/thread_safe_logger_to_http_server_test

Add thread-safe logger to http server test
parents 7fc948a3 facb1d07
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <fstream> #include <fstream>
#include <future> #include <future>
#include <mutex> #include <mutex>
#include <sstream>
#include <string> #include <string>
#include <thread> #include <thread>
...@@ -19,10 +20,56 @@ ...@@ -19,10 +20,56 @@
using namespace Pistache; using namespace Pistache;
#define THREAD_INFO \ namespace
"[" << std::hex << std::this_thread::get_id() << std::dec << "]" {
#define SERVER_PREFIX "[server] " THREAD_INFO
#define CLIENT_PREFIX "[client] " THREAD_INFO class SimpleLogger
{
public:
static SimpleLogger& instance()
{
static SimpleLogger logger;
return logger;
}
void log(const std::string& message)
{
std::lock_guard<std::mutex> guard { m_coutLock };
std::cout << message << std::endl;
}
private:
SimpleLogger() = default;
std::mutex m_coutLock;
};
// from
// https://stackoverflow.com/questions/9667963/can-i-rewrite-a-logging-macro-with-stream-operators-to-use-a-c-template-functi
class ScopedLogger
{
public:
ScopedLogger(const std::string& prefix)
{
m_stream << "[" << prefix << "] [" << std::hex << std::this_thread::get_id() << "] " << std::dec;
}
~ScopedLogger()
{
SimpleLogger::instance().log(m_stream.str());
}
std::stringstream& stream()
{
return m_stream;
}
private:
std::stringstream m_stream;
};
#define LOGGER(prefix, message) ScopedLogger(prefix).stream() << message;
}
struct HelloHandlerWithDelay : public Http::Handler struct HelloHandlerWithDelay : public Http::Handler
{ {
...@@ -31,8 +78,7 @@ struct HelloHandlerWithDelay : public Http::Handler ...@@ -31,8 +78,7 @@ struct HelloHandlerWithDelay : public Http::Handler
explicit HelloHandlerWithDelay(int delay = 0) explicit HelloHandlerWithDelay(int delay = 0)
: delay_(delay) : delay_(delay)
{ {
std::cout << SERVER_PREFIX << " Init Hello handler with " << delay_ LOGGER("server", "Init Hello handler with " << delay_ << " seconds delay");
<< " seconds delay\n";
} }
void onRequest(const Http::Request& /*request*/, void onRequest(const Http::Request& /*request*/,
...@@ -70,7 +116,7 @@ struct HandlerWithSlowPage : public Http::Handler ...@@ -70,7 +116,7 @@ struct HandlerWithSlowPage : public Http::Handler
} }
writer.send(Http::Code::Ok, message); writer.send(Http::Code::Ok, message);
std::cout << SERVER_PREFIX << " Sent: " << message; LOGGER("server", "Sent: " << message);
} }
int delay_; int delay_;
...@@ -90,8 +136,7 @@ struct FileHandler : public Http::Handler ...@@ -90,8 +136,7 @@ struct FileHandler : public Http::Handler
Http::serveFile(writer, fileName_) Http::serveFile(writer, fileName_)
.then( .then(
[this](ssize_t bytes) { [this](ssize_t bytes) {
std::cout << SERVER_PREFIX << " Sent " << bytes << " bytes from " LOGGER("server", "Sent " << bytes << " bytes from " << fileName_ << " file");
<< fileName_ << " file" << std::endl;
}, },
Async::IgnoreException); Async::IgnoreException);
} }
...@@ -111,7 +156,7 @@ struct AddressEchoHandler : public Http::Handler ...@@ -111,7 +156,7 @@ struct AddressEchoHandler : public Http::Handler
{ {
std::string requestAddress = request.address().host(); std::string requestAddress = request.address().host();
writer.send(Http::Code::Ok, requestAddress); writer.send(Http::Code::Ok, requestAddress);
std::cout << SERVER_PREFIX << " Sent: " << requestAddress << std::endl; LOGGER("server", "Sent: " << requestAddress);
} }
}; };
...@@ -150,8 +195,7 @@ int clientLogicFunc(int response_size, const std::string& server_page, ...@@ -150,8 +195,7 @@ int clientLogicFunc(int response_size, const std::string& server_page,
auto response = rb.send(); auto response = rb.send();
response.then( response.then(
[&resolver_counter](Http::Response resp) { [&resolver_counter](Http::Response resp) {
std::cout << CLIENT_PREFIX << " Response code is " << resp.code() LOGGER("client", "Response code is " << resp.code());
<< std::endl;
if (resp.code() == Http::Code::Ok) if (resp.code() == Http::Code::Ok)
{ {
++resolver_counter; ++resolver_counter;
...@@ -159,7 +203,7 @@ int clientLogicFunc(int response_size, const std::string& server_page, ...@@ -159,7 +203,7 @@ int clientLogicFunc(int response_size, const std::string& server_page,
}, },
[&reject_counter](std::exception_ptr exc) { [&reject_counter](std::exception_ptr exc) {
PrintException excPrinter; PrintException excPrinter;
std::cout << CLIENT_PREFIX << " Reject with reason: "; LOGGER("client", "Reject with reason:");
excPrinter(exc); excPrinter(exc);
++reject_counter; ++reject_counter;
}); });
...@@ -172,11 +216,8 @@ int clientLogicFunc(int response_size, const std::string& server_page, ...@@ -172,11 +216,8 @@ int clientLogicFunc(int response_size, const std::string& server_page,
client.shutdown(); client.shutdown();
std::cout << CLIENT_PREFIX << " resolves: " << resolver_counter LOGGER("client", " resolves: " << resolver_counter << ", rejects: " << reject_counter << ", timeout: " << timeout_seconds << " seconds"
<< ", rejects: " << reject_counter << ", wait: " << wait_seconds << " seconds");
<< ", timeout: " << timeout_seconds << " seconds"
<< ", wait: " << wait_seconds << " seconds"
<< "\n";
return resolver_counter; return resolver_counter;
} }
...@@ -191,7 +232,7 @@ TEST(http_server_test, ...@@ -191,7 +232,7 @@ TEST(http_server_test,
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts); server.init(server_opts);
std::cout << "Trying to run server...\n"; LOGGER("test", "Trying to run server...");
const int ONE_SECOND_TIMEOUT = 1; const int ONE_SECOND_TIMEOUT = 1;
const int SIX_SECONDS_DELAY = 6; const int SIX_SECONDS_DELAY = 6;
server.setHandler( server.setHandler(
...@@ -199,7 +240,7 @@ TEST(http_server_test, ...@@ -199,7 +240,7 @@ TEST(http_server_test,
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
const int CLIENT_REQUEST_SIZE = 1; const int CLIENT_REQUEST_SIZE = 1;
int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address,
...@@ -221,7 +262,7 @@ TEST( ...@@ -221,7 +262,7 @@ TEST(
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts); server.init(server_opts);
std::cout << "Trying to run server...\n"; LOGGER("test", "Trying to run server...");
const int ONE_SECOND_TIMEOUT = 1; const int ONE_SECOND_TIMEOUT = 1;
const int SIX_SECONDS_DELAY = 6; const int SIX_SECONDS_DELAY = 6;
server.setHandler( server.setHandler(
...@@ -229,7 +270,7 @@ TEST( ...@@ -229,7 +270,7 @@ TEST(
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
const int CLIENT_REQUEST_SIZE = 3; const int CLIENT_REQUEST_SIZE = 3;
int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address,
...@@ -248,12 +289,12 @@ TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server) ...@@ -248,12 +289,12 @@ TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server)
auto flags = Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags).threads(3); auto server_opts = Http::Endpoint::options().flags(flags).threads(3);
server.init(server_opts); server.init(server_opts);
std::cout << "Trying to run server...\n"; LOGGER("test", "Trying to run server...");
server.setHandler(Http::make_handler<HelloHandlerWithDelay>()); server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
ASSERT_NO_THROW(server.serveThreaded()); ASSERT_NO_THROW(server.serveThreaded());
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server is running: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
const int NO_TIMEOUT = 0; const int NO_TIMEOUT = 0;
const int SIX_SECONDS_TIMOUT = 6; const int SIX_SECONDS_TIMOUT = 6;
...@@ -289,7 +330,7 @@ TEST(http_server_test, ...@@ -289,7 +330,7 @@ TEST(http_server_test,
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
const int FIRST_CLIENT_REQUEST_SIZE = 1; const int FIRST_CLIENT_REQUEST_SIZE = 1;
const int FIRST_CLIENT_TIMEOUT = SIX_SECONDS_DELAY / 2; const int FIRST_CLIENT_TIMEOUT = SIX_SECONDS_DELAY / 2;
...@@ -326,7 +367,7 @@ TEST(http_server_test, server_with_static_file) ...@@ -326,7 +367,7 @@ TEST(http_server_test, server_with_static_file)
{ {
std::cerr << "No suitable filename can be generated!" << std::endl; std::cerr << "No suitable filename can be generated!" << std::endl;
} }
std::cout << "Creating temporary file: " << fileName << std::endl; LOGGER("test", "Creating temporary file: " << fileName);
std::ofstream tmpFile; std::ofstream tmpFile;
tmpFile.open(fileName); tmpFile.open(fileName);
...@@ -343,7 +384,7 @@ TEST(http_server_test, server_with_static_file) ...@@ -343,7 +384,7 @@ TEST(http_server_test, server_with_static_file)
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
Http::Client client; Http::Client client;
client.init(); client.init();
...@@ -367,7 +408,7 @@ TEST(http_server_test, server_with_static_file) ...@@ -367,7 +408,7 @@ TEST(http_server_test, server_with_static_file)
client.shutdown(); client.shutdown();
server.shutdown(); server.shutdown();
std::cout << "Deleting file " << fileName << std::endl; LOGGER("test", "Deleting file " << fileName);
std::remove(fileName); std::remove(fileName);
ASSERT_EQ(data, resultData); ASSERT_EQ(data, resultData);
...@@ -385,7 +426,7 @@ TEST(http_server_test, server_request_copies_address) ...@@ -385,7 +426,7 @@ TEST(http_server_test, server_request_copies_address)
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
Http::Client client; Http::Client client;
client.init(); client.init();
...@@ -394,8 +435,7 @@ TEST(http_server_test, server_request_copies_address) ...@@ -394,8 +435,7 @@ TEST(http_server_test, server_request_copies_address)
std::string resultData; std::string resultData;
response.then( response.then(
[&resultData](Http::Response resp) { [&resultData](Http::Response resp) {
std::cout << CLIENT_PREFIX << " Response code is " << resp.code() LOGGER("client", " Response code is " << resp.code());
<< std::endl;
if (resp.code() == Http::Code::Ok) if (resp.code() == Http::Code::Ok)
{ {
resultData = resp.body(); resultData = resp.body();
...@@ -427,7 +467,7 @@ struct ResponseSizeHandler : public Http::Handler ...@@ -427,7 +467,7 @@ struct ResponseSizeHandler : public Http::Handler
{ {
std::string requestAddress = request.address().host(); std::string requestAddress = request.address().host();
writer.send(Http::Code::Ok, requestAddress); writer.send(Http::Code::Ok, requestAddress);
std::cout << SERVER_PREFIX << " Sent: " << requestAddress << std::endl; LOGGER("server", "Sent: " << requestAddress);
rsize_ = writer.getResponseSize(); rsize_ = writer.getResponseSize();
rcode_ = writer.getResponseCode(); rcode_ = writer.getResponseCode();
} }
...@@ -451,7 +491,7 @@ TEST(http_server_test, response_size_captured) ...@@ -451,7 +491,7 @@ TEST(http_server_test, response_size_captured)
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString(); const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n"; LOGGER("test", "Server address: " << server_address);
// Use the built-in http client, but this test is interested in testing // Use the built-in http client, but this test is interested in testing
// that the ResponseWriter in the server stashed the correct size and code // that the ResponseWriter in the server stashed the correct size and code
...@@ -463,8 +503,7 @@ TEST(http_server_test, response_size_captured) ...@@ -463,8 +503,7 @@ TEST(http_server_test, response_size_captured)
std::string resultData; std::string resultData;
response.then( response.then(
[&resultData](Http::Response resp) { [&resultData](Http::Response resp) {
std::cout << CLIENT_PREFIX << " Response code is " << resp.code() LOGGER("client", "Response code is " << resp.code());
<< std::endl;
if (resp.code() == Http::Code::Ok) if (resp.code() == Http::Code::Ok)
{ {
resultData = resp.body(); resultData = resp.body();
...@@ -482,7 +521,7 @@ TEST(http_server_test, response_size_captured) ...@@ -482,7 +521,7 @@ TEST(http_server_test, response_size_captured)
// Sanity check (stolen from AddressEchoHandler test). // Sanity check (stolen from AddressEchoHandler test).
ASSERT_EQ("127.0.0.1", resultData); ASSERT_EQ("127.0.0.1", resultData);
std::cout << CLIENT_PREFIX << " Response size is " << rsize << "\n"; LOGGER("test", "Response size is " << rsize);
ASSERT_GT(rsize, 1u); ASSERT_GT(rsize, 1u);
ASSERT_LT(rsize, 300u); ASSERT_LT(rsize, 300u);
ASSERT_EQ(rcode, Http::Code::Ok); ASSERT_EQ(rcode, Http::Code::Ok);
...@@ -505,7 +544,7 @@ TEST(http_server_test, client_request_header_timeout_raises_http_408) ...@@ -505,7 +544,7 @@ TEST(http_server_test, client_request_header_timeout_raises_http_408)
auto port = server.getPort(); auto port = server.getPort();
auto addr = "localhost:" + port.toString(); auto addr = "localhost:" + port.toString();
std::cout << "Server address: " << addr << "\n"; LOGGER("test", "Server address: " << addr)
char recvBuf[1024]; char recvBuf[1024];
std::memset(recvBuf, 0, sizeof(recvBuf)); std::memset(recvBuf, 0, sizeof(recvBuf));
...@@ -539,7 +578,7 @@ TEST(http_server_test, client_request_body_timeout_raises_http_408) ...@@ -539,7 +578,7 @@ TEST(http_server_test, client_request_body_timeout_raises_http_408)
auto port = server.getPort(); auto port = server.getPort();
auto addr = "localhost:" + port.toString(); auto addr = "localhost:" + port.toString();
std::cout << "Server address: " << addr << "\n"; LOGGER("test", "Server address: " << addr);
std::string reqStr = "GET /ping HTTP/1.1\r\n"; std::string reqStr = "GET /ping HTTP/1.1\r\n";
std::string headerStr = "Host: localhost\r\nUser-Agent: test\r\n"; std::string headerStr = "Host: localhost\r\nUser-Agent: test\r\n";
...@@ -612,14 +651,12 @@ namespace ...@@ -612,14 +651,12 @@ namespace
} }
std::string requestAddress = request.address().host(); std::string requestAddress = request.address().host();
writer.send(Http::Code::Ok, requestAddress); writer.send(Http::Code::Ok, requestAddress);
std::cout << SERVER_PREFIX << " Sent `" << requestAddress << "` to " LOGGER("server", "Sent `" << requestAddress << "` to " << *peer);
<< *peer << std::endl;
} }
void onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) override void onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) override
{ {
std::cout << SERVER_PREFIX << " Disconnect from " << *peer LOGGER("server", "Disconnect from " << *peer);
<< std::endl;
activeConnections.erase(peer->getID()); activeConnections.erase(peer->getID());
waitHelper->increment(); waitHelper->increment();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment