AWSMetricsGemMock.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Jobs/JobManager.h>
  10. #include <AzCore/Jobs/JobManagerBus.h>
  11. #include <AzCore/Jobs/JobContext.h>
  12. #include <AzCore/Memory/PoolAllocator.h>
  13. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  14. #include <AzCore/Serialization/Json/RegistrationContext.h>
  15. #include <AzCore/Settings/SettingsRegistryImpl.h>
  16. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  17. #include <AzCore/UnitTest/TestTypes.h>
  18. #include <AzCore/Utils/Utils.h>
  19. #include <AzFramework/IO/LocalFileIO.h>
  20. namespace AWSMetrics
  21. {
  22. class AWSMetricsGemAllocatorFixture
  23. : public UnitTest::LeakDetectionFixture
  24. {
  25. protected:
  26. void SetUp() override
  27. {
  28. UnitTest::LeakDetectionFixture::SetUp();
  29. // Set up the file IO and alias
  30. m_localFileIO = aznew AZ::IO::LocalFileIO();
  31. m_priorFileIO = AZ::IO::FileIOBase::GetInstance();
  32. // we need to set it to nullptr first because otherwise the
  33. // underneath code assumes that we might be leaking the previous instance
  34. AZ::IO::FileIOBase::SetInstance(nullptr);
  35. AZ::IO::FileIOBase::SetInstance(m_localFileIO);
  36. const AZ::IO::Path engineRoot = AZ::Test::GetEngineRootPath();
  37. const auto productAssetPath = GetTestFolderPath() / "Cache";
  38. const auto userPath = GetTestFolderPath() / "user";
  39. m_localFileIO->CreatePath(productAssetPath.c_str());
  40. m_localFileIO->CreatePath(userPath.c_str());
  41. m_localFileIO->SetAlias("@engroot@", engineRoot.c_str());
  42. m_localFileIO->SetAlias("@products@", productAssetPath.c_str());
  43. m_localFileIO->SetAlias("@user@", userPath.c_str());
  44. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  45. m_registrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  46. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  47. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  48. m_settingsRegistry->SetContext(m_serializeContext.get());
  49. m_settingsRegistry->SetContext(m_registrationContext.get());
  50. m_settingsRegistry->Set(AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder, engineRoot.c_str());
  51. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  52. }
  53. void TearDown() override
  54. {
  55. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  56. m_registrationContext->EnableRemoveReflection();
  57. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  58. m_registrationContext->DisableRemoveReflection();
  59. m_settingsRegistry.reset();
  60. m_serializeContext.reset();
  61. m_registrationContext.reset();
  62. const auto productAssetPath = GetTestFolderPath() / "Cache";
  63. const auto userPath = GetTestFolderPath() / "user";
  64. // Clear the product asset cache alias to prevent cache write errors
  65. m_localFileIO->ClearAlias("@products@");
  66. m_localFileIO->DestroyPath(userPath.c_str());
  67. m_localFileIO->DestroyPath(productAssetPath.c_str());
  68. AZ::IO::FileIOBase::SetInstance(nullptr);
  69. delete m_localFileIO;
  70. AZ::IO::FileIOBase::SetInstance(m_priorFileIO);
  71. UnitTest::LeakDetectionFixture::TearDown();
  72. }
  73. AZStd::string CreateClientConfigFile(bool offlineRecordingEnabled, double maxQueueSizeInMb, int queueFlushPeriodInSeconds, int MaxNumRetries)
  74. {
  75. AZStd::string offlineRecordingEnabledStr = offlineRecordingEnabled ? "true" : "false";
  76. AZStd::string configFilePath = GetDefaultTestFilePath();
  77. AZStd::string settings = AZStd::string::format("{\"Amazon\":{\"Gems\":{\"AWSMetrics\":{\"OfflineRecording\":%s,\"MaxQueueSizeInMb\":%f,\"QueueFlushPeriodInSeconds\":%d,\"MaxNumRetries\":%d}}}}",
  78. offlineRecordingEnabledStr.c_str(),
  79. maxQueueSizeInMb,
  80. queueFlushPeriodInSeconds,
  81. MaxNumRetries);
  82. CreateFile(configFilePath, settings);
  83. return configFilePath;
  84. }
  85. bool CreateFile(const AZStd::string& filePath, const AZStd::string& content)
  86. {
  87. AZ::IO::HandleType fileHandle;
  88. // Suppress errors about writing to product asset cache
  89. AZ_TEST_START_TRACE_SUPPRESSION;
  90. if (!m_localFileIO->Open(filePath.c_str(), AZ::IO::OpenMode::ModeWrite | AZ::IO::OpenMode::ModeText, fileHandle))
  91. {
  92. return false;
  93. }
  94. m_localFileIO->Write(fileHandle, content.c_str(), content.size());
  95. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  96. m_localFileIO->Close(fileHandle);
  97. return true;
  98. }
  99. AZStd::string GetDefaultTestFilePath()
  100. {
  101. return (GetTestFolderPath() / "Test.json").Native();
  102. }
  103. bool RemoveFile(const AZStd::string& filePath)
  104. {
  105. if (m_localFileIO->Exists(filePath.c_str()))
  106. {
  107. return m_localFileIO->Remove(filePath.c_str());
  108. }
  109. return true;
  110. }
  111. AZ::IO::FileIOBase* m_priorFileIO = nullptr;
  112. AZ::IO::FileIOBase* m_localFileIO = nullptr;
  113. AZ::Test::ScopedAutoTempDirectory m_testDirectory;
  114. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  115. AZStd::unique_ptr<AZ::JsonRegistrationContext> m_registrationContext;
  116. AZStd::unique_ptr<AZ::SettingsRegistryImpl> m_settingsRegistry;
  117. private:
  118. AZ::IO::Path GetTestFolderPath()
  119. {
  120. AZ::IO::Path testPathString{ m_testDirectory.GetDirectory() };
  121. return testPathString;
  122. }
  123. };
  124. }