Browse Source

Merge pull request #324 from hyperxor/use_cpp_11_for_delays

Use sleep_for and chrono for delays in tests
yhirose 6 years ago
parent
commit
7f749740f7
1 changed files with 18 additions and 21 deletions
  1. 18 21
      test/test.cc

+ 18 - 21
test/test.cc

@@ -1,7 +1,11 @@
-#include <future>
-#include <gtest/gtest.h>
 #include <httplib.h>
 #include <httplib.h>
 
 
+#include <gtest/gtest.h>
+
+#include <chrono>
+#include <future>
+#include <thread>
+
 #define SERVER_CERT_FILE "./cert.pem"
 #define SERVER_CERT_FILE "./cert.pem"
 #define SERVER_PRIVATE_KEY_FILE "./key.pem"
 #define SERVER_PRIVATE_KEY_FILE "./key.pem"
 #define CA_CERT_FILE "./ca-bundle.crt"
 #define CA_CERT_FILE "./ca-bundle.crt"
@@ -10,13 +14,6 @@
 #define CLIENT_CERT_FILE "./client.cert.pem"
 #define CLIENT_CERT_FILE "./client.cert.pem"
 #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
 #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
 
 
-#ifdef _WIN32
-#include <process.h>
-#define msleep(n) ::Sleep(n)
-#else
-#define msleep(n) ::usleep(n * 1000)
-#endif
-
 using namespace std;
 using namespace std;
 using namespace httplib;
 using namespace httplib;
 
 
@@ -654,7 +651,7 @@ protected:
              })
              })
         .Get("/slow",
         .Get("/slow",
              [&](const Request & /*req*/, Response &res) {
              [&](const Request & /*req*/, Response &res) {
-               msleep(2000);
+               std::this_thread::sleep_for(std::chrono::seconds(2));
                res.set_content("slow", "text/plain");
                res.set_content("slow", "text/plain");
              })
              })
         .Get("/remote_addr",
         .Get("/remote_addr",
@@ -965,7 +962,7 @@ protected:
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
 
 
     while (!svr_.is_running()) {
     while (!svr_.is_running()) {
-      msleep(1);
+      std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
     }
   }
   }
 
 
@@ -1576,7 +1573,7 @@ TEST_F(ServerTest, SlowRequest) {
       std::thread([=]() { auto res = cli_.Get("/slow"); }));
       std::thread([=]() { auto res = cli_.Get("/slow"); }));
   request_threads_.push_back(
   request_threads_.push_back(
       std::thread([=]() { auto res = cli_.Get("/slow"); }));
       std::thread([=]() { auto res = cli_.Get("/slow"); }));
-  msleep(100);
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
 }
 }
 
 
 TEST_F(ServerTest, Put) {
 TEST_F(ServerTest, Put) {
@@ -1921,7 +1918,7 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
 
 
   thread t = thread([&] { svr.listen(HOST, PORT); });
   thread t = thread([&] { svr.listen(HOST, PORT); });
   while (!svr.is_running()) {
   while (!svr.is_running()) {
-    msleep(1);
+    std::this_thread::sleep_for(std::chrono::milliseconds(1));
   }
   }
 
 
   // Only space and horizontal tab are whitespace. Make sure other whitespace-
   // Only space and horizontal tab are whitespace. Make sure other whitespace-
@@ -1951,7 +1948,7 @@ TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
   bool listen_thread_ok = false;
   bool listen_thread_ok = false;
   thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
   thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
   while (!svr.is_running()) {
   while (!svr.is_running()) {
-    msleep(1);
+    std::this_thread::sleep_for(std::chrono::milliseconds(1));
   }
   }
 
 
   // A certain header line causes an exception if the header property is parsed
   // A certain header line causes an exception if the header property is parsed
@@ -2026,7 +2023,7 @@ protected:
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
 
 
     while (!svr_.is_running()) {
     while (!svr_.is_running()) {
-      msleep(1);
+      std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
     }
   }
   }
 
 
@@ -2060,12 +2057,12 @@ protected:
   virtual void SetUp() {
   virtual void SetUp() {
     t_ = thread([&]() {
     t_ = thread([&]() {
       svr_.bind_to_any_port(HOST);
       svr_.bind_to_any_port(HOST);
-      msleep(500);
+      std::this_thread::sleep_for(std::chrono::milliseconds(500));
       ASSERT_TRUE(svr_.listen_after_bind());
       ASSERT_TRUE(svr_.listen_after_bind());
     });
     });
 
 
     while (!svr_.is_running()) {
     while (!svr_.is_running()) {
-      msleep(1);
+      std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
     }
   }
   }
 
 
@@ -2105,7 +2102,7 @@ protected:
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
     t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
 
 
     while (!svr_.is_running()) {
     while (!svr_.is_running()) {
-      msleep(1);
+      std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
     }
   }
   }
 
 
@@ -2200,7 +2197,7 @@ TEST(SSLClientServerTest, ClientCertPresent) {
   });
   });
 
 
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
-  msleep(1);
+  std::this_thread::sleep_for(std::chrono::milliseconds(1));
 
 
   httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
   httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
   auto res = cli.Get("/test");
   auto res = cli.Get("/test");
@@ -2219,7 +2216,7 @@ TEST(SSLClientServerTest, ClientCertMissing) {
   svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
   svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
 
 
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
-  msleep(1);
+  std::this_thread::sleep_for(std::chrono::milliseconds(1));
 
 
   httplib::SSLClient cli(HOST, PORT);
   httplib::SSLClient cli(HOST, PORT);
   auto res = cli.Get("/test");
   auto res = cli.Get("/test");
@@ -2241,7 +2238,7 @@ TEST(SSLClientServerTest, TrustDirOptional) {
   });
   });
 
 
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
   thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
-  msleep(1);
+  std::this_thread::sleep_for(std::chrono::milliseconds(1));
 
 
   httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
   httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
   auto res = cli.Get("/test");
   auto res = cli.Get("/test");