Skip to content

Commit 9dc4e23

Browse files
committed
Unit test for yhirose#52
1 parent 3c71108 commit 9dc4e23

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

httplib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class Server {
205205
bool is_running() const;
206206
void stop();
207207

208+
bool is_handling_requests() const;
209+
208210
protected:
209211
bool process_request(Stream& strm, bool last_connection);
210212

@@ -1482,6 +1484,11 @@ inline void Server::stop()
14821484
svr_sock_ = -1;
14831485
}
14841486

1487+
inline bool Server::is_handling_requests() const
1488+
{
1489+
return running_threads_ > 0;
1490+
}
1491+
14851492
inline bool Server::parse_request_line(const char* s, Request& req)
14861493
{
14871494
static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? (HTTP/1\\.[01])\r\n");

test/test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ class ServerTest : public ::testing::Test {
262262
svr_.get("/hi", [&](const Request& /*req*/, Response& res) {
263263
res.set_content("Hello World!", "text/plain");
264264
})
265+
.get("/slow", [&](const Request& /*req*/, Response& res) {
266+
msleep(3000);
267+
res.set_content("slow", "text/plain");
268+
})
265269
.get("/remote_addr", [&](const Request& req, Response& res) {
266270
auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
267271
res.set_content(remote_addr.c_str(), "text/plain");
@@ -358,6 +362,7 @@ class ServerTest : public ::testing::Test {
358362
virtual void TearDown() {
359363
svr_.stop();
360364
t_.join();
365+
EXPECT_EQ(false, svr_.is_handling_requests());
361366
}
362367

363368
map<string, string> persons_;
@@ -664,6 +669,14 @@ TEST_F(ServerTest, GetMethodRemoteAddr)
664669
EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
665670
}
666671

672+
TEST_F(ServerTest, SlowRequest)
673+
{
674+
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
675+
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
676+
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
677+
msleep(1000);
678+
}
679+
667680
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
668681
TEST_F(ServerTest, Gzip)
669682
{

0 commit comments

Comments
 (0)