Unverified Commit fdcf5274 authored by Igor [hyperxor]'s avatar Igor [hyperxor] Committed by GitHub

Fix issue #842 (#845)

* Fix issue #842

* Add one more dot

* Follow review suggestions

* clang format + small change
parent a3c5c68e
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <chrono> #include <chrono>
#include <condition_variable>
#include <fstream> #include <fstream>
#include <future> #include <future>
#include <mutex>
#include <string> #include <string>
using namespace Pistache; using namespace Pistache;
...@@ -416,18 +418,44 @@ TEST(http_server_test, response_size_captured) { ...@@ -416,18 +418,44 @@ TEST(http_server_test, response_size_captured) {
ASSERT_EQ(rcode, Http::Code::Ok); ASSERT_EQ(rcode, Http::Code::Ok);
} }
namespace {
class WaitHelper {
public:
void increment() {
std::lock_guard<std::mutex> lock(counterLock_);
++counter_;
cv_.notify_one();
}
template <typename Duration>
bool wait(const size_t count, const Duration timeout) {
std::unique_lock<std::mutex> lock(counterLock_);
return cv_.wait_for(lock, timeout,
[this, count]() { return counter_ >= count; });
}
private:
size_t counter_ = 0;
std::mutex counterLock_;
std::condition_variable cv_;
};
struct ClientCountingHandler : public Http::Handler { struct ClientCountingHandler : public Http::Handler {
HTTP_PROTOTYPE(ClientCountingHandler) HTTP_PROTOTYPE(ClientCountingHandler)
ClientCountingHandler(std::atomic<size_t> & counter) : counter_(counter) { std::cout << "INITING" << std::endl;} explicit ClientCountingHandler(std::shared_ptr<WaitHelper> waitHelper)
: waitHelper(waitHelper) {
std::cout << "[server] Ininting..." << std::endl;
}
void onRequest(const Http::Request &request, void onRequest(const Http::Request &request,
Http::ResponseWriter writer) override { Http::ResponseWriter writer) override {
auto peer = writer.getPeer(); auto peer = writer.getPeer();
if (peer) { if (peer) {
activeConnections.insert(peer->getID()); activeConnections.insert(peer->getID());
} else { } else {
return; return;
} }
std::string requestAddress = request.address().host(); std::string requestAddress = request.address().host();
writer.send(Http::Code::Ok, requestAddress); writer.send(Http::Code::Ok, requestAddress);
...@@ -435,21 +463,20 @@ struct ClientCountingHandler : public Http::Handler { ...@@ -435,21 +463,20 @@ struct ClientCountingHandler : public Http::Handler {
} }
void onDisconnection(const std::shared_ptr<Tcp::Peer> &peer) override { void onDisconnection(const std::shared_ptr<Tcp::Peer> &peer) override {
++counter_; std::cout << "[server] Disconnect from peer ID " << peer->getID()
std::cout << "[server] Disconnect from peer ID " << peer->getID() << " connecting from " << peer->address().host() << "; counter now at " << counter_ << std::endl; << " connecting from " << peer->address().host() << std::endl;
activeConnections.erase(peer->getID()); activeConnections.erase(peer->getID());
waitHelper->increment();
} }
size_t getClientsServed() const { return counter_; } private:
std::unordered_set<size_t> activeConnections; std::unordered_set<size_t> activeConnections;
std::shared_ptr<WaitHelper> waitHelper;
std::atomic<size_t> & counter_;
}; };
TEST( } // namespace
http_server_test,
client_multiple_requests_disconnects_handled) { TEST(http_server_test, client_multiple_requests_disconnects_handled) {
const Pistache::Address address("localhost", Pistache::Port(0)); const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
...@@ -458,8 +485,8 @@ TEST( ...@@ -458,8 +485,8 @@ TEST(
server.init(server_opts); server.init(server_opts);
std::cout << "Trying to run server...\n"; std::cout << "Trying to run server...\n";
std::atomic<size_t> counter{0}; auto waitHelper = std::make_shared<WaitHelper>();
auto handler = Http::make_handler<ClientCountingHandler>(counter); auto handler = Http::make_handler<ClientCountingHandler>(waitHelper);
server.setHandler(handler); server.setHandler(handler);
server.serveThreaded(); server.serveThreaded();
...@@ -469,9 +496,9 @@ TEST( ...@@ -469,9 +496,9 @@ TEST(
const size_t CLIENT_REQUEST_SIZE = 3; const size_t CLIENT_REQUEST_SIZE = 3;
clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, 1, 6); clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, 1, 6);
const bool result =
waitHelper->wait(CLIENT_REQUEST_SIZE, std::chrono::seconds(2));
server.shutdown(); server.shutdown();
std::this_thread::sleep_for(std::chrono::seconds(1));
ASSERT_EQ(counter, CLIENT_REQUEST_SIZE); ASSERT_EQ(result, true);
ASSERT_EQ(handler->activeConnections.size(), 0UL);
} }
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