@@ -157,6 +157,7 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
157
157
158
158
std::mutex mutex;
159
159
std::condition_variable cv;
160
+ bool connectionAttemptComplete = false ;
160
161
bool connected = false ;
161
162
bool subscribed = false ;
162
163
bool published = false ;
@@ -172,7 +173,11 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
172
173
(int )sessionPresent);
173
174
{
174
175
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
+ }
176
181
}
177
182
cv.notify_one ();
178
183
};
@@ -229,11 +234,24 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
229
234
mqttConnection->OnConnectionClosed = onConnectionClosed;
230
235
Aws::Crt::UUID Uuid;
231
236
Aws::Crt::String uuidStr = Uuid.ToString ();
232
- mqttConnection->Connect (uuidStr.c_str (), true );
233
237
238
+ bool done = false ;
239
+ while (!done)
234
240
{
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
+ }
237
255
}
238
256
239
257
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)
243
261
cv.wait (lock, [&]() { return subscribed; });
244
262
}
245
263
264
+ // try to settle any eventual consistency issues server-side
265
+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
266
+
246
267
Aws::Crt::ByteBuf payload = Aws::Crt::ByteBufFromCString (" notice me pls" );
247
268
mqttConnection->Publish (" /publish/me/senpai" , QOS::AWS_MQTT_QOS_AT_LEAST_ONCE, false , payload, onPubAck);
248
269
0 commit comments