Unverified Commit c05ae942 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #397 from knowledge4igor/use_ephemeral_port_in_several_tests

Use ephemeral ports in several tests
parents 5cf1bb19 4b4c8e34
...@@ -25,7 +25,7 @@ struct CookieHandler : public Http::Handler { ...@@ -25,7 +25,7 @@ struct CookieHandler : public Http::Handler {
}; };
TEST(http_client_test, one_client_with_one_request_with_onecookie) { TEST(http_client_test, one_client_with_one_request_with_onecookie) {
const std::string address = "localhost:9086"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -34,6 +34,9 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) { ...@@ -34,6 +34,9 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) {
server.setHandler(Http::make_handler<CookieHandler>()); server.setHandler(Http::make_handler<CookieHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
client.init(); client.init();
...@@ -41,7 +44,7 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) { ...@@ -41,7 +44,7 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) {
const std::string name = "FOO"; const std::string name = "FOO";
const std::string value = "bar"; const std::string value = "bar";
auto cookie = Http::Cookie(name, value); auto cookie = Http::Cookie(name, value);
auto rb = client.get(address).cookie(cookie); auto rb = client.get(server_address).cookie(cookie);
auto response = rb.send(); auto response = rb.send();
Http::CookieJar cj; Http::CookieJar cj;
...@@ -63,7 +66,7 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) { ...@@ -63,7 +66,7 @@ TEST(http_client_test, one_client_with_one_request_with_onecookie) {
} }
TEST(http_client_test, one_client_with_one_request_with_several_cookies) { TEST(http_client_test, one_client_with_one_request_with_several_cookies) {
const std::string address = "localhost:9088"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -72,6 +75,9 @@ TEST(http_client_test, one_client_with_one_request_with_several_cookies) { ...@@ -72,6 +75,9 @@ TEST(http_client_test, one_client_with_one_request_with_several_cookies) {
server.setHandler(Http::make_handler<CookieHandler>()); server.setHandler(Http::make_handler<CookieHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
client.init(); client.init();
...@@ -85,7 +91,7 @@ TEST(http_client_test, one_client_with_one_request_with_several_cookies) { ...@@ -85,7 +91,7 @@ TEST(http_client_test, one_client_with_one_request_with_several_cookies) {
const std::string name3 = "Key"; const std::string name3 = "Key";
const std::string value3 = "value"; const std::string value3 = "value";
auto cookie3 = Http::Cookie(name3, value3); auto cookie3 = Http::Cookie(name3, value3);
auto rb = client.get(address).cookie(cookie1).cookie(cookie2).cookie(cookie3); auto rb = client.get(server_address).cookie(cookie1).cookie(cookie2).cookie(cookie3);
auto response = rb.send(); auto response = rb.send();
std::unordered_map<std::string, std::string> cookiesStorages; std::unordered_map<std::string, std::string> cookiesStorages;
......
...@@ -32,7 +32,7 @@ struct DelayHandler : public Http::Handler ...@@ -32,7 +32,7 @@ struct DelayHandler : public Http::Handler
TEST(http_client_test, one_client_with_one_request) TEST(http_client_test, one_client_with_one_request)
{ {
const std::string address = "localhost:9100"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -41,10 +41,13 @@ TEST(http_client_test, one_client_with_one_request) ...@@ -41,10 +41,13 @@ TEST(http_client_test, one_client_with_one_request)
server.setHandler(Http::make_handler<HelloHandler>()); server.setHandler(Http::make_handler<HelloHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
client.init(); client.init();
auto rb = client.get(address); auto rb = client.get(server_address);
auto response = rb.send(); auto response = rb.send();
bool done = false; bool done = false;
response.then([&](Http::Response rsp) response.then([&](Http::Response rsp)
...@@ -63,7 +66,7 @@ TEST(http_client_test, one_client_with_one_request) ...@@ -63,7 +66,7 @@ TEST(http_client_test, one_client_with_one_request)
} }
TEST(http_client_test, one_client_with_multiple_requests) { TEST(http_client_test, one_client_with_multiple_requests) {
const std::string address = "localhost:9101"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -72,6 +75,9 @@ TEST(http_client_test, one_client_with_multiple_requests) { ...@@ -72,6 +75,9 @@ TEST(http_client_test, one_client_with_multiple_requests) {
server.setHandler(Http::make_handler<HelloHandler>()); server.setHandler(Http::make_handler<HelloHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
client.init(); client.init();
...@@ -79,7 +85,7 @@ TEST(http_client_test, one_client_with_multiple_requests) { ...@@ -79,7 +85,7 @@ TEST(http_client_test, one_client_with_multiple_requests) {
const int RESPONSE_SIZE = 3; const int RESPONSE_SIZE = 3;
int response_counter = 0; int response_counter = 0;
auto rb = client.get(address); auto rb = client.get(server_address);
for (int i = 0; i < RESPONSE_SIZE; ++i) { for (int i = 0; i < RESPONSE_SIZE; ++i) {
auto response = rb.send(); auto response = rb.send();
response.then([&](Http::Response rsp) { response.then([&](Http::Response rsp) {
...@@ -101,7 +107,7 @@ TEST(http_client_test, one_client_with_multiple_requests) { ...@@ -101,7 +107,7 @@ TEST(http_client_test, one_client_with_multiple_requests) {
} }
TEST(http_client_test, multiple_clients_with_one_request) { TEST(http_client_test, multiple_clients_with_one_request) {
const std::string address = "localhost:9102"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -110,6 +116,9 @@ TEST(http_client_test, multiple_clients_with_one_request) { ...@@ -110,6 +116,9 @@ TEST(http_client_test, multiple_clients_with_one_request) {
server.setHandler(Http::make_handler<HelloHandler>()); server.setHandler(Http::make_handler<HelloHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
const int CLIENT_SIZE = 3; const int CLIENT_SIZE = 3;
Http::Client client1; Http::Client client1;
client1.init(); client1.init();
...@@ -121,21 +130,21 @@ TEST(http_client_test, multiple_clients_with_one_request) { ...@@ -121,21 +130,21 @@ TEST(http_client_test, multiple_clients_with_one_request) {
std::vector<Async::Promise<Http::Response>> responses; std::vector<Async::Promise<Http::Response>> responses;
std::atomic<int> response_counter(0); std::atomic<int> response_counter(0);
auto rb1 = client1.get(address); auto rb1 = client1.get(server_address);
auto response1 = rb1.send(); auto response1 = rb1.send();
response1.then([&](Http::Response rsp) { response1.then([&](Http::Response rsp) {
if (rsp.code() == Http::Code::Ok) if (rsp.code() == Http::Code::Ok)
++response_counter; ++response_counter;
}, Async::IgnoreException); }, Async::IgnoreException);
responses.push_back(std::move(response1)); responses.push_back(std::move(response1));
auto rb2 = client2.get(address); auto rb2 = client2.get(server_address);
auto response2 = rb2.send(); auto response2 = rb2.send();
response2.then([&](Http::Response rsp) { response2.then([&](Http::Response rsp) {
if (rsp.code() == Http::Code::Ok) if (rsp.code() == Http::Code::Ok)
++response_counter; ++response_counter;
}, Async::IgnoreException); }, Async::IgnoreException);
responses.push_back(std::move(response2)); responses.push_back(std::move(response2));
auto rb3 = client3.get(address); auto rb3 = client3.get(server_address);
auto response3 = rb3.send(); auto response3 = rb3.send();
response3.then([&](Http::Response rsp) { response3.then([&](Http::Response rsp) {
if (rsp.code() == Http::Code::Ok) if (rsp.code() == Http::Code::Ok)
...@@ -158,7 +167,7 @@ TEST(http_client_test, multiple_clients_with_one_request) { ...@@ -158,7 +167,7 @@ TEST(http_client_test, multiple_clients_with_one_request) {
TEST(http_client_test, timeout_reject) TEST(http_client_test, timeout_reject)
{ {
const std::string address = "localhost:9103"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -167,10 +176,13 @@ TEST(http_client_test, timeout_reject) ...@@ -167,10 +176,13 @@ TEST(http_client_test, timeout_reject)
server.setHandler(Http::make_handler<DelayHandler>()); server.setHandler(Http::make_handler<DelayHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
client.init(); client.init();
auto rb = client.get(address).timeout(std::chrono::milliseconds(1000)); auto rb = client.get(server_address).timeout(std::chrono::milliseconds(1000));
auto response = rb.send(); auto response = rb.send();
bool is_reject = false; bool is_reject = false;
response.then([&is_reject](Http::Response /*rsp*/) response.then([&is_reject](Http::Response /*rsp*/)
...@@ -193,7 +205,7 @@ TEST(http_client_test, timeout_reject) ...@@ -193,7 +205,7 @@ TEST(http_client_test, timeout_reject)
TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_host) TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_host)
{ {
const std::string address = "localhost:9104"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
...@@ -202,6 +214,9 @@ TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_ ...@@ -202,6 +214,9 @@ TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_
server.setHandler(Http::make_handler<HelloHandler>()); server.setHandler(Http::make_handler<HelloHandler>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
Http::Client client; Http::Client client;
auto opts = Http::Client::options().maxConnectionsPerHost(1).threads(2); auto opts = Http::Client::options().maxConnectionsPerHost(1).threads(2);
client.init(opts); client.init(opts);
...@@ -210,7 +225,7 @@ TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_ ...@@ -210,7 +225,7 @@ TEST(http_client_test, one_client_with_multiple_requests_and_one_connection_per_
const int RESPONSE_SIZE = 6; const int RESPONSE_SIZE = 6;
std::atomic<int> response_counter(0); std::atomic<int> response_counter(0);
auto rb = client.get(address); auto rb = client.get(server_address);
for (int i = 0; i < RESPONSE_SIZE; ++i) for (int i = 0; i < RESPONSE_SIZE; ++i)
{ {
auto response = rb.send(); auto response = rb.send();
......
...@@ -72,9 +72,9 @@ int clientLogicFunc(int response_size, const std::string& server_page, int wait_ ...@@ -72,9 +72,9 @@ int clientLogicFunc(int response_size, const std::string& server_page, int wait_
} }
TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_server) { TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_server) {
const std::string server_address = "localhost:9095"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(server_address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts); server.init(server_opts);
...@@ -82,6 +82,9 @@ TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_serv ...@@ -82,6 +82,9 @@ TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_serv
server.setHandler(Http::make_handler<HelloHandlerWithDelay>(SIX_SECONDS_DELAY)); server.setHandler(Http::make_handler<HelloHandlerWithDelay>(SIX_SECONDS_DELAY));
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
const int CLIENT_REQUEST_SIZE = 1; const int CLIENT_REQUEST_SIZE = 1;
int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, SIX_SECONDS_DELAY); int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, SIX_SECONDS_DELAY);
...@@ -91,9 +94,9 @@ TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_serv ...@@ -91,9 +94,9 @@ TEST(http_server_test, client_disconnection_on_timeout_from_single_threaded_serv
} }
TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_single_threaded_server) { TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_single_threaded_server) {
const std::string server_address = "localhost:9096"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(server_address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
auto server_opts = Http::Endpoint::options().flags(flags); auto server_opts = Http::Endpoint::options().flags(flags);
server.init(server_opts); server.init(server_opts);
...@@ -102,6 +105,9 @@ TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_si ...@@ -102,6 +105,9 @@ TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_si
server.setHandler(Http::make_handler<HelloHandlerWithDelay>(SIX_SECONDS_DELAY)); server.setHandler(Http::make_handler<HelloHandlerWithDelay>(SIX_SECONDS_DELAY));
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
const int CLIENT_REQUEST_SIZE = 3; const int CLIENT_REQUEST_SIZE = 3;
int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, SIX_SECONDS_DELAY); int counter = clientLogicFunc(CLIENT_REQUEST_SIZE, server_address, SIX_SECONDS_DELAY);
...@@ -111,15 +117,18 @@ TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_si ...@@ -111,15 +117,18 @@ TEST(http_server_test, client_multiple_requests_disconnection_on_timeout_from_si
} }
TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server) { TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server) {
const std::string server_address = "localhost:9097"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(server_address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | 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);
server.setHandler(Http::make_handler<HelloHandlerWithDelay>()); server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
const int SIX_SECONDS_TIMOUT = 6; const int SIX_SECONDS_TIMOUT = 6;
const int FIRST_CLIENT_REQUEST_SIZE = 4; const int FIRST_CLIENT_REQUEST_SIZE = 4;
std::future<int> result1(std::async(clientLogicFunc, std::future<int> result1(std::async(clientLogicFunc,
...@@ -142,9 +151,9 @@ TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server) { ...@@ -142,9 +151,9 @@ TEST(http_server_test, multiple_client_with_requests_to_multithreaded_server) {
} }
TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_server) { TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_server) {
const std::string server_address = "localhost:9098"; const Pistache::Address address("localhost", Pistache::Port(0));
Http::Endpoint server(server_address); Http::Endpoint server(address);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::InstallSignalHandler | 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);
...@@ -152,6 +161,9 @@ TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_ ...@@ -152,6 +161,9 @@ TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_
server.setHandler(Http::make_handler<SlowHandlerOnSpecialPage>(SIX_SECONDS_DELAY)); server.setHandler(Http::make_handler<SlowHandlerOnSpecialPage>(SIX_SECONDS_DELAY));
server.serveThreaded(); server.serveThreaded();
const std::string server_address = "localhost:" + server.getPort().toString();
std::cout << "Server address: " << server_address << "\n";
const int FIRST_CLIENT_REQUEST_SIZE = 1; const int FIRST_CLIENT_REQUEST_SIZE = 1;
std::future<int> result1(std::async(clientLogicFunc, std::future<int> result1(std::async(clientLogicFunc,
FIRST_CLIENT_REQUEST_SIZE, FIRST_CLIENT_REQUEST_SIZE,
......
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