Unverified Commit 3f298999 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #404 from dennisjenkins75/ephemeral-port-unit-tests

Ephemeral port unit tests
parents e879b36f ffd44ec3
...@@ -28,8 +28,6 @@ struct TestSet { ...@@ -28,8 +28,6 @@ struct TestSet {
typedef std::vector<TestSet> PayloadTestSets; typedef std::vector<TestSet> PayloadTestSets;
static const uint16_t PORT = 9080;
void testPayloads(Http::Client & client, std::string url, PayloadTestSets & testPayloads) { void testPayloads(Http::Client & client, std::string url, PayloadTestSets & testPayloads) {
// Client tests to make sure the payload is enforced // Client tests to make sure the payload is enforced
std::mutex resultsetMutex; std::mutex resultsetMutex;
...@@ -65,19 +63,10 @@ void handleEcho(const Rest::Request&req, Http::ResponseWriter response) { ...@@ -65,19 +63,10 @@ void handleEcho(const Rest::Request&req, Http::ResponseWriter response) {
TEST(payload, from_description) TEST(payload, from_description)
{ {
Http::Client client; const Address addr(Ipv4::any(), Port(0));
auto client_opts = Http::Client::options()
.threads(3)
.maxConnectionsPerHost(3);
client.init(client_opts);
Address addr(Ipv4::any(), 9080);
const size_t threads = 20; const size_t threads = 20;
const size_t maxPayload = 1024; // very small const size_t maxPayload = 1024; // very small
auto pid = fork();
if ( pid == 0) {
std::shared_ptr<Http::Endpoint> endpoint;
Rest::Description desc("Rest Description Test", "v1"); Rest::Description desc("Rest Description Test", "v1");
Rest::Router router; Rest::Router router;
...@@ -88,33 +77,39 @@ TEST(payload, from_description) ...@@ -88,33 +77,39 @@ TEST(payload, from_description)
router.initFromDescription(desc); router.initFromDescription(desc);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto opts = Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(threads) .threads(threads)
.flags(flags) .flags(flags)
.maxPayload(maxPayload); .maxPayload(maxPayload);
;
endpoint = std::make_shared<Pistache::Http::Endpoint>(addr); auto endpoint = std::make_shared<Pistache::Http::Endpoint>(addr);
endpoint->init(opts); endpoint->init(opts);
endpoint->setHandler(router.handler()); endpoint->setHandler(router.handler());
endpoint->serve(); endpoint->serveThreaded();
endpoint->shutdown();
return;
} Http::Client client;
auto client_opts = Http::Client::options()
.threads(3)
.maxConnectionsPerHost(3);
client.init(client_opts);
// TODO: Remove temp hack once 'serveThreaded()' waits for socket to be
// created before returning.
std::this_thread::sleep_for(std::chrono::milliseconds(150)); std::this_thread::sleep_for(std::chrono::milliseconds(150));
const auto port = endpoint->getPort();
PayloadTestSets payloads{ PayloadTestSets payloads{
{800, Http::Code::Ok} {800, Http::Code::Ok}
, {1024, Http::Code::Request_Entity_Too_Large} , {1024, Http::Code::Request_Entity_Too_Large}
,{2048, Http::Code::Request_Entity_Too_Large} ,{2048, Http::Code::Request_Entity_Too_Large}
}; };
testPayloads(client, "127.0.0.1:" + std::to_string(PORT), payloads); testPayloads(client, "127.0.0.1:" + std::to_string(port), payloads);
kill(pid, SIGTERM);
int r;
waitpid(pid, &r, 0);
client.shutdown(); client.shutdown();
endpoint->shutdown();
} }
TEST(payload, manual_construction) { TEST(payload, manual_construction) {
...@@ -133,33 +128,34 @@ TEST(payload, manual_construction) { ...@@ -133,33 +128,34 @@ TEST(payload, manual_construction) {
tag placeholder; tag placeholder;
}; };
Http::Client client; // General test parameters.
auto client_opts = Http::Client::options() const Address addr(Ipv4::any(), Port(0));
.threads(3) const int threads = 20;
.maxConnectionsPerHost(3); const auto flags = Tcp::Options::ReuseAddr;
client.init(client_opts); const size_t maxPayload = 2048;
Port port(PORT);
Address addr(Ipv4::any(), port);
int threads = 20;
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr;
size_t maxPayload = 2048;
auto pid = fork(); // Build in-process server threads.
if (pid == 0) {
auto endpoint = std::make_shared<Http::Endpoint>(addr); auto endpoint = std::make_shared<Http::Endpoint>(addr);
auto opts = Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(threads) .threads(threads)
.flags(flags) .flags(flags)
.maxPayload(maxPayload); .maxPayload(maxPayload);
;
endpoint->init(opts); endpoint->init(opts);
endpoint->setHandler(Http::make_handler<MyHandler>()); endpoint->setHandler(Http::make_handler<MyHandler>());
endpoint->serve(); endpoint->serveThreaded();
endpoint->shutdown();
return; // TODO: Remove temp hack once 'serveThreaded()' waits for socket to be
} // created before returning.
std::this_thread::sleep_for(std::chrono::milliseconds(150));
const auto port = endpoint->getPort();
// Create http client.
Http::Client client;
auto client_opts = Http::Client::options()
.threads(3)
.maxConnectionsPerHost(3);
client.init(client_opts);
PayloadTestSets payloads{ PayloadTestSets payloads{
{1024, Http::Code::Ok} {1024, Http::Code::Ok}
...@@ -167,11 +163,9 @@ TEST(payload, manual_construction) { ...@@ -167,11 +163,9 @@ TEST(payload, manual_construction) {
, {2048, Http::Code::Request_Entity_Too_Large} , {2048, Http::Code::Request_Entity_Too_Large}
, {4096, Http::Code::Request_Entity_Too_Large} , {4096, Http::Code::Request_Entity_Too_Large}
}; };
testPayloads(client, "127.0.0.1:" + std::to_string(PORT), payloads); testPayloads(client, "127.0.0.1:" + std::to_string(port), payloads);
// Cleanup // Cleanup
kill(pid, SIGTERM);
int r;
waitpid(pid, &r, 0);
client.shutdown(); client.shutdown();
endpoint->shutdown();
} }
...@@ -38,6 +38,10 @@ public: ...@@ -38,6 +38,10 @@ public:
httpEndpoint->shutdown(); httpEndpoint->shutdown();
} }
Port getPort() const {
return httpEndpoint->getPort();
}
private: private:
void setupRoutes() { void setupRoutes() {
using namespace Rest; using namespace Rest;
...@@ -56,20 +60,21 @@ private: ...@@ -56,20 +60,21 @@ private:
}; };
TEST(rest_server_test, basic_test) { TEST(rest_server_test, basic_test) {
Port port(9090);
int thr = 1; int thr = 1;
Address addr(Ipv4::any(), port); Address addr(Ipv4::any(), Port(0));
StatsEndpoint stats(addr); StatsEndpoint stats(addr);
stats.init(thr); stats.init(thr);
stats.start(); stats.start();
Port port = stats.getPort();
cout << "Cores = " << hardware_concurrency() << endl; cout << "Cores = " << hardware_concurrency() << endl;
cout << "Using " << thr << " threads" << endl; cout << "Using " << thr << " threads" << endl;
cout << "Port = " << port << endl;
httplib::Client client("localhost", 9090); httplib::Client client("localhost", port);
auto res = client.Get("/read/function1"); auto res = client.Get("/read/function1");
ASSERT_EQ(res->status, 200); ASSERT_EQ(res->status, 200);
ASSERT_EQ(res->body, "1"); ASSERT_EQ(res->body, "1");
......
...@@ -14,7 +14,6 @@ using namespace Pistache; ...@@ -14,7 +14,6 @@ using namespace Pistache;
static const size_t N_LETTERS = 26; static const size_t N_LETTERS = 26;
static const size_t LETTER_REPEATS = 100000; static const size_t LETTER_REPEATS = 100000;
static const size_t SET_REPEATS = 10; static const size_t SET_REPEATS = 10;
static const uint16_t PORT = 9082;
void dumpData(const Rest::Request&req, Http::ResponseWriter response) { void dumpData(const Rest::Request&req, Http::ResponseWriter response) {
UNUSED(req); UNUSED(req);
...@@ -49,7 +48,7 @@ void dumpData(const Rest::Request&req, Http::ResponseWriter response) { ...@@ -49,7 +48,7 @@ void dumpData(const Rest::Request&req, Http::ResponseWriter response) {
TEST(stream, from_description) TEST(stream, from_description)
{ {
Address addr(Ipv4::any(), PORT); Address addr(Ipv4::any(), Port(0));
const size_t threads = 20; const size_t threads = 20;
std::shared_ptr<Http::Endpoint> endpoint; std::shared_ptr<Http::Endpoint> endpoint;
...@@ -63,8 +62,7 @@ TEST(stream, from_description) ...@@ -63,8 +62,7 @@ TEST(stream, from_description)
router.initFromDescription(desc); router.initFromDescription(desc);
auto flags = Tcp::Options::InstallSignalHandler | Tcp::Options::ReuseAddr; auto flags = Tcp::Options::ReuseAddr;
auto opts = Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(threads) .threads(threads)
.flags(flags) .flags(flags)
...@@ -86,7 +84,8 @@ TEST(stream, from_description) ...@@ -86,7 +84,8 @@ TEST(stream, from_description)
return size * nmemb; return size * nmemb;
}; };
std::string url = "http://localhost:" + std::to_string(PORT) + "/"; const auto port = endpoint->getPort();
std::string url = "http://localhost:" + std::to_string(port) + "/";
CURLcode res = CURLE_FAILED_INIT; CURLcode res = CURLE_FAILED_INIT;
CURL * curl = curl_easy_init(); CURL * curl = curl_easy_init();
if (curl) if (curl)
......
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