Skip to content

Commit

Permalink
Fix http unit test failed problem. see #15
Browse files Browse the repository at this point in the history
  • Loading branch information
zieckey committed Mar 18, 2017
1 parent e5aa638 commit 7921208
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 204 deletions.
53 changes: 27 additions & 26 deletions test/http_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,93 +44,93 @@ static void DefaultRequestHandler(evpp::EventLoop* loop, const evpp::http::Conte

namespace {


static int g_listening_port = 49000;
static std::vector<int> 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<evpp::httpc::Response>& response) {
auto f = [r, finished](const std::shared_ptr<evpp::httpc::Response>& 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<evpp::httpc::Response>& response) {
auto f = [body, r, finished](const std::shared_ptr<evpp::httpc::Response>& 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<evpp::httpc::Response>& response) {
auto f = [r, finished](const std::shared_ptr<evpp::httpc::Response>& 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<evpp::httpc::Response>& response) {
auto f = [r, finished](const std::shared_ptr<evpp::httpc::Response>& 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<evpp::httpc::Response>& response) {
auto f = [r, finished](const std::shared_ptr<evpp::httpc::Response>& 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);
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand Down
171 changes: 0 additions & 171 deletions test/http_server_test2.cc

This file was deleted.

4 changes: 0 additions & 4 deletions vsprojects/libevpp-test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\test\http_server_test2.cc">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\test\invoke_timer_test.cc">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
Expand Down
3 changes: 0 additions & 3 deletions vsprojects/libevpp-test.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
<ClCompile Include="..\test\udp_server_test.cc">
<Filter>udp</Filter>
</ClCompile>
<ClCompile Include="..\test\http_server_test2.cc">
<Filter>http</Filter>
</ClCompile>
<ClCompile Include="..\test\tcp_server_test.cc">
<Filter>tcp</Filter>
</ClCompile>
Expand Down

0 comments on commit 7921208

Please sign in to comment.