Skip to content

Commit b7ae265

Browse files
authored
fix random test failure due to thread not finishing (#91)
* fix random test failure due to thread not finishing * removed an unused variable
1 parent 607bd3a commit b7ae265

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

test/AdapterTests.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ TEST_CASE( "Test destination mode", "[destination]") {
451451
this_thread::sleep_for(chrono::milliseconds(IO_PAUSE_MS));
452452

453453
// Verify destination app is connected
454-
REQUIRE( accepted );
455454
tcp_accept_thread.join();
455+
REQUIRE( accepted );
456456

457457
// Simulate sending data messages from destination app
458458
uint8_t read_buffer[READ_BUFFER_SIZE];
@@ -471,27 +471,6 @@ TEST_CASE( "Test destination mode", "[destination]") {
471471
{
472472
return (msg.type() == com::amazonaws::iot::securedtunneling::Message_Type_STREAM_RESET) && msg.streamid() == 1;
473473
});
474-
destination_socket.close();
475-
476-
accepted = false;
477-
tcp_accept_thread = std::thread{[&acceptor, &destination_socket, &accepted]()
478-
{
479-
acceptor.accept(destination_socket);
480-
accepted = true;
481-
}};
482-
ws_server.deliver_message(ws_server_message);
483-
this_thread::sleep_for(chrono::milliseconds(IO_PAUSE_MS));
484-
REQUIRE( accepted );
485-
tcp_accept_thread.join();
486-
487-
// Simulate sending data messages from destination app
488-
for(int i = 0; i < 5; ++i)
489-
{
490-
string const test_string = (boost::format("test message: %1%") % i).str();
491-
destination_socket.send(boost::asio::buffer(test_string));
492-
destination_socket.read_some(boost::asio::buffer(reinterpret_cast<void *>(read_buffer), READ_BUFFER_SIZE));
493-
CHECK( string(reinterpret_cast<char *>(read_buffer)) == test_string );
494-
}
495474

496475
//instruct websocket to close on client
497476
ws_server.close_client("test_closure", boost::beast::websocket::internal_error); //need to perform write to trigger close

test/WebProxyAdapterTests.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
using namespace std;
1818

19+
1920
namespace 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

Comments
 (0)