|
I have a simple test that sets up an MqttConnection: TEST(IoT, SimpleMqttConnection)
{
Aws::Crt::ApiHandle api_handle;
Aws::Crt::Io::EventLoopGroup iot_event_loop_group(1);
ASSERT_TRUE(iot_event_loop_group);
std::size_t max_num_hosts_in_cache = 2;
std::size_t cache_ttl = 30;
Aws::Crt::Io::DefaultHostResolver host_resolver(iot_event_loop_group, max_num_hosts_in_cache, cache_ttl);
Aws::Crt::Io::ClientBootstrap bootstrap(iot_event_loop_group, host_resolver);
ASSERT_TRUE(bootstrap);
Aws::Iot::MqttClient mqtt_client(bootstrap);
auto client_config = buildMqttConnectionConfig(); // defined elsewhere
auto mqtt_connection = mqtt_client.NewConnection(client_config);
mqtt_connection->SetReconnectTimeout(10, 10);
ASSERT_TRUE(*mqtt_connection);
std::promise<bool> connectionCompletedPromise;
std::promise<void> connectionClosedPromise;
// This will execute when an mqtt connect has completed or failed.
auto onConnectionCompleted = [&](Aws::Crt::Mqtt::MqttConnection &, int errorCode, Aws::Crt::Mqtt::ReturnCode returnCode, bool)
{
if (errorCode)
{
std::cerr << "Connection failed with error " << Aws::Crt::ErrorDebugString(errorCode);
connectionCompletedPromise.set_value(false);
}
else
{
std::cout << "Connection completed with return code " << returnCode << std::endl;
connectionCompletedPromise.set_value(true);
}
};
mqtt_connection->OnConnectionCompleted = std::move(onConnectionCompleted);
std::cout << "Connecting..." << std::endl;
auto client_id = Aws::Crt::UUID().ToString();
bool clean_session = true; // see https://github.com/aws/aws-iot-device-sdk-cpp-v2/issues/400#issuecomment-1069613603
static const int KEEP_ALIVE_SEC = 0;
if (!mqtt_connection->Connect(client_id.c_str(), clean_session, KEEP_ALIVE_SEC))
{
FAIL() << "MQTT Connection failed with error " << Aws::Crt::ErrorDebugString(mqtt_connection->LastError());
}
EXPECT_TRUE(connectionCompletedPromise.get_future().get());
std::cout << "Test scope ends." << std::endl;
}Output is But then the program hangs. If I add api_handle.SetShutdownBehavior(Aws::Crt::ApiHandleShutdownBehavior::NonBlocking);the program hangs as well. The ~ApiHandle destructor does not call What do I need to do to make the program finish cleanly? |
Answered by
jmklix
Jul 7, 2022
Replies: 2 comments 3 replies
|
It looks like your using part of SampleConnectAndDisconnect, but you seem to be missing the wait for disconnect: /*
* Invoked when a disconnect message has completed.
*/
auto onDisconnect = [&](Aws::Crt::Mqtt::MqttConnection &) {
fprintf(stdout, "Disconnect completed\n");
connectionClosedPromise.set_value();
};
...
/* Disconnect */
if (connection->Disconnect())
{
connectionClosedPromise.get_future().wait();
} |
3 replies
Answer selected by
stwirth
|
Hello! Reopening this discussion to make it searchable. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like your using part of SampleConnectAndDisconnect, but you seem to be missing the wait for disconnect: