Skip to content

Commit

Permalink
Update generated tests to not allocate Aws:: stl objects as static
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Apr 23, 2024
1 parent 99f54e4 commit 542f071
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
40 changes: 33 additions & 7 deletions generated/tests/s3-gen-tests/S3EndpointProviderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ using ExpEpProps = Aws::UnorderedMap<Aws::String, Aws::Vector<Aws::Vector<EpProp
using ExpEpAuthScheme = Aws::Vector<EpProp>;
using ExpEpHeaders = Aws::UnorderedMap<Aws::String, Aws::Vector<Aws::String>>;

class S3EndpointProviderTests : public ::testing::TestWithParam<size_t> {};

struct S3EndpointProviderEndpointTestCase
{
using OperationParamsFromTest = EndpointParameters;
Expand Down Expand Up @@ -55,7 +53,32 @@ struct S3EndpointProviderEndpointTestCase
// Aws::Vector<OperationInput> operationInput;
};

static const Aws::Vector<S3EndpointProviderEndpointTestCase> TEST_CASES = {
class S3EndpointProviderTests : public ::testing::TestWithParam<size_t>
{
public:
static const size_t TEST_CASES_SZ;
protected:
static Aws::Vector<S3EndpointProviderEndpointTestCase> getTestCase();
static Aws::UniquePtrSafeDeleted<Aws::Vector<S3EndpointProviderEndpointTestCase>> TEST_CASES;
static void SetUpTestSuite()
{
TEST_CASES = Aws::MakeUniqueSafeDeleted<Aws::Vector<S3EndpointProviderEndpointTestCase>>(ALLOCATION_TAG, getTestCase());
ASSERT_TRUE(TEST_CASES) << "Failed to allocate TEST_CASES table";
assert(TEST_CASES->size() == TEST_CASES_SZ);
}

static void TearDownTestSuite()
{
TEST_CASES.reset();
}
};

Aws::UniquePtrSafeDeleted<Aws::Vector<S3EndpointProviderEndpointTestCase>> S3EndpointProviderTests::TEST_CASES;
const size_t S3EndpointProviderTests::TEST_CASES_SZ = 297;

Aws::Vector<S3EndpointProviderEndpointTestCase> S3EndpointProviderTests::getTestCase() {

Aws::Vector<S3EndpointProviderEndpointTestCase> test_cases = {
/*TEST CASE 0*/
{"region is not a valid DNS-suffix", // documentation
{EpParam("UseFIPS", false), EpParam("Region", "a b"), EpParam("Accelerate", false), EpParam("UseDualStack", false)}, // params
Expand Down Expand Up @@ -2585,7 +2608,9 @@ static const Aws::Vector<S3EndpointProviderEndpointTestCase> TEST_CASES = {
{}, // tags
{{/*No endpoint expected*/}, /*error*/"S3Express bucket name is not a valid virtual hostable name."} // expect
}
};
};
return test_cases;
}

Aws::String RulesToSdkSignerName(const Aws::String& rulesSignerName)
{
Expand Down Expand Up @@ -2680,9 +2705,10 @@ void ValidateOutcome(const ResolveEndpointOutcome& outcome, const S3EndpointProv
TEST_P(S3EndpointProviderTests, EndpointProviderTest)
{
const size_t TEST_CASE_IDX = GetParam();
ASSERT_LT(TEST_CASE_IDX, TEST_CASES.size()) << "Something is wrong with the test fixture itself.";
const S3EndpointProviderEndpointTestCase& TEST_CASE = TEST_CASES.at(TEST_CASE_IDX);
ASSERT_LT(TEST_CASE_IDX, TEST_CASES->size()) << "Something is wrong with the test fixture itself.";
const S3EndpointProviderEndpointTestCase& TEST_CASE = TEST_CASES->at(TEST_CASE_IDX);
SCOPED_TRACE(Aws::String("\nTEST CASE # ") + Aws::Utils::StringUtils::to_string(TEST_CASE_IDX) + ": " + TEST_CASE.documentation);
SCOPED_TRACE(Aws::String("\n--gtest_filter=EndpointTestsFromModel/S3EndpointProviderTests.EndpointProviderTest/") + Aws::Utils::StringUtils::to_string(TEST_CASE_IDX));

std::shared_ptr<S3EndpointProvider> endpointProvider = Aws::MakeShared<S3EndpointProvider>(ALLOCATION_TAG);
ASSERT_TRUE(endpointProvider) << "Failed to allocate/initialize S3EndpointProvider";
Expand Down Expand Up @@ -2724,4 +2750,4 @@ TEST_P(S3EndpointProviderTests, EndpointProviderTest)

INSTANTIATE_TEST_SUITE_P(EndpointTestsFromModel,
S3EndpointProviderTests,
::testing::Range((size_t) 0u, TEST_CASES.size()));
::testing::Range((size_t) 0u, S3EndpointProviderTests::TEST_CASES_SZ));
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ namespace Aws
{
#ifdef USE_AWS_MEMORY_MANAGEMENT
Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem();
assert(memorySystem && "Memory system is not initialized");
// Was InitAPI forgotten or ShutdownAPI already called or Aws:: class used as static?
// TODO: enforce to non-conditional assert
AWS_ASSERT(memorySystem && "Memory system is not initialized.");
AWS_UNREFERENCED_PARAM(memorySystem);
#endif
AWS_UNREFERENCED_PARAM(allocationTag);

Expand Down
2 changes: 1 addition & 1 deletion src/aws-cpp-sdk-core/source/Aws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace Aws
Aws::Utils::Memory::InitializeAWSMemorySystem(*options.memoryManagementOptions.memoryManager);
}
#endif // USE_AWS_MEMORY_MANAGEMENT
Aws::InitializeCrt();
Aws::Client::CoreErrorsMapper::InitCoreErrorsMapper();
if(options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off)
{
Expand All @@ -72,6 +71,7 @@ namespace Aws
AWS_LOGSTREAM_INFO(ALLOCATION_TAG, "Initiate AWS SDK for C++ with Version:" << Aws::String(Aws::Version::GetVersionString()));
}

Aws::InitializeCrt();
Aws::Config::InitConfigAndCredentialsCacheManager();

if (options.ioOptions.clientBootstrap_create_fn)
Expand Down
10 changes: 8 additions & 2 deletions src/aws-cpp-sdk-core/source/utils/memory/AWSMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ void* Malloc(const char* allocationTag, size_t allocationSize)
{
Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem();
#ifdef USE_AWS_MEMORY_MANAGEMENT
// Was InitAPI forgotten or ShutdownAPI already called?
assert(memorySystem && "Memory system is not initialized.");
// Was InitAPI forgotten or ShutdownAPI already called or Aws:: class used as static?
// TODO: enforce to non-conditional assert AWS_ASSERT
AWS_ASSERT(memorySystem && "Memory system is not initialized.");
#endif

void* rawMemory = nullptr;
Expand All @@ -92,6 +93,11 @@ void Free(void* memoryPtr)
}

Aws::Utils::Memory::MemorySystemInterface* memorySystem = Aws::Utils::Memory::GetMemorySystem();
#ifdef USE_AWS_MEMORY_MANAGEMENT
// Was InitAPI forgotten or ShutdownAPI already called or Aws:: class used as static?
// TODO: enforce to non-conditional assert
AWS_ASSERT(memorySystem && "Memory system is not initialized.");
#endif
if(memorySystem != nullptr)
{
memorySystem->FreeMemory(memoryPtr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ using ExpEpProps = Aws::UnorderedMap<Aws::String, Aws::Vector<Aws::Vector<EpProp
using ExpEpAuthScheme = Aws::Vector<EpProp>;
using ExpEpHeaders = Aws::UnorderedMap<Aws::String, Aws::Vector<Aws::String>>;

class ${metadata.classNamePrefix}EndpointProviderTests : public ::testing::TestWithParam<size_t> {};

struct ${metadata.classNamePrefix}EndpointProviderEndpointTestCase
{
using OperationParamsFromTest = EndpointParameters;
Expand Down Expand Up @@ -57,7 +55,32 @@ struct ${metadata.classNamePrefix}EndpointProviderEndpointTestCase
// Aws::Vector<OperationInput> operationInput;
};

static const Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase> TEST_CASES = {
class ${metadata.classNamePrefix}EndpointProviderTests : public ::testing::TestWithParam<size_t>
{
public:
static const size_t TEST_CASES_SZ;
protected:
static Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase> getTestCase();
static Aws::UniquePtrSafeDeleted<Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase>> TEST_CASES;
static void SetUpTestSuite()
{
TEST_CASES = Aws::MakeUniqueSafeDeleted<Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase>>(ALLOCATION_TAG, getTestCase());
ASSERT_TRUE(TEST_CASES) << "Failed to allocate TEST_CASES table";
assert(TEST_CASES->size() == TEST_CASES_SZ);
}

static void TearDownTestSuite()
{
TEST_CASES.reset();
}
};

Aws::UniquePtrSafeDeleted<Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase>> ${metadata.classNamePrefix}EndpointProviderTests::TEST_CASES;
const size_t ${metadata.classNamePrefix}EndpointProviderTests::TEST_CASES_SZ = ${testCases.size()};

Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase> ${metadata.classNamePrefix}EndpointProviderTests::getTestCase() {

Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTestCase> test_cases = {
#foreach($testCase in $testCases)
/*TEST CASE ${foreach.index}*/
{"${testCase.documentation}", // documentation
Expand All @@ -68,7 +91,9 @@ static const Aws::Vector<${metadata.classNamePrefix}EndpointProviderEndpointTest
#if($foreach.hasNext) },#else }#end

#end
};
};
return test_cases;
}

Aws::String RulesToSdkSignerName(const Aws::String& rulesSignerName)
{
Expand Down Expand Up @@ -163,9 +188,10 @@ void ValidateOutcome(const ResolveEndpointOutcome& outcome, const ${metadata.cla
TEST_P(${metadata.classNamePrefix}EndpointProviderTests, EndpointProviderTest)
{
const size_t TEST_CASE_IDX = GetParam();
ASSERT_LT(TEST_CASE_IDX, TEST_CASES.size()) << "Something is wrong with the test fixture itself.";
const ${metadata.classNamePrefix}EndpointProviderEndpointTestCase& TEST_CASE = TEST_CASES.at(TEST_CASE_IDX);
ASSERT_LT(TEST_CASE_IDX, TEST_CASES->size()) << "Something is wrong with the test fixture itself.";
const ${metadata.classNamePrefix}EndpointProviderEndpointTestCase& TEST_CASE = TEST_CASES->at(TEST_CASE_IDX);
SCOPED_TRACE(Aws::String("\nTEST CASE # ") + Aws::Utils::StringUtils::to_string(TEST_CASE_IDX) + ": " + TEST_CASE.documentation);
SCOPED_TRACE(Aws::String("\n--gtest_filter=EndpointTestsFromModel/${metadata.classNamePrefix}EndpointProviderTests.EndpointProviderTest/") + Aws::Utils::StringUtils::to_string(TEST_CASE_IDX));

std::shared_ptr<${metadata.classNamePrefix}EndpointProvider> endpointProvider = Aws::MakeShared<${metadata.classNamePrefix}EndpointProvider>(ALLOCATION_TAG);
ASSERT_TRUE(endpointProvider) << "Failed to allocate/initialize ${metadata.classNamePrefix}EndpointProvider";
Expand Down Expand Up @@ -207,4 +233,4 @@ TEST_P(${metadata.classNamePrefix}EndpointProviderTests, EndpointProviderTest)

INSTANTIATE_TEST_SUITE_P(EndpointTestsFromModel,
${metadata.classNamePrefix}EndpointProviderTests,
::testing::Range((size_t) 0u, TEST_CASES.size()));
::testing::Range((size_t) 0u, ${metadata.classNamePrefix}EndpointProviderTests::TEST_CASES_SZ));

0 comments on commit 542f071

Please sign in to comment.