Skip to content

Commit

Permalink
Add some eventual consistency delays and connect retry to to the IoTP…
Browse files Browse the repository at this point in the history
…ublishSubscribe test
  • Loading branch information
Bret Ambrose committed Jan 14, 2025
1 parent e65a61d commit 061bf08
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 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,24 @@ 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
{
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 +261,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

0 comments on commit 061bf08

Please sign in to comment.