FutureTests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/WwiseTask.h"
  16. #include "Wwise/WwiseUnitTests.h"
  17. #if WWISE_UNIT_TESTS
  18. #include "Wwise/WwiseFuture.h"
  19. #include <atomic>
  20. WWISE_TEST_CASE(Concurrency_Future_Smoke, "Wwise::Concurrency::Future_Smoke", "[ApplicationContextMask][SmokeFilter]")
  21. {
  22. SECTION("Static")
  23. {
  24. static_assert(std::is_constructible<TWwisePromise<void>>::value, "Can create a void promise");
  25. static_assert(std::is_constructible<TWwiseFuture<void>>::value, "Can create a void future");
  26. static_assert(std::is_constructible<TWwisePromise<int>>::value, "Can create an int promise");
  27. static_assert(std::is_constructible<TWwiseFuture<int>>::value, "Can create an int future");
  28. static_assert(std::is_constructible<TWwisePromise<int&>>::value, "Can create a reference promise");
  29. static_assert(std::is_constructible<TWwiseFuture<int&>>::value, "Can create a reference future");
  30. static_assert(!std::is_copy_constructible<TWwisePromise<void>>::value, "A promise cannot be copied");
  31. static_assert(std::is_move_constructible<TWwisePromise<void>>::value, "A promise can be moved");
  32. static_assert(!std::is_copy_constructible<TWwiseFuture<void>>::value, "A future cannot be copied");
  33. static_assert(std::is_move_constructible<TWwiseFuture<void>>::value, "A future can be moved");
  34. }
  35. SECTION("Basic Operations")
  36. {
  37. TWwisePromise<void> VoidPromise;
  38. TWwisePromise<int> IntPromise;
  39. TWwisePromise<int&> IntRefPromise;
  40. TWwiseFuture<void> UninitVoidFuture;
  41. TWwiseFuture<int> UninitIntFuture;
  42. TWwiseFuture<int&> UninitIntRefFuture;
  43. TWwiseFuture<void> VoidFuture( VoidPromise.GetFuture() );
  44. TWwiseFuture<int> IntFuture( IntPromise.GetFuture() );
  45. TWwiseFuture<int&> IntRefFuture( IntRefPromise.GetFuture() );
  46. CHECK(!UninitVoidFuture.IsValid());
  47. CHECK(!UninitIntFuture.IsValid());
  48. CHECK(!UninitIntRefFuture.IsValid());
  49. CHECK(VoidFuture.IsValid());
  50. CHECK(IntFuture.IsValid());
  51. CHECK(IntRefFuture.IsValid());
  52. CHECK(!VoidFuture.IsReady());
  53. CHECK(!IntFuture.IsReady());
  54. CHECK(!IntRefFuture.IsReady());
  55. int Value = 1;
  56. VoidPromise.SetValue();
  57. IntPromise.SetValue(1);
  58. IntRefPromise.SetValue(Value);
  59. CHECK(VoidFuture.IsReady());
  60. CHECK(IntFuture.IsReady());
  61. CHECK(IntRefFuture.IsReady());
  62. }
  63. SECTION("Next")
  64. {
  65. bool bDone = false;
  66. TWwisePromise<void> VoidPromise;
  67. auto VoidFuture( VoidPromise.GetFuture() );
  68. auto NewFuture( VoidFuture.Next([&bDone](int)
  69. {
  70. bDone = true;
  71. }));
  72. CHECK(!VoidFuture.IsValid());
  73. CHECK(NewFuture.IsValid());
  74. VoidPromise.EmplaceValue();
  75. CHECK(bDone);
  76. CHECK(NewFuture.IsReady());
  77. }
  78. SECTION("Next Already Emplaced")
  79. {
  80. bool bDone = false;
  81. TWwisePromise<void> VoidPromise;
  82. VoidPromise.EmplaceValue();
  83. auto VoidFuture( VoidPromise.GetFuture() );
  84. auto NewFuture( VoidFuture.Next([&bDone](int)
  85. {
  86. bDone = true;
  87. }));
  88. CHECK(!VoidFuture.IsValid());
  89. CHECK(NewFuture.IsValid());
  90. CHECK(bDone);
  91. CHECK(NewFuture.IsReady());
  92. }
  93. SECTION("Then")
  94. {
  95. bool bDone = false;
  96. TWwisePromise<void> VoidPromise;
  97. auto VoidFuture( VoidPromise.GetFuture() );
  98. auto NewFuture( VoidFuture.Then([&bDone](TWwiseFuture<int> Future) mutable
  99. {
  100. Future.Get();
  101. CHECK(Future.IsReady());
  102. bDone = true;
  103. }));
  104. CHECK(!VoidFuture.IsValid());
  105. CHECK(NewFuture.IsValid());
  106. VoidPromise.EmplaceValue();
  107. CHECK(bDone);
  108. CHECK(NewFuture.IsReady());
  109. }
  110. SECTION("Then Already Emplaced")
  111. {
  112. bool bDone = false;
  113. TWwisePromise<void> VoidPromise;
  114. VoidPromise.EmplaceValue();
  115. auto VoidFuture( VoidPromise.GetFuture() );
  116. auto NewFuture( VoidFuture.Then([&bDone](TWwiseFuture<int> Future) mutable
  117. {
  118. Future.Get();
  119. CHECK(Future.IsReady());
  120. bDone = true;
  121. }));
  122. CHECK(!VoidFuture.IsValid());
  123. CHECK(NewFuture.IsValid());
  124. CHECK(bDone);
  125. CHECK(NewFuture.IsReady());
  126. }
  127. SECTION("Wait Fulfilled")
  128. {
  129. TWwisePromise<void> VoidPromise;
  130. auto VoidFuture( VoidPromise.GetFuture() );
  131. VoidPromise.EmplaceValue();
  132. CHECK(VoidFuture.WaitFor(0));
  133. }
  134. SECTION("Wait Unfulfilled")
  135. {
  136. TWwisePromise<void> VoidPromise;
  137. auto VoidFuture( VoidPromise.GetFuture() );
  138. FFunctionGraphTask::CreateAndDispatchWhenReady([VoidPromise = MoveTemp(VoidPromise)]() mutable
  139. {
  140. VoidPromise.EmplaceValue();
  141. });
  142. CHECK(VoidFuture.WaitFor(FTimespan::FromMilliseconds(10)));
  143. }
  144. SECTION("Wait Not Ready")
  145. {
  146. TWwisePromise<void> VoidPromise;
  147. auto VoidFuture( VoidPromise.GetFuture() );
  148. CHECK(!VoidFuture.WaitFor(0));
  149. VoidPromise.EmplaceValue();
  150. }
  151. }
  152. /*
  153. WWISE_TEST_CASE(Concurrency_Future, "Wwise::Concurrency::Future", "[ApplicationContextMask][ProductFilter]")
  154. {
  155. }
  156. */
  157. /*
  158. WWISE_TEST_CASE(Concurrency_Future_Perf, "Wwise::Concurrency::Future_Perf", "[ApplicationContextMask][PerfFilter]")
  159. {
  160. }
  161. */
  162. void Multithreaded_Then_Future_Op(TWwiseFuture<int>&& Future, FEventRef& Event, int Num)
  163. {
  164. LaunchWwiseTask(WWISE_TEST_ASYNC_NAME, [Future = MoveTemp(Future), &Event, Num]() mutable
  165. {
  166. Future.Get();
  167. if (Num <= 0)
  168. {
  169. Event->Trigger();
  170. return;
  171. }
  172. Future.Then([&Event, Num](TWwiseFuture<int> Future) mutable
  173. {
  174. Multithreaded_Then_Future_Op(MoveTemp(Future), Event, Num - 1);
  175. });
  176. });
  177. };
  178. WWISE_TEST_CASE(Concurrency_Future_Stress, "Wwise::Concurrency::Future_Stress", "[ApplicationContextMask][StressFilter]")
  179. {
  180. SECTION("Multithreaded Then Future")
  181. {
  182. static constexpr const auto Count = 50;
  183. FEventRef Events[Count];
  184. for (int Num = 0; Num < Count; ++Num)
  185. {
  186. TWwisePromise<int> Promise;
  187. auto Future( Promise.GetFuture() );
  188. auto& Event(Events[Num]);
  189. if ( Num & 0 )
  190. {
  191. Promise.EmplaceValue(1);
  192. }
  193. Multithreaded_Then_Future_Op(MoveTemp(Future), Event, Num);
  194. if (!( Num & 0 ))
  195. {
  196. CHECK(! Events[Num]->Wait(1) );
  197. Promise.EmplaceValue(1);
  198. }
  199. }
  200. for (int Num = 0; Num < Count; ++Num)
  201. {
  202. CHECK(Events[Num]->Wait(100));
  203. }
  204. }
  205. SECTION("Multithreaded Promise Wait")
  206. {
  207. static constexpr const auto Count = 2000;
  208. TWwiseFuture<int> Futures[Count];
  209. FEventRef Events[Count];
  210. for (int Num = 0; Num < Count; ++Num)
  211. {
  212. TWwisePromise<int> Promise;
  213. Futures[Num] = Promise.GetFuture();
  214. LaunchWwiseTask(WWISE_TEST_ASYNC_NAME, [Future = &Futures[Num], Event = &Events[Num]]() mutable
  215. {
  216. CHECK(Future->WaitFor(FTimespan::FromMilliseconds(100)));
  217. if (Future->IsReady())
  218. {
  219. CHECK(Future->Get());
  220. }
  221. (*Event)->Trigger();
  222. });
  223. LaunchWwiseTask(WWISE_TEST_ASYNC_NAME, [Promise = MoveTemp(Promise)]() mutable
  224. {
  225. Promise.EmplaceValue(1);
  226. });
  227. }
  228. for (int Num = 0; Num < Count; ++Num)
  229. {
  230. CHECK(Events[Num]->Wait(FTimespan::FromMilliseconds(200)));
  231. }
  232. }
  233. }
  234. #endif // WWISE_UNIT_TESTS