Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IotPublishSubscribe test with additional resiliency #698

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions tests/IotServiceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)

std::mutex mutex;
std::condition_variable cv;
bool connectionAttemptComplete = false;
bool connected = false;
bool subscribed = false;
bool published = false;
Expand All @@ -172,7 +173,11 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
(int)sessionPresent);
{
std::lock_guard<std::mutex> lock(mutex);
connected = true;
connectionAttemptComplete = true;
if (errorCode == AWS_ERROR_SUCCESS && returnCode == AWS_MQTT_CONNECT_ACCEPTED)
{
connected = true;
}
}
cv.notify_one();
};
Expand Down Expand Up @@ -229,11 +234,25 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
mqttConnection->OnConnectionClosed = onConnectionClosed;
Aws::Crt::UUID Uuid;
Aws::Crt::String uuidStr = Uuid.ToString();
mqttConnection->Connect(uuidStr.c_str(), true);

bool done = false;
while (!done)
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&]() { return connected; });
mqttConnection->Connect(uuidStr.c_str(), true);

{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&]() { return connectionAttemptComplete; });
if (connected)
{
done = true;
}
else
{
connectionAttemptComplete = false;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
}

mqttConnection->Subscribe("/publish/me/senpai", QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, onTest, onSubAck);
Expand All @@ -243,6 +262,9 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
cv.wait(lock, [&]() { return subscribed; });
}

// try to settle any eventual consistency issues server-side
std::this_thread::sleep_for(std::chrono::seconds(2));

Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString("notice me pls");
mqttConnection->Publish("/publish/me/senpai", QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false, payload, onPubAck);

Expand Down
Loading