@@ -8207,9 +8207,8 @@ TEST(Expect100ContinueTest, ServerClosesConnection) {
82078207}
82088208#endif
82098209
8210- TEST (MaxTimeoutTest, ContentStream) {
8211- Server svr;
8212-
8210+ template <typename S, typename C>
8211+ inline void max_timeout_test (S &svr, C &cli, time_t timeout, time_t threshold) {
82138212 svr.Get (" /stream" , [&](const Request &, Response &res) {
82148213 auto data = new std::string (" 01234567890123456789" );
82158214
@@ -8279,10 +8278,6 @@ TEST(MaxTimeoutTest, ContentStream) {
82798278
82808279 svr.wait_until_ready ();
82818280
8282- const time_t timeout = 2000 ;
8283- const time_t threshold = 200 ;
8284-
8285- Client cli (" localhost" , PORT);
82868281 cli.set_max_timeout (std::chrono::milliseconds (timeout));
82878282
82888283 {
@@ -8334,132 +8329,25 @@ TEST(MaxTimeoutTest, ContentStream) {
83348329 }
83358330}
83368331
8337- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
8338- TEST (MaxTimeoutTest, ContentStreamSSL) {
8339- SSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
8340-
8341- svr.Get (" /stream" , [&](const Request &, Response &res) {
8342- auto data = new std::string (" 01234567890123456789" );
8343-
8344- res.set_content_provider (
8345- data->size (), " text/plain" ,
8346- [&, data](size_t offset, size_t length, DataSink &sink) {
8347- const size_t DATA_CHUNK_SIZE = 4 ;
8348- const auto &d = *data;
8349- std::this_thread::sleep_for (std::chrono::seconds (1 ));
8350- sink.write (&d[offset], std::min (length, DATA_CHUNK_SIZE));
8351- return true ;
8352- },
8353- [data](bool success) {
8354- EXPECT_FALSE (success);
8355- delete data;
8356- });
8357- });
8358-
8359- svr.Get (" /stream_without_length" , [&](const Request &, Response &res) {
8360- auto i = new size_t (0 );
8361-
8362- res.set_content_provider (
8363- " text/plain" ,
8364- [i](size_t , DataSink &sink) {
8365- if (*i < 5 ) {
8366- std::this_thread::sleep_for (std::chrono::seconds (1 ));
8367- sink.write (" abcd" , 4 );
8368- (*i)++;
8369- } else {
8370- sink.done ();
8371- }
8372- return true ;
8373- },
8374- [i](bool success) {
8375- EXPECT_FALSE (success);
8376- delete i;
8377- });
8378- });
8379-
8380- svr.Get (" /chunked" , [&](const Request &, Response &res) {
8381- auto i = new size_t (0 );
8382-
8383- res.set_chunked_content_provider (
8384- " text/plain" ,
8385- [i](size_t , DataSink &sink) {
8386- if (*i < 5 ) {
8387- std::this_thread::sleep_for (std::chrono::seconds (1 ));
8388- sink.os << " abcd" ;
8389- (*i)++;
8390- } else {
8391- sink.done ();
8392- }
8393- return true ;
8394- },
8395- [i](bool success) {
8396- EXPECT_FALSE (success);
8397- delete i;
8398- });
8399- });
8332+ TEST (MaxTimeoutTest, ContentStream) {
8333+ time_t timeout = 2000 ;
8334+ time_t threshold = 200 ;
84008335
8401- auto listen_thread = std::thread ([&svr]() { svr.listen (" localhost" , PORT); });
8402- auto se = detail::scope_exit ([&] {
8403- svr.stop ();
8404- listen_thread.join ();
8405- ASSERT_FALSE (svr.is_running ());
8406- });
8336+ Server svr;
8337+ Client cli (" localhost" , PORT);
8338+ max_timeout_test (svr, cli, timeout, threshold);
8339+ }
84078340
8408- svr.wait_until_ready ();
8341+ #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
8342+ TEST (MaxTimeoutTest, ContentStreamSSL) {
8343+ time_t timeout = 2000 ;
8344+ time_t threshold = 500 ; // SSL_shutdown is slow on some operating systems.
84098345
8410- const time_t timeout = 2000 ;
8411- const time_t threshold = 1000 ; // SSL_shutdown is slow...
8346+ SSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
84128347
84138348 SSLClient cli (" localhost" , PORT);
84148349 cli.enable_server_certificate_verification (false );
8415- cli.set_max_timeout (std::chrono::milliseconds (timeout));
8416-
8417- {
8418- auto start = std::chrono::steady_clock::now ();
8419-
8420- auto res = cli.Get (" /stream" );
8421-
8422- auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
8423- std::chrono::steady_clock::now () - start)
8424- .count ();
8425-
8426- ASSERT_FALSE (res);
8427- EXPECT_EQ (Error::Read, res.error ());
8428- EXPECT_TRUE (timeout <= elapsed && elapsed < timeout + threshold)
8429- << " Timeout exceeded by " << (elapsed - timeout) << " ms" ;
8430- }
84318350
8432- {
8433- auto start = std::chrono::steady_clock::now ();
8434-
8435- auto res = cli.Get (" /stream_without_length" );
8436-
8437- auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
8438- std::chrono::steady_clock::now () - start)
8439- .count ();
8440-
8441- ASSERT_FALSE (res);
8442- EXPECT_EQ (Error::Read, res.error ());
8443- EXPECT_TRUE (timeout <= elapsed && elapsed < timeout + threshold)
8444- << " Timeout exceeded by " << (elapsed - timeout) << " ms" ;
8445- }
8446-
8447- {
8448- auto start = std::chrono::steady_clock::now ();
8449-
8450- auto res = cli.Get (" /chunked" , [&](const char *data, size_t data_length) {
8451- EXPECT_EQ (" abcd" , string (data, data_length));
8452- return true ;
8453- });
8454-
8455- auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
8456- std::chrono::steady_clock::now () - start)
8457- .count ();
8458-
8459- ASSERT_FALSE (res);
8460- EXPECT_EQ (Error::Read, res.error ());
8461- EXPECT_TRUE (timeout <= elapsed && elapsed < timeout + threshold)
8462- << " Timeout exceeded by " << (elapsed - timeout) << " ms" ;
8463- }
8351+ max_timeout_test (svr, cli, timeout, threshold);
84648352}
84658353#endif
0 commit comments