1
0
mirror of synced 2025-04-20 11:47:43 +03:00

Minor fixes on test cases (#1709)

* Fix data race

* Replace sleep_for() to wait_until_ready()
This commit is contained in:
Jiwoo Park 2023-11-12 11:28:50 +09:00 committed by GitHub
parent 1d14e051a5
commit f1431311a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1088,12 +1088,8 @@ TEST(RedirectToDifferentPort, Redirect) {
ASSERT_FALSE(svr1.is_running()); ASSERT_FALSE(svr1.is_running());
}); });
while (!svr1.is_running() || !svr2.is_running()) { svr1.wait_until_ready();
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr2.wait_until_ready();
}
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", svr2_port); Client cli("localhost", svr2_port);
cli.set_follow_location(true); cli.set_follow_location(true);
@ -1125,9 +1121,6 @@ TEST(RedirectFromPageWithContent, Redirect) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli("localhost", PORT); Client cli("localhost", PORT);
cli.set_follow_location(true); cli.set_follow_location(true);
@ -1193,9 +1186,6 @@ TEST(RedirectFromPageWithContentIP6, Redirect) {
ASSERT_LT(milliseconds, 5000U); ASSERT_LT(milliseconds, 5000U);
} }
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli("http://[::1]:1234"); Client cli("http://[::1]:1234");
cli.set_follow_location(true); cli.set_follow_location(true);
@ -1246,8 +1236,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1277,8 +1266,7 @@ TEST(BindServerTest, DISABLED_BindDualStack) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli("127.0.0.1", PORT); Client cli("127.0.0.1", PORT);
@ -1349,8 +1337,7 @@ TEST(ErrorHandlerTest, ContentLength) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1391,8 +1378,7 @@ TEST(ExceptionHandlerTest, ContentLength) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
for (size_t i = 0; i < 10; i++) { for (size_t i = 0; i < 10; i++) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1432,8 +1418,7 @@ TEST(NoContentTest, ContentLength) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1483,8 +1468,7 @@ TEST(RoutingHandlerTest, PreRoutingHandler) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@ -1552,8 +1536,7 @@ TEST(InvalidFormatTest, StatusCode) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
// Give GET time to get a few messages. svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1577,7 +1560,7 @@ TEST(URLFragmentTest, WithFragment) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::seconds(1)); svr.wait_until_ready();
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -1613,7 +1596,7 @@ TEST(HeaderWriter, SetHeaderWriter) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::seconds(1)); svr.wait_until_ready();
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -2205,9 +2188,7 @@ protected:
t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
while (!svr_.is_running()) { svr_.wait_until_ready();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} }
virtual void TearDown() { virtual void TearDown() {
@ -4222,9 +4203,6 @@ TEST(MountTest, Unmount) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
svr.set_mount_point("/mount2", "./www2"); svr.set_mount_point("/mount2", "./www2");
@ -4275,9 +4253,6 @@ TEST(ExceptionTest, ThrowExceptionInHandler) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
{ {
@ -4319,9 +4294,6 @@ TEST(KeepAliveTest, ReadTimeout) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
cli.set_keep_alive(true); cli.set_keep_alive(true);
cli.set_read_timeout(std::chrono::seconds(1)); cli.set_read_timeout(std::chrono::seconds(1));
@ -4351,7 +4323,7 @@ TEST(KeepAliveTest, Issue1041) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(200)); svr.wait_until_ready();
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_keep_alive(true); cli.set_keep_alive(true);
@ -4384,7 +4356,7 @@ TEST(KeepAliveTest, SSLClientReconnection) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(200)); svr.wait_until_ready();
SSLClient cli(HOST, PORT); SSLClient cli(HOST, PORT);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -4447,9 +4419,6 @@ TEST(ClientProblemDetectionTest, ContentProvider) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
{ {
@ -4487,9 +4456,6 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
auto res = cli.Get("/"); auto res = cli.Get("/");
@ -4533,7 +4499,6 @@ TEST(GetWithParametersTest, GetWithParameters) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -4592,7 +4557,6 @@ TEST(GetWithParametersTest, GetWithParameters2) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
@ -4660,7 +4624,6 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT); Client cli("localhost", PORT);
@ -4695,9 +4658,6 @@ TEST(KeepAliveTest, ReadTimeoutSSL) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
SSLClient cli("localhost", PORT); SSLClient cli("localhost", PORT);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
cli.set_keep_alive(true); cli.set_keep_alive(true);
@ -4736,9 +4696,7 @@ protected:
t_ = thread( t_ = thread(
[&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); }); [&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); });
while (!svr_.is_running()) { svr_.wait_until_ready();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} }
virtual void TearDown() { virtual void TearDown() {
@ -4775,9 +4733,7 @@ protected:
ASSERT_TRUE(svr_.listen_after_bind()); ASSERT_TRUE(svr_.listen_after_bind());
}); });
while (!svr_.is_running()) { svr_.wait_until_ready();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} }
virtual void TearDown() { virtual void TearDown() {
@ -4818,9 +4774,7 @@ protected:
t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
while (!svr_.is_running()) { svr_.wait_until_ready();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
} }
virtual void TearDown() { virtual void TearDown() {
@ -4937,7 +4891,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli("127.0.0.1", PORT); SSLClient cli("127.0.0.1", PORT);
cli.set_ca_cert_path(SERVER_CERT2_FILE); cli.set_ca_cert_path(SERVER_CERT2_FILE);
@ -5053,7 +5007,7 @@ TEST(SSLClientServerTest, ClientCertPresent) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -5128,7 +5082,7 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT, client_cert, client_private_key); SSLClient cli(HOST, PORT, client_cert, client_private_key);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -5159,7 +5113,7 @@ TEST(SSLClientServerTest, ClientCertMissing) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT); SSLClient cli(HOST, PORT);
auto res = cli.Get("/test"); auto res = cli.Get("/test");
@ -5183,7 +5137,7 @@ TEST(SSLClientServerTest, TrustDirOptional) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -5204,12 +5158,12 @@ TEST(SSLClientServerTest, SSLConnectTimeout) {
client_ca_cert_dir_path), client_ca_cert_dir_path),
stop_(false) {} stop_(false) {}
bool stop_; std::atomic_bool stop_;
private: private:
bool process_and_close_socket(socket_t /*sock*/) override { bool process_and_close_socket(socket_t /*sock*/) override {
// Don't create SSL context // Don't create SSL context
while (!stop_) { while (!stop_.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
return true; return true;
@ -5231,7 +5185,7 @@ TEST(SSLClientServerTest, SSLConnectTimeout) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -5303,7 +5257,7 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
cli.enable_server_certificate_verification(false); cli.enable_server_certificate_verification(false);
@ -5454,9 +5408,6 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) {
svr.wait_until_ready(); svr.wait_until_ready();
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli(HOST, PORT); Client cli(HOST, PORT);
auto res = cli.Get("/foo"); auto res = cli.Get("/foo");
ASSERT_TRUE(res); ASSERT_TRUE(res);
@ -5591,7 +5542,8 @@ TEST(HttpToHttpsRedirectTest, CertFile) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::milliseconds(1)); svr.wait_until_ready();
ssl_svr.wait_until_ready();
Client cli("127.0.0.1", PORT); Client cli("127.0.0.1", PORT);
cli.set_ca_cert_path(SERVER_CERT2_FILE); cli.set_ca_cert_path(SERVER_CERT2_FILE);
@ -5647,7 +5599,6 @@ TEST(MultipartFormDataTest, LargeData) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
std::string data(1024 * 1024 * 2, '.'); std::string data(1024 * 1024 * 2, '.');
@ -5795,7 +5746,6 @@ TEST(MultipartFormDataTest, DataProviderItems) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
Client cli("https://localhost:8080"); Client cli("https://localhost:8080");
@ -5997,7 +5947,6 @@ TEST(MultipartFormDataTest, PostCustomBoundary) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
std::string data(1024 * 1024 * 2, '.'); std::string data(1024 * 1024 * 2, '.');
@ -6019,9 +5968,6 @@ TEST(MultipartFormDataTest, PostCustomBoundary) {
} }
TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { TEST(MultipartFormDataTest, PostInvalidBoundaryChars) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string data(1024 * 1024 * 2, '&'); std::string data(1024 * 1024 * 2, '&');
std::stringstream buffer; std::stringstream buffer;
buffer << data; buffer << data;
@ -6084,7 +6030,6 @@ TEST(MultipartFormDataTest, PutFormData) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
std::string data(1024 * 1024 * 2, '&'); std::string data(1024 * 1024 * 2, '&');
@ -6149,7 +6094,6 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) {
}); });
svr.wait_until_ready(); svr.wait_until_ready();
std::this_thread::sleep_for(std::chrono::seconds(1));
{ {
std::string data(1024 * 1024 * 2, '&'); std::string data(1024 * 1024 * 2, '&');
@ -6171,9 +6115,6 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) {
} }
TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { TEST(MultipartFormDataTest, PutInvalidBoundaryChars) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string data(1024 * 1024 * 2, '&'); std::string data(1024 * 1024 * 2, '&');
std::stringstream buffer; std::stringstream buffer;
buffer << data; buffer << data;
@ -6502,7 +6443,7 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::seconds(1)); svr.wait_until_ready();
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
@ -6548,7 +6489,7 @@ TEST(VulnerabilityTest, CRLFInjection) {
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
}); });
std::this_thread::sleep_for(std::chrono::seconds(1)); svr.wait_until_ready();
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);