Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race conditions in samples and service tests #748

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions samples/device_defender/mqtt5_basic_report/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,19 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputDeviceDefender(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
auto clientConfigBuilder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto clientConfigBuilder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));
if (clientConfigBuilder == nullptr)
{
fprintf(
stdout,
"Failed to setup MQTT5 client builder with error code %d: %s",
LastError(),
ErrorDebugString(LastError()));
return -1;
}

if (cmdData.input_ca != "")
{
clientConfigBuilder->WithCertificateAuthority(cmdData.input_ca.c_str());
Expand Down Expand Up @@ -119,9 +130,6 @@ int main(int argc, char *argv[])
// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = clientConfigBuilder->Build();

// Clean up the builder
delete clientConfigBuilder;

if (client == nullptr)
{
fprintf(
Expand Down
2 changes: 1 addition & 1 deletion samples/fleet_provisioning/fleet_provisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ int main(int argc, char *argv[])
{
/************************ Setup ****************************/

// Do the global initialization for the API
// Do the global initialization for the API
ApiHandle apiHandle;

/**
Expand Down
12 changes: 6 additions & 6 deletions samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ struct RegisterThingContext
/**
* Create MQTT5 client.
*/
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(Mqtt5ClientContext &ctx, const Utils::cmdData &cmdData)
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmdData &cmdData, Mqtt5ClientContext &ctx)
{
// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));

// Check if the builder setup correctly.
if (builder == nullptr)
Expand Down Expand Up @@ -128,7 +129,6 @@ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(Mqtt5ClientConte

// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
delete builder;

return client;
}
Expand Down Expand Up @@ -418,7 +418,7 @@ int main(int argc, char *argv[])
{
/************************ Setup ****************************/

// Do the global initialization for the API
// Do the global initialization for the API
ApiHandle apiHandle;

/**
Expand All @@ -429,7 +429,7 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputFleetProvisioning(argc, argv, &apiHandle);

Mqtt5ClientContext mqtt5ClientContext;
auto client = createMqtt5Client(mqtt5ClientContext, cmdData);
auto client = createMqtt5Client(cmdData, mqtt5ClientContext);

/************************ Run the sample ****************************/

Expand Down
6 changes: 3 additions & 3 deletions samples/jobs/mqtt5_job_execution/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputJobs(argc, argv, &apiHandle);

// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));

// Check if the builder setup correctly.
if (builder == nullptr)
Expand Down Expand Up @@ -101,7 +102,6 @@ int main(int argc, char *argv[])

// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
delete builder;
/************************ Run the sample ****************************/

fprintf(stdout, "Connecting...\n");
Expand Down
8 changes: 3 additions & 5 deletions samples/mqtt5/mqtt5_pubsub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputPubSub(argc, argv, &apiHandle, "mqtt5-pubsub");

// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));

// Check if the builder setup correctly.
if (builder == nullptr)
Expand Down Expand Up @@ -107,9 +108,6 @@ int main(int argc, char *argv[])
// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();

// Clean up the builder
delete builder;

if (client == nullptr)
{
fprintf(
Expand Down
6 changes: 3 additions & 3 deletions samples/mqtt5/mqtt5_shared_subscription/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class sample_mqtt5_client
std::shared_ptr<sample_mqtt5_client> result = std::make_shared<sample_mqtt5_client>();
result->name = input_clientName;

Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
input_endpoint, input_cert.c_str(), input_key.c_str());
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
input_endpoint, input_cert.c_str(), input_key.c_str()));
if (builder == nullptr)
{
return nullptr;
Expand Down Expand Up @@ -136,7 +137,6 @@ class sample_mqtt5_client
});

result->client = builder->Build();
delete builder;
return result;
}
};
Expand Down
4 changes: 4 additions & 0 deletions samples/secure_tunneling/secure_tunnel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ int main(int argc, char *argv[])
* In a real world application you probably don't want to enforce synchronous behavior
* but this is a sample console application, so we'll just do that with a condition variable.
*/
std::promise<void> clientConnectedPromise;
std::promise<bool> clientStoppedPromise;

// service id storage for use in sample
Expand Down Expand Up @@ -285,6 +286,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "Sending Stream Start request\n");
secureTunnel->SendStreamStart();
}
clientConnectedPromise.set_value();
}
});

Expand Down Expand Up @@ -345,6 +347,8 @@ int main(int argc, char *argv[])
exit(-1);
}

clientConnectedPromise.get_future().wait_for(std::chrono::seconds(5));

/*
* In Destination mode the Secure Tunnel Client will remain open and echo messages that come in.
* In Source mode the Secure Tunnel Client will send 4 messages and then disconnect and terminate.
Expand Down
7 changes: 3 additions & 4 deletions samples/shadow/mqtt5_shadow_sync/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputShadow(argc, argv, &apiHandle);

// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());

auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()));
// Check if the builder setup correctly.
if (builder == nullptr)
{
Expand Down Expand Up @@ -148,7 +148,6 @@ int main(int argc, char *argv[])

// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
delete builder;
/************************ Run the sample ****************************/

fprintf(stdout, "Connecting...\n");
Expand Down
Loading
Loading