| 9 | |
| 10 | |
| 11 | TEST(TestFutureTraits, Simple) { |
| 12 | TVector<TString> result; |
| 13 | |
| 14 | auto coroutine1 = [&result]() -> NThreading::TFuture<size_t> { |
| 15 | result.push_back("coroutine1"); |
| 16 | co_return 1; |
| 17 | }; |
| 18 | |
| 19 | NThreading::TPromise<size_t> coroutine2SuspendPromise = NThreading::NewPromise<size_t>(); |
| 20 | auto coroutine2 = [&result, coroutine2SuspendFuture = coroutine2SuspendPromise.GetFuture()]() -> NThreading::TFuture<size_t> { |
| 21 | result.push_back("coroutine2"); |
| 22 | |
| 23 | result.push_back("pre_coroutine2_suspend_future"); |
| 24 | size_t futureResult = co_await coroutine2SuspendFuture; |
| 25 | result.push_back("post_coroutine2_suspend_future"); |
| 26 | |
| 27 | co_return 2 + futureResult; |
| 28 | }; |
| 29 | |
| 30 | auto coroutineAll = [&]() -> NThreading::TFuture<size_t> { |
| 31 | Y_DEFER { |
| 32 | result.push_back("coroutine_all_destroy"); |
| 33 | }; |
| 34 | |
| 35 | result.push_back("pre_coroutine1"); |
| 36 | size_t coroutine1Res = co_await coroutine1(); |
| 37 | result.push_back("post_coroutine1"); |
| 38 | |
| 39 | result.push_back("pre_coroutine2"); |
| 40 | size_t coroutine2Res = co_await coroutine2(); |
| 41 | result.push_back("post_coroutine2"); |
| 42 | |
| 43 | co_return coroutine1Res + coroutine2Res; |
| 44 | }; |
| 45 | |
| 46 | NThreading::TFuture<size_t> coroutineAllFuture = coroutineAll(); |
| 47 | EXPECT_FALSE(coroutineAllFuture.HasValue()); |
| 48 | EXPECT_FALSE(coroutineAllFuture.HasException()); |
| 49 | EXPECT_THAT( |
| 50 | result, |
| 51 | ::testing::ContainerEq( |
| 52 | TVector<TString>({ |
| 53 | "pre_coroutine1", |
| 54 | "coroutine1", |
| 55 | "post_coroutine1", |
| 56 | |
| 57 | "pre_coroutine2", |
| 58 | "coroutine2", |
| 59 | "pre_coroutine2_suspend_future" |
| 60 | }) |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | coroutine2SuspendPromise.SetValue(3u); |
| 65 | EXPECT_TRUE(coroutineAllFuture.HasValue()); |
| 66 | EXPECT_EQ(coroutineAllFuture.GetValue(), 6u); |
| 67 | EXPECT_THAT( |
| 68 | result, |
no test coverage detected