From 7fbcab9f1341196bb50361283c99bf5961da91d4 Mon Sep 17 00:00:00 2001 From: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> Date: Tue, 15 Feb 2022 13:27:54 -0600 Subject: [PATCH] gtest_repeat crashes (#7645) * Moved allocation to avoid crash on gtest_repeat. Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> * Fixed GradientSignal tests to work with gtest_repeat. Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> --- .../AzTest/AzTest/GemTestEnvironment.cpp | 3 ++- .../Code/Tests/GradientSignalTestFixtures.cpp | 16 ++++++++++++++++ .../Code/Tests/GradientSignalTestFixtures.h | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzTest/AzTest/GemTestEnvironment.cpp b/Code/Framework/AzTest/AzTest/GemTestEnvironment.cpp index e862cdfa94..47eb71898a 100644 --- a/Code/Framework/AzTest/AzTest/GemTestEnvironment.cpp +++ b/Code/Framework/AzTest/AzTest/GemTestEnvironment.cpp @@ -52,7 +52,6 @@ namespace AZ GemTestEnvironment::GemTestEnvironment() { - m_parameters = new Parameters; } void GemTestEnvironment::AddDynamicModulePaths(const AZStd::vector& dynamicModulePaths) @@ -84,6 +83,8 @@ namespace AZ AZ::AllocatorInstance::Create(); + m_parameters = new Parameters; + AddGemsAndComponents(); PreCreateApplication(); diff --git a/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.cpp b/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.cpp index 8d5ada5214..2115d878c6 100644 --- a/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.cpp +++ b/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.cpp @@ -75,6 +75,22 @@ namespace UnitTest }); } + void GradientSignalTestEnvironment::PostCreateApplication() + { + // Ebus usage will allocate a global context on first usage. If that first usage occurs in a DLL, then the context will be + // invalid on subsequent unit test runs if using gtest_repeat. However, if we force the ebus to create their global context in + // the main test DLL (this one), the context will remain active throughout repeated runs. By creating them in + // PostCreateApplication(), they will be created before the DLLs get loaded and any system components from those DLLs run, so we + // can guarantee this will be the first usage. + + // These ebuses need their contexts created here before any of the dependent DLLs get loaded: + AZ::AssetTypeInfoBus::GetOrCreateContext(); + SurfaceData::SurfaceDataSystemRequestBus::GetOrCreateContext(); + SurfaceData::SurfaceDataProviderRequestBus::GetOrCreateContext(); + SurfaceData::SurfaceDataModifierRequestBus::GetOrCreateContext(); + LmbrCentral::ShapeComponentRequestsBus::GetOrCreateContext(); + } + void GradientSignalBaseFixture::SetupCoreSystems() { // Using the AZ::RPI::MakeAssetHandler will both create the asset handlers, diff --git a/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.h b/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.h index 4d2233528b..486cdd6cc0 100644 --- a/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.h +++ b/Gems/GradientSignal/Code/Tests/GradientSignalTestFixtures.h @@ -21,6 +21,7 @@ namespace UnitTest { public: void AddGemsAndComponents() override; + void PostCreateApplication() override; }; #ifdef HAVE_BENCHMARK