AkComponentCallbackTests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *****************************************************************************/
  15. #include "Wwise/WwiseUnitTests.h"
  16. #if WWISE_UNIT_TESTS && UE_5_1_OR_LATER
  17. #include "AkComponentCallbackManager.h"
  18. #include "Tasks/Task.h"
  19. WWISE_TEST_CASE(AkComponentCallback_Stress, "Audio::Wwise::AkAudio::AkComponentCallback", "[ApplicationContextMask][StressFilter]")
  20. {
  21. struct TestCookie
  22. {
  23. bool AlreadyCancelled = false;
  24. bool bTestPassed = true;
  25. };
  26. SECTION("Safely use (or dispose of) Cookie after FAkComponentCallbackManager::CancelEventCallback")
  27. {
  28. FAkComponentCallbackManager* CallbackManager = FAkComponentCallbackManager::GetInstance();
  29. if (!CallbackManager)
  30. return;
  31. TArray<UE::Tasks::FTask> Tasks = {};
  32. // Joiner tasks makes sure that we trigger the tasks simultaneously
  33. UE::Tasks::FTaskEvent Joiner{UE_SOURCE_LOCATION};
  34. TArray<TestCookie*> CookiesToDelete;
  35. for (int i = 0; i < 1000; i++)
  36. {
  37. TestCookie* Cookie = new TestCookie();
  38. Cookie->AlreadyCancelled = false;
  39. CookiesToDelete.Add(Cookie);
  40. IAkUserEventCallbackPackage* CallbackPackage = CallbackManager->CreateCallbackPackage(
  41. [](AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo)
  42. {
  43. TestCookie* LocalCookie = static_cast<TestCookie*>(in_pCallbackInfo->pCookie);
  44. // If already cancelled, we should not be executing this callback -> test failure
  45. LocalCookie->bTestPassed = !LocalCookie->AlreadyCancelled;
  46. }, Cookie, AK_EndOfEvent, DUMMY_GAMEOBJ, false);
  47. AkEventCallbackInfo CallbackInfo =
  48. {
  49. AkCallbackInfo
  50. {
  51. CallbackPackage,
  52. DUMMY_GAMEOBJ,
  53. },
  54. 1,
  55. 1
  56. };
  57. // Create tasks to try to execute callbacks and cancel them at the same time
  58. UE::Tasks::FTask TaskA = UE::Tasks::Launch(UE_SOURCE_LOCATION,
  59. [CallbackManager, Cookie]
  60. {
  61. CallbackManager->CancelEventCallback(Cookie);
  62. Cookie->AlreadyCancelled = true;
  63. }, Joiner);
  64. UE::Tasks::FTask TaskB = UE::Tasks::Launch(UE_SOURCE_LOCATION,
  65. [CallbackManager, CallbackInfo]
  66. {
  67. CallbackManager->AkComponentCallback(AkCallbackType::AK_EndOfEvent,
  68. const_cast<AkEventCallbackInfo*>(&CallbackInfo));
  69. }, Joiner);
  70. Tasks.Add(TaskA);
  71. Tasks.Add(TaskB);
  72. }
  73. Joiner.Trigger();
  74. Joiner.Wait();
  75. for (auto& Task : Tasks)
  76. {
  77. Task.Wait();
  78. }
  79. bool bTestsPassed = true;
  80. for (auto Cookie : CookiesToDelete)
  81. {
  82. bTestsPassed &= Cookie->bTestPassed;
  83. delete Cookie;
  84. }
  85. CHECK(bTestsPassed)
  86. }
  87. }
  88. #endif