@@ -157,6 +157,7 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
157157
158158 std::mutex mutex;
159159 std::condition_variable cv;
160+ bool connectionAttemptComplete = false ;
160161 bool connected = false ;
161162 bool subscribed = false ;
162163 bool published = false ;
@@ -172,7 +173,11 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
172173 (int )sessionPresent);
173174 {
174175 std::lock_guard<std::mutex> lock (mutex);
175- connected = true ;
176+ connectionAttemptComplete = true ;
177+ if (errorCode == AWS_ERROR_SUCCESS && returnCode == AWS_MQTT_CONNECT_ACCEPTED)
178+ {
179+ connected = true ;
180+ }
176181 }
177182 cv.notify_one ();
178183 };
@@ -229,11 +234,24 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
229234 mqttConnection->OnConnectionClosed = onConnectionClosed;
230235 Aws::Crt::UUID Uuid;
231236 Aws::Crt::String uuidStr = Uuid.ToString ();
232- mqttConnection->Connect (uuidStr.c_str (), true );
233237
238+ bool done = false ;
239+ while (!done)
234240 {
235- std::unique_lock<std::mutex> lock (mutex);
236- cv.wait (lock, [&]() { return connected; });
241+ mqttConnection->Connect (uuidStr.c_str (), true );
242+
243+ {
244+ std::unique_lock<std::mutex> lock (mutex);
245+ cv.wait (lock, [&]() { return connectionAttemptComplete; });
246+ if (connected)
247+ {
248+ done = true ;
249+ }
250+ else
251+ {
252+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
253+ }
254+ }
237255 }
238256
239257 mqttConnection->Subscribe (" /publish/me/senpai" , QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, onTest, onSubAck);
@@ -243,6 +261,9 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
243261 cv.wait (lock, [&]() { return subscribed; });
244262 }
245263
264+ // try to settle any eventual consistency issues server-side
265+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
266+
246267 Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString (" notice me pls" );
247268 mqttConnection->Publish (" /publish/me/senpai" , QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false , payload, onPubAck);
248269
0 commit comments