Skip to content

Commit 98cd80e

Browse files
author
Bret Ambrose
committed
Refactor double client id test to not use a promise that can get multi-completed
1 parent 7f22f00 commit 98cd80e

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

tests/Mqtt5ClientTest.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,16 +1172,25 @@ static int s_TestMqtt5DoubleClientIDFailure(Aws::Crt::Allocator *allocator, void
11721172
std::shared_ptr<Aws::Crt::Mqtt5::ConnectPacket> packetConnect =
11731173
Aws::Crt::MakeShared<Aws::Crt::Mqtt5::ConnectPacket>(allocator);
11741174
packetConnect->WithClientId("TestMqtt5DoubleClientIDFailure" + Aws::Crt::UUID().ToString());
1175-
std::promise<void> disconnectPromise;
1175+
1176+
// using a promise here is problematic due to multi-completion being a crash in C++
1177+
bool disconnected = false;
1178+
std::mutex disconnectedLock;
1179+
std::condition_variable disconnectedSignal;
11761180

11771181
Mqtt5TestContext testContext1 = createTestContext(
11781182
allocator,
11791183
MQTT5CONNECT_DIRECT_IOT_CORE,
1180-
[packetConnect, &disconnectPromise](Mqtt5ClientOptions &options, const Mqtt5TestEnvVars &, Mqtt5TestContext &)
1184+
[&](Mqtt5ClientOptions &options, const Mqtt5TestEnvVars &, Mqtt5TestContext &)
11811185
{
11821186
options.WithConnectOptions(packetConnect);
1183-
options.WithClientDisconnectionCallback([&disconnectPromise](const OnDisconnectionEventData &)
1184-
{ disconnectPromise.set_value(); });
1187+
options.WithClientDisconnectionCallback(
1188+
[&](const OnDisconnectionEventData &)
1189+
{
1190+
std::lock_guard<std::mutex> lock(disconnectedLock);
1191+
disconnected = true;
1192+
disconnectedSignal.notify_one();
1193+
});
11851194

11861195
return AWS_OP_SUCCESS;
11871196
});
@@ -1222,9 +1231,10 @@ static int s_TestMqtt5DoubleClientIDFailure(Aws::Crt::Allocator *allocator, void
12221231
ASSERT_TRUE(testContext2.connectionPromise.get_future().get());
12231232

12241233
// Client 1 should get diconnected.
1225-
disconnectPromise.get_future().get();
1226-
// reset the promise so it would not get confused when we stop the client;
1227-
disconnectPromise = std::promise<void>();
1234+
{
1235+
std::unique_lock<std::mutex> lock(disconnectedLock);
1236+
disconnectedSignal.wait(lock, [&]() { return disconnected; });
1237+
}
12281238

12291239
ASSERT_TRUE(mqtt5Client2->Stop());
12301240
testContext2.stoppedPromise.get_future().get();

0 commit comments

Comments
 (0)