1616
1717using namespace std ;
1818
19+
1920namespace aws {
2021 namespace iot {
2122 namespace securedtunneling {
2223 namespace test {
2324 constexpr char LOCALHOST[] = " 127.0.0.1" ;
24- constexpr int IO_PAUSE_MS = 1000 ;
25+ constexpr int IO_TIMEOUT_MS = 10000 ;
26+ mutex m;
27+ condition_variable cv;
2528 unsigned short get_available_port () {
2629 boost::asio::io_context io_ctx { };
2730 tcp::acceptor acceptor (io_ctx);
@@ -69,6 +72,7 @@ namespace aws {
6972 auto on_tcp_tunnel =
7073 [this ](const boost::system::error_code& ec_response) {
7174 ec = ec_response;
75+ cv.notify_one ();
7276 };
7377 http_server_thread = make_unique<thread>([this ]() { http_server.run (); });
7478 https_proxy_adapter_thread = make_unique<thread>([this , on_tcp_tunnel]() {
@@ -89,7 +93,8 @@ namespace aws {
8993 cout << " Test HTTPS proxy adapter base case with no credentials" << endl;
9094 TestContext test_context{};
9195 test_context.start ();
92- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
96+ std::unique_lock<mutex> lk (m);
97+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
9398 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::Success));
9499 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::Success);
95100 test_context.stop ();
@@ -99,7 +104,8 @@ namespace aws {
99104 cout << " Test HTTPS proxy adapter handling of valid credentials response" << endl;
100105 TestContext test_context{username + " :" + password};
101106 test_context.start ();
102- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
107+ std::unique_lock<mutex> lk (m);
108+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
103109 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::Success));
104110 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::Success);
105111 test_context.stop ();
@@ -109,7 +115,8 @@ namespace aws {
109115 cout << " Test HTTPS proxy adapter handling of bad credentials response" << endl;
110116 TestContext test_context{username + " a:" + password};
111117 test_context.start ();
112- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
118+ std::unique_lock<mutex> lk (m);
119+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
113120 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::ClientError));
114121 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::ClientError);
115122 test_context.stop ();
@@ -119,7 +126,8 @@ namespace aws {
119126 cout << " Test HTTPS proxy adapter handling of 500 response" << endl;
120127 TestContext test_context{" 500" };
121128 test_context.start ();
122- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
129+ std::unique_lock<mutex> lk (m);
130+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
123131 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::ServerError));
124132 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::ServerError);
125133 test_context.stop ();
@@ -129,7 +137,8 @@ namespace aws {
129137 cout << " Test HTTPS proxy adapter handling of 100 response" << endl;
130138 TestContext test_context{" 100" };
131139 test_context.start ();
132- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
140+ std::unique_lock<mutex> lk (m);
141+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
133142 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::OtherHttpError));
134143 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::OtherHttpError);
135144 test_context.stop ();
@@ -139,7 +148,8 @@ namespace aws {
139148 cout << " Test HTTPS proxy adapter handling of 300 response" << endl;
140149 TestContext test_context{" 300" };
141150 test_context.start ();
142- this_thread::sleep_for (chrono::microseconds (IO_PAUSE_MS));
151+ std::unique_lock<mutex> lk (m);
152+ cv.wait_for (lk, chrono::milliseconds (IO_TIMEOUT_MS));
143153 REQUIRE (test_context.ec .message () == WebProxyAdapterErrc_category ().message ((int ) WebProxyAdapterErrc::RedirectionError));
144154 REQUIRE (static_cast <WebProxyAdapterErrc>(test_context.ec .value ()) == WebProxyAdapterErrc::RedirectionError);
145155 test_context.stop ();
0 commit comments