diff --git a/test/http_server_test.cc b/test/http_server_test.cc index 7b189ea42..166b0a7bb 100644 --- a/test/http_server_test.cc +++ b/test/http_server_test.cc @@ -44,93 +44,93 @@ static void DefaultRequestHandler(evpp::EventLoop* loop, const evpp::http::Conte namespace { - -static int g_listening_port = 49000; +static std::vector g_listening_port = { 49000, 49001 }; static std::string GetHttpServerURL() { + static int i = 0; std::ostringstream oss; - oss << "http://127.0.0.1:" << g_listening_port; + oss << "http://127.0.0.1:" << g_listening_port[(i++ % g_listening_port.size())]; return oss.str(); } -void testDefaultHandler1(evpp::EventLoop* loop) { +void testDefaultHandler1(evpp::EventLoop* loop, int* finished) { std::string uri = "/status?a=1"; std::string url = GetHttpServerURL() + uri; auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { + auto f = [r, finished](const std::shared_ptr& response) { std::string result = response->body().ToString(); H_TEST_ASSERT(!result.empty()); H_TEST_ASSERT(result.find("uri=/status") != std::string::npos); H_TEST_ASSERT(result.find("uri=/status?a=1") == std::string::npos); H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); + *finished += 1; delete r; - g_stopping = true; }; r->Execute(f); } -void testDefaultHandler2(evpp::EventLoop* loop) { +void testDefaultHandler2(evpp::EventLoop* loop, int* finished) { std::string uri = "/status"; std::string body = "The http request body."; std::string url = GetHttpServerURL() + uri; auto r = new evpp::httpc::Request(loop, url, body, evpp::Duration(1.0)); - auto f = [body, r](const std::shared_ptr& response) { + auto f = [body, r, finished](const std::shared_ptr& response) { std::string result = response->body().ToString(); H_TEST_ASSERT(!result.empty()); H_TEST_ASSERT(result.find("uri=/status") != std::string::npos); H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); H_TEST_ASSERT(result.find(body.c_str()) != std::string::npos); + *finished += 1; delete r; - g_stopping = true; }; r->Execute(f); } -void testDefaultHandler3(evpp::EventLoop* loop) { +void testDefaultHandler3(evpp::EventLoop* loop, int* finished) { std::string uri = "/status/method/method2/xx"; std::string url = GetHttpServerURL() + uri; auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { + auto f = [r, finished](const std::shared_ptr& response) { std::string result = response->body().ToString(); H_TEST_ASSERT(!result.empty()); H_TEST_ASSERT(result.find("uri=/status/method/method2/xx") != std::string::npos); H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); + *finished += 1; delete r; - g_stopping = true; }; r->Execute(f); } -void testPushBootHandler(evpp::EventLoop* loop) { +void testPushBootHandler(evpp::EventLoop* loop, int* finished) { std::string uri = "/push/boot"; std::string url = GetHttpServerURL() + uri; auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { + auto f = [r, finished](const std::shared_ptr& response) { std::string result = response->body().ToString(); H_TEST_ASSERT(!result.empty()); H_TEST_ASSERT(result.find("uri=/push/boot") != std::string::npos); H_TEST_ASSERT(result.find("func=RequestHandler") != std::string::npos); + *finished += 1; delete r; - g_stopping = true; }; r->Execute(f); } -void testStop(evpp::EventLoop* loop) { +void testStop(evpp::EventLoop* loop, int* finished) { std::string uri = "/mod/stop"; std::string url = GetHttpServerURL() + uri; auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { + auto f = [r, finished](const std::shared_ptr& response) { std::string result = response->body().ToString(); H_TEST_ASSERT(!result.empty()); H_TEST_ASSERT(result.find("uri=/mod/stop") != std::string::npos); H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); + *finished += 1; delete r; - g_stopping = true; }; r->Execute(f); @@ -139,16 +139,17 @@ void testStop(evpp::EventLoop* loop) { static void TestAll() { evpp::EventLoopThread t; t.Start(true); - testDefaultHandler1(t.event_loop()); - testDefaultHandler2(t.event_loop()); - testDefaultHandler3(t.event_loop()); - testPushBootHandler(t.event_loop()); - testStop(t.event_loop()); + int finished = 0; + testDefaultHandler1(t.event_loop(), &finished); + testDefaultHandler2(t.event_loop(), &finished); + testDefaultHandler3(t.event_loop(), &finished); + testPushBootHandler(t.event_loop(), &finished); + testStop(t.event_loop(), &finished); while (true) { usleep(10); - if (g_stopping) { + if (finished == 5) { break; } } @@ -159,8 +160,8 @@ static void TestAll() { TEST_UNIT(testHTTPServer1) { - for (int i = 0; i < 3; ++i) { - g_stopping = true; + for (int j = 0; j < 1000; j++) + for (int i = 0; i < 10; ++i) { evpp::http::Server ph(i); ph.RegisterDefaultHandler(&DefaultRequestHandler); ph.RegisterHandler("/push/boot", &RequestHandler); diff --git a/test/http_server_test2.cc b/test/http_server_test2.cc deleted file mode 100644 index e423c825c..000000000 --- a/test/http_server_test2.cc +++ /dev/null @@ -1,171 +0,0 @@ -#include - -#include "test_common.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include - -#include "evpp/http/service.h" -#include "evpp/http/context.h" -#include "evpp/http/http_server.h" - -static bool g_stopping = false; -static void RequestHandler(evpp::EventLoop* loop, const evpp::http::ContextPtr& ctx, const evpp::http::HTTPSendResponseCallback& cb) { - std::stringstream oss; - oss << "func=" << __FUNCTION__ << " OK" - << " ip=" << ctx->remote_ip() << "\n" - << " uri=" << ctx->uri() << "\n" - << " body=" << ctx->body().ToString() << "\n"; - cb(oss.str()); -} - -static void DefaultRequestHandler(evpp::EventLoop* loop, const evpp::http::ContextPtr& ctx, const evpp::http::HTTPSendResponseCallback& cb) { - //std::cout << __func__ << " called ...\n"; - std::stringstream oss; - oss << "func=" << __FUNCTION__ << "\n" - << " ip=" << ctx->remote_ip() << "\n" - << " uri=" << ctx->uri() << "\n" - << " body=" << ctx->body().ToString() << "\n"; - - if (ctx->uri().find("stop") != std::string::npos) { - g_stopping = true; - } - - cb(oss.str()); -} - -namespace { - -static std::vector g_listening_port = {49000, 49001}; - -static std::string GetHttpServerURL() { - static int i = 0; - std::ostringstream oss; - oss << "http://127.0.0.1:" << g_listening_port[(i++ % g_listening_port.size())]; - return oss.str(); -} - -void testDefaultHandler1(evpp::EventLoop* loop) { - std::string uri = "/status?a=1"; - std::string url = GetHttpServerURL() + uri; - auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { - std::string result = response->body().ToString(); - H_TEST_ASSERT(!result.empty()); - H_TEST_ASSERT(result.find("uri=/status") != std::string::npos); - H_TEST_ASSERT(result.find("uri=/status?a=1") == std::string::npos); - H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); - delete r; - g_stopping = true; - }; - r->Execute(f); -} - -void testDefaultHandler2(evpp::EventLoop* loop) { - std::string uri = "/status"; - std::string body = "The http request body."; - std::string url = GetHttpServerURL() + uri; - auto r = new evpp::httpc::Request(loop, url, body, evpp::Duration(1.0)); - auto f = [body, r](const std::shared_ptr& response) { - std::string result = response->body().ToString(); - H_TEST_ASSERT(!result.empty()); - H_TEST_ASSERT(result.find("uri=/status") != std::string::npos); - H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); - H_TEST_ASSERT(result.find(body.c_str()) != std::string::npos); - delete r; - g_stopping = true; - }; - - r->Execute(f); -} - -void testDefaultHandler3(evpp::EventLoop* loop) { - std::string uri = "/status/method/method2/xx"; - std::string url = GetHttpServerURL() + uri; - auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { - std::string result = response->body().ToString(); - H_TEST_ASSERT(!result.empty()); - H_TEST_ASSERT(result.find("uri=/status/method/method2/xx") != std::string::npos); - H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); - delete r; - g_stopping = true; - }; - - r->Execute(f); -} - -void testPushBootHandler(evpp::EventLoop* loop) { - std::string uri = "/push/boot"; - std::string url = GetHttpServerURL() + uri; - auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { - std::string result = response->body().ToString(); - H_TEST_ASSERT(!result.empty()); - H_TEST_ASSERT(result.find("uri=/push/boot") != std::string::npos); - H_TEST_ASSERT(result.find("func=RequestHandler") != std::string::npos); - delete r; - g_stopping = true; - }; - - r->Execute(f); -} - -void testStop(evpp::EventLoop* loop) { - std::string uri = "/mod/stop"; - std::string url = GetHttpServerURL() + uri; - auto r = new evpp::httpc::Request(loop, url, "", evpp::Duration(1.0)); - auto f = [r](const std::shared_ptr& response) { - std::string result = response->body().ToString(); - H_TEST_ASSERT(!result.empty()); - H_TEST_ASSERT(result.find("uri=/mod/stop") != std::string::npos); - H_TEST_ASSERT(result.find("func=DefaultRequestHandler") != std::string::npos); - delete r; - g_stopping = true; - }; - - r->Execute(f); -} - -static void TestAll() { - evpp::EventLoopThread t; - t.Start(true); - testDefaultHandler1(t.event_loop()); - testDefaultHandler2(t.event_loop()); - testDefaultHandler3(t.event_loop()); - testPushBootHandler(t.event_loop()); - testStop(t.event_loop()); - - while (true) { - usleep(10); - - if (g_stopping) { - break; - } - } - - t.Stop(true); -} -} - - -TEST_UNIT(testHTTPServer2) { - for (int i = 0; i < 3; ++i) { - g_stopping = true; - evpp::http::Server ph(i); - ph.RegisterDefaultHandler(&DefaultRequestHandler); - ph.RegisterHandler("/push/boot", &RequestHandler); - bool r = ph.Init(g_listening_port) && ph.Start(); - H_TEST_ASSERT(r); - TestAll(); - ph.Stop(true); - } -} diff --git a/vsprojects/libevpp-test.vcxproj b/vsprojects/libevpp-test.vcxproj index b2b3acde0..3f1d5c2a6 100644 --- a/vsprojects/libevpp-test.vcxproj +++ b/vsprojects/libevpp-test.vcxproj @@ -122,10 +122,6 @@ false false - - false - false - false false diff --git a/vsprojects/libevpp-test.vcxproj.filters b/vsprojects/libevpp-test.vcxproj.filters index 5f8d87afa..9186ed6f5 100644 --- a/vsprojects/libevpp-test.vcxproj.filters +++ b/vsprojects/libevpp-test.vcxproj.filters @@ -81,9 +81,6 @@ udp - - http - tcp