Skip to content

Commit

Permalink
fix random test failure due to thread not finishing (#91)
Browse files Browse the repository at this point in the history
* fix random test failure due to thread not finishing

* removed an unused variable
  • Loading branch information
xlcheng1 authored Aug 12, 2022
1 parent 607bd3a commit b7ae265
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
23 changes: 1 addition & 22 deletions test/AdapterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ TEST_CASE( "Test destination mode", "[destination]") {
this_thread::sleep_for(chrono::milliseconds(IO_PAUSE_MS));

// Verify destination app is connected
REQUIRE( accepted );
tcp_accept_thread.join();
REQUIRE( accepted );

// Simulate sending data messages from destination app
uint8_t read_buffer[READ_BUFFER_SIZE];
Expand All @@ -471,27 +471,6 @@ TEST_CASE( "Test destination mode", "[destination]") {
{
return (msg.type() == com::amazonaws::iot::securedtunneling::Message_Type_STREAM_RESET) && msg.streamid() == 1;
});
destination_socket.close();

accepted = false;
tcp_accept_thread = std::thread{[&acceptor, &destination_socket, &accepted]()
{
acceptor.accept(destination_socket);
accepted = true;
}};
ws_server.deliver_message(ws_server_message);
this_thread::sleep_for(chrono::milliseconds(IO_PAUSE_MS));
REQUIRE( accepted );
tcp_accept_thread.join();

// Simulate sending data messages from destination app
for(int i = 0; i < 5; ++i)
{
string const test_string = (boost::format("test message: %1%") % i).str();
destination_socket.send(boost::asio::buffer(test_string));
destination_socket.read_some(boost::asio::buffer(reinterpret_cast<void *>(read_buffer), READ_BUFFER_SIZE));
CHECK( string(reinterpret_cast<char *>(read_buffer)) == test_string );
}

//instruct websocket to close on client
ws_server.close_client("test_closure", boost::beast::websocket::internal_error); //need to perform write to trigger close
Expand Down
24 changes: 17 additions & 7 deletions test/WebProxyAdapterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

using namespace std;


namespace aws {
namespace iot {
namespace securedtunneling {
namespace test {
constexpr char LOCALHOST[] = "127.0.0.1";
constexpr int IO_PAUSE_MS = 1000;
constexpr int IO_TIMEOUT_MS = 10000;
mutex m;
condition_variable cv;
unsigned short get_available_port() {
boost::asio::io_context io_ctx { };
tcp::acceptor acceptor(io_ctx);
Expand Down Expand Up @@ -69,6 +72,7 @@ namespace aws {
auto on_tcp_tunnel =
[this](const boost::system::error_code& ec_response) {
ec = ec_response;
cv.notify_one();
};
http_server_thread = make_unique<thread>([this]() { http_server.run(); });
https_proxy_adapter_thread = make_unique<thread>([this, on_tcp_tunnel]() {
Expand All @@ -89,7 +93,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter base case with no credentials" << endl;
TestContext test_context{};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::Success));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::Success);
test_context.stop();
Expand All @@ -99,7 +104,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter handling of valid credentials response" << endl;
TestContext test_context{username + ":" + password};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::Success));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::Success);
test_context.stop();
Expand All @@ -109,7 +115,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter handling of bad credentials response" << endl;
TestContext test_context{username + "a:" + password};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::ClientError));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::ClientError);
test_context.stop();
Expand All @@ -119,7 +126,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter handling of 500 response" << endl;
TestContext test_context{"500"};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::ServerError));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::ServerError);
test_context.stop();
Expand All @@ -129,7 +137,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter handling of 100 response" << endl;
TestContext test_context{"100"};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::OtherHttpError));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::OtherHttpError);
test_context.stop();
Expand All @@ -139,7 +148,8 @@ namespace aws {
cout << "Test HTTPS proxy adapter handling of 300 response" << endl;
TestContext test_context{"300"};
test_context.start();
this_thread::sleep_for(chrono::microseconds(IO_PAUSE_MS));
std::unique_lock<mutex> lk(m);
cv.wait_for(lk, chrono::milliseconds(IO_TIMEOUT_MS));
REQUIRE(test_context.ec.message() == WebProxyAdapterErrc_category().message((int) WebProxyAdapterErrc::RedirectionError));
REQUIRE(static_cast<WebProxyAdapterErrc>(test_context.ec.value()) == WebProxyAdapterErrc::RedirectionError);
test_context.stop();
Expand Down

0 comments on commit b7ae265

Please sign in to comment.