future_ut.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. #include "future.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <list>
  4. #include <type_traits>
  5. namespace NThreading {
  6. namespace {
  7. class TCopyCounter {
  8. public:
  9. TCopyCounter(size_t* numCopies)
  10. : NumCopies(numCopies)
  11. {}
  12. TCopyCounter(const TCopyCounter& that)
  13. : NumCopies(that.NumCopies)
  14. {
  15. ++*NumCopies;
  16. }
  17. TCopyCounter& operator=(const TCopyCounter& that) {
  18. NumCopies = that.NumCopies;
  19. ++*NumCopies;
  20. return *this;
  21. }
  22. TCopyCounter(TCopyCounter&& that) = default;
  23. TCopyCounter& operator=(TCopyCounter&& that) = default;
  24. private:
  25. size_t* NumCopies = nullptr;
  26. };
  27. template <typename T>
  28. auto MakePromise() {
  29. if constexpr (std::is_same_v<T, void>) {
  30. return NewPromise();
  31. }
  32. return NewPromise<T>();
  33. }
  34. template <typename T>
  35. void TestFutureStateId() {
  36. TFuture<T> empty;
  37. UNIT_ASSERT(!empty.StateId().Defined());
  38. auto promise1 = MakePromise<T>();
  39. auto future11 = promise1.GetFuture();
  40. UNIT_ASSERT(future11.StateId().Defined());
  41. auto future12 = promise1.GetFuture();
  42. UNIT_ASSERT_EQUAL(future11.StateId(), future11.StateId()); // same result for subsequent invocations
  43. UNIT_ASSERT_EQUAL(future11.StateId(), future12.StateId()); // same result for different futures with the same state
  44. auto promise2 = MakePromise<T>();
  45. auto future2 = promise2.GetFuture();
  46. UNIT_ASSERT(future2.StateId().Defined());
  47. UNIT_ASSERT_UNEQUAL(future11.StateId(), future2.StateId()); // different results for futures with different states
  48. }
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. Y_UNIT_TEST_SUITE(TFutureTest) {
  52. Y_UNIT_TEST(ShouldInitiallyHasNoValue) {
  53. TPromise<int> promise;
  54. UNIT_ASSERT(!promise.HasValue());
  55. promise = NewPromise<int>();
  56. UNIT_ASSERT(!promise.HasValue());
  57. TFuture<int> future;
  58. UNIT_ASSERT(!future.HasValue());
  59. future = promise.GetFuture();
  60. UNIT_ASSERT(!future.HasValue());
  61. }
  62. Y_UNIT_TEST(ShouldInitiallyHasNoValueVoid) {
  63. TPromise<void> promise;
  64. UNIT_ASSERT(!promise.HasValue());
  65. promise = NewPromise();
  66. UNIT_ASSERT(!promise.HasValue());
  67. TFuture<void> future;
  68. UNIT_ASSERT(!future.HasValue());
  69. future = promise.GetFuture();
  70. UNIT_ASSERT(!future.HasValue());
  71. }
  72. Y_UNIT_TEST(ShouldStoreValue) {
  73. TPromise<int> promise = NewPromise<int>();
  74. promise.SetValue(123);
  75. UNIT_ASSERT(promise.HasValue());
  76. UNIT_ASSERT_EQUAL(promise.GetValue(), 123);
  77. TFuture<int> future = promise.GetFuture();
  78. UNIT_ASSERT(future.HasValue());
  79. UNIT_ASSERT_EQUAL(future.GetValue(), 123);
  80. future = MakeFuture(345);
  81. UNIT_ASSERT(future.HasValue());
  82. UNIT_ASSERT(future.IsReady());
  83. UNIT_ASSERT_EQUAL(future.GetValue(), 345);
  84. }
  85. Y_UNIT_TEST(ShouldStoreValueVoid) {
  86. TPromise<void> promise = NewPromise();
  87. promise.SetValue();
  88. UNIT_ASSERT(promise.HasValue());
  89. TFuture<void> future = promise.GetFuture();
  90. UNIT_ASSERT(future.HasValue());
  91. UNIT_ASSERT(future.IsReady());
  92. future = MakeFuture();
  93. UNIT_ASSERT(future.HasValue());
  94. }
  95. struct TTestCallback {
  96. int Value;
  97. TTestCallback(int value)
  98. : Value(value)
  99. {
  100. }
  101. void Callback(const TFuture<int>& future) {
  102. Value += future.GetValue();
  103. }
  104. int Func(const TFuture<int>& future) {
  105. return (Value += future.GetValue());
  106. }
  107. void VoidFunc(const TFuture<int>& future) {
  108. future.GetValue();
  109. }
  110. TFuture<int> FutureFunc(const TFuture<int>& future) {
  111. return MakeFuture(Value += future.GetValue());
  112. }
  113. TPromise<void> Signal = NewPromise();
  114. TFuture<void> FutureVoidFunc(const TFuture<int>& future) {
  115. future.GetValue();
  116. return Signal;
  117. }
  118. };
  119. Y_UNIT_TEST(ShouldInvokeCallback) {
  120. TPromise<int> promise = NewPromise<int>();
  121. TTestCallback callback(123);
  122. TFuture<int> future = promise.GetFuture()
  123. .Subscribe([&](const TFuture<int>& theFuture) { return callback.Callback(theFuture); });
  124. promise.SetValue(456);
  125. UNIT_ASSERT_EQUAL(future.GetValue(), 456);
  126. UNIT_ASSERT_EQUAL(callback.Value, 123 + 456);
  127. }
  128. Y_UNIT_TEST(ShouldApplyFunc) {
  129. TPromise<int> promise = NewPromise<int>();
  130. TTestCallback callback(123);
  131. TFuture<int> future = promise.GetFuture()
  132. .Apply([&](const auto& theFuture) { return callback.Func(theFuture); });
  133. promise.SetValue(456);
  134. UNIT_ASSERT_EQUAL(future.GetValue(), 123 + 456);
  135. UNIT_ASSERT_EQUAL(callback.Value, 123 + 456);
  136. }
  137. Y_UNIT_TEST(ShouldApplyVoidFunc) {
  138. TPromise<int> promise = NewPromise<int>();
  139. TTestCallback callback(123);
  140. TFuture<void> future = promise.GetFuture()
  141. .Apply([&](const auto& theFuture) { return callback.VoidFunc(theFuture); });
  142. promise.SetValue(456);
  143. UNIT_ASSERT(future.HasValue());
  144. }
  145. Y_UNIT_TEST(ShouldApplyFutureFunc) {
  146. TPromise<int> promise = NewPromise<int>();
  147. TTestCallback callback(123);
  148. TFuture<int> future = promise.GetFuture()
  149. .Apply([&](const auto& theFuture) { return callback.FutureFunc(theFuture); });
  150. promise.SetValue(456);
  151. UNIT_ASSERT_EQUAL(future.GetValue(), 123 + 456);
  152. UNIT_ASSERT_EQUAL(callback.Value, 123 + 456);
  153. }
  154. Y_UNIT_TEST(ShouldApplyFutureVoidFunc) {
  155. TPromise<int> promise = NewPromise<int>();
  156. TTestCallback callback(123);
  157. TFuture<void> future = promise.GetFuture()
  158. .Apply([&](const auto& theFuture) { return callback.FutureVoidFunc(theFuture); });
  159. promise.SetValue(456);
  160. UNIT_ASSERT(!future.HasValue());
  161. callback.Signal.SetValue();
  162. UNIT_ASSERT(future.HasValue());
  163. }
  164. Y_UNIT_TEST(ShouldIgnoreResultIfAsked) {
  165. TPromise<int> promise = NewPromise<int>();
  166. TTestCallback callback(123);
  167. TFuture<int> future = promise.GetFuture().IgnoreResult().Return(42);
  168. promise.SetValue(456);
  169. UNIT_ASSERT_EQUAL(future.GetValue(), 42);
  170. }
  171. class TCustomException: public yexception {
  172. };
  173. Y_UNIT_TEST(ShouldRethrowException) {
  174. TPromise<int> promise = NewPromise<int>();
  175. try {
  176. ythrow TCustomException();
  177. } catch (...) {
  178. promise.SetException(std::current_exception());
  179. }
  180. UNIT_ASSERT(!promise.HasValue());
  181. UNIT_ASSERT(promise.HasException());
  182. UNIT_ASSERT_EXCEPTION(promise.GetValue(), TCustomException);
  183. UNIT_ASSERT_EXCEPTION(promise.TryRethrow(), TCustomException);
  184. }
  185. Y_UNIT_TEST(ShouldRethrowCallbackException) {
  186. TPromise<int> promise = NewPromise<int>();
  187. TFuture<int> future = promise.GetFuture();
  188. future.Subscribe([](const TFuture<int>&) {
  189. throw TCustomException();
  190. });
  191. UNIT_ASSERT_EXCEPTION(promise.SetValue(123), TCustomException);
  192. }
  193. Y_UNIT_TEST(ShouldRethrowCallbackExceptionIgnoreResult) {
  194. TPromise<int> promise = NewPromise<int>();
  195. TFuture<void> future = promise.GetFuture().IgnoreResult();
  196. future.Subscribe([](const TFuture<void>&) {
  197. throw TCustomException();
  198. });
  199. UNIT_ASSERT_EXCEPTION(promise.SetValue(123), TCustomException);
  200. }
  201. Y_UNIT_TEST(ShouldWaitExceptionOrAll) {
  202. TPromise<void> promise1 = NewPromise();
  203. TPromise<void> promise2 = NewPromise();
  204. TFuture<void> future = WaitExceptionOrAll(promise1, promise2);
  205. UNIT_ASSERT(!future.HasValue());
  206. promise1.SetValue();
  207. UNIT_ASSERT(!future.HasValue());
  208. promise2.SetValue();
  209. UNIT_ASSERT(future.HasValue());
  210. }
  211. Y_UNIT_TEST(ShouldWaitExceptionOrAllVector) {
  212. TPromise<void> promise1 = NewPromise();
  213. TPromise<void> promise2 = NewPromise();
  214. TVector<TFuture<void>> promises;
  215. promises.push_back(promise1);
  216. promises.push_back(promise2);
  217. TFuture<void> future = WaitExceptionOrAll(promises);
  218. UNIT_ASSERT(!future.HasValue());
  219. promise1.SetValue();
  220. UNIT_ASSERT(!future.HasValue());
  221. promise2.SetValue();
  222. UNIT_ASSERT(future.HasValue());
  223. }
  224. Y_UNIT_TEST(ShouldWaitExceptionOrAllVectorWithValueType) {
  225. TPromise<int> promise1 = NewPromise<int>();
  226. TPromise<int> promise2 = NewPromise<int>();
  227. TVector<TFuture<int>> promises;
  228. promises.push_back(promise1);
  229. promises.push_back(promise2);
  230. TFuture<void> future = WaitExceptionOrAll(promises);
  231. UNIT_ASSERT(!future.HasValue());
  232. promise1.SetValue(0);
  233. UNIT_ASSERT(!future.HasValue());
  234. promise2.SetValue(0);
  235. UNIT_ASSERT(future.HasValue());
  236. }
  237. Y_UNIT_TEST(ShouldWaitExceptionOrAllList) {
  238. TPromise<void> promise1 = NewPromise();
  239. TPromise<void> promise2 = NewPromise();
  240. std::list<TFuture<void>> promises;
  241. promises.push_back(promise1);
  242. promises.push_back(promise2);
  243. TFuture<void> future = WaitExceptionOrAll(promises);
  244. UNIT_ASSERT(!future.HasValue());
  245. promise1.SetValue();
  246. UNIT_ASSERT(!future.HasValue());
  247. promise2.SetValue();
  248. UNIT_ASSERT(future.HasValue());
  249. }
  250. Y_UNIT_TEST(ShouldWaitExceptionOrAllVectorEmpty) {
  251. TVector<TFuture<void>> promises;
  252. TFuture<void> future = WaitExceptionOrAll(promises);
  253. UNIT_ASSERT(future.HasValue());
  254. }
  255. Y_UNIT_TEST(ShouldWaitAnyVector) {
  256. TPromise<void> promise1 = NewPromise();
  257. TPromise<void> promise2 = NewPromise();
  258. TVector<TFuture<void>> promises;
  259. promises.push_back(promise1);
  260. promises.push_back(promise2);
  261. TFuture<void> future = WaitAny(promises);
  262. UNIT_ASSERT(!future.HasValue());
  263. promise1.SetValue();
  264. UNIT_ASSERT(future.HasValue());
  265. promise2.SetValue();
  266. UNIT_ASSERT(future.HasValue());
  267. }
  268. Y_UNIT_TEST(ShouldWaitAnyVectorWithValueType) {
  269. TPromise<int> promise1 = NewPromise<int>();
  270. TPromise<int> promise2 = NewPromise<int>();
  271. TVector<TFuture<int>> promises;
  272. promises.push_back(promise1);
  273. promises.push_back(promise2);
  274. TFuture<void> future = WaitAny(promises);
  275. UNIT_ASSERT(!future.HasValue());
  276. promise1.SetValue(0);
  277. UNIT_ASSERT(future.HasValue());
  278. promise2.SetValue(0);
  279. UNIT_ASSERT(future.HasValue());
  280. }
  281. Y_UNIT_TEST(ShouldWaitAnyList) {
  282. TPromise<void> promise1 = NewPromise();
  283. TPromise<void> promise2 = NewPromise();
  284. std::list<TFuture<void>> promises;
  285. promises.push_back(promise1);
  286. promises.push_back(promise2);
  287. TFuture<void> future = WaitAny(promises);
  288. UNIT_ASSERT(!future.HasValue());
  289. promise1.SetValue();
  290. UNIT_ASSERT(future.HasValue());
  291. promise2.SetValue();
  292. UNIT_ASSERT(future.HasValue());
  293. }
  294. Y_UNIT_TEST(ShouldWaitAnyVectorEmpty) {
  295. TVector<TFuture<void>> promises;
  296. TFuture<void> future = WaitAny(promises);
  297. UNIT_ASSERT(future.HasValue());
  298. }
  299. Y_UNIT_TEST(ShouldWaitAny) {
  300. TPromise<void> promise1 = NewPromise();
  301. TPromise<void> promise2 = NewPromise();
  302. TFuture<void> future = WaitAny(promise1, promise2);
  303. UNIT_ASSERT(!future.HasValue());
  304. promise1.SetValue();
  305. UNIT_ASSERT(future.HasValue());
  306. promise2.SetValue();
  307. UNIT_ASSERT(future.HasValue());
  308. }
  309. Y_UNIT_TEST(ShouldStoreTypesWithoutDefaultConstructor) {
  310. // compileability test
  311. struct TRec {
  312. explicit TRec(int) {
  313. }
  314. };
  315. auto promise = NewPromise<TRec>();
  316. promise.SetValue(TRec(1));
  317. auto future = MakeFuture(TRec(1));
  318. const auto& rec = future.GetValue();
  319. Y_UNUSED(rec);
  320. }
  321. Y_UNIT_TEST(ShouldStoreMovableTypes) {
  322. // compileability test
  323. struct TRec : TMoveOnly {
  324. explicit TRec(int) {
  325. }
  326. };
  327. auto promise = NewPromise<TRec>();
  328. promise.SetValue(TRec(1));
  329. auto future = MakeFuture(TRec(1));
  330. const auto& rec = future.GetValue();
  331. Y_UNUSED(rec);
  332. }
  333. Y_UNIT_TEST(ShouldMoveMovableTypes) {
  334. // compileability test
  335. struct TRec : TMoveOnly {
  336. explicit TRec(int) {
  337. }
  338. };
  339. auto promise = NewPromise<TRec>();
  340. promise.SetValue(TRec(1));
  341. auto future = MakeFuture(TRec(1));
  342. auto rec = future.ExtractValue();
  343. Y_UNUSED(rec);
  344. }
  345. Y_UNIT_TEST(ShouldNotExtractAfterGet) {
  346. TPromise<int> promise = NewPromise<int>();
  347. promise.SetValue(123);
  348. UNIT_ASSERT(promise.HasValue());
  349. UNIT_ASSERT_EQUAL(promise.GetValue(), 123);
  350. UNIT_CHECK_GENERATED_EXCEPTION(promise.ExtractValue(), TFutureException);
  351. }
  352. Y_UNIT_TEST(ShouldNotGetAfterExtract) {
  353. TPromise<int> promise = NewPromise<int>();
  354. promise.SetValue(123);
  355. UNIT_ASSERT(promise.HasValue());
  356. UNIT_ASSERT_EQUAL(promise.ExtractValue(), 123);
  357. UNIT_CHECK_GENERATED_EXCEPTION(promise.GetValue(), TFutureException);
  358. }
  359. Y_UNIT_TEST(ShouldNotExtractAfterExtract) {
  360. TPromise<int> promise = NewPromise<int>();
  361. promise.SetValue(123);
  362. UNIT_ASSERT(promise.HasValue());
  363. UNIT_ASSERT_EQUAL(promise.ExtractValue(), 123);
  364. UNIT_CHECK_GENERATED_EXCEPTION(promise.ExtractValue(), TFutureException);
  365. }
  366. Y_UNIT_TEST(ShouldNotExtractFromSharedDefault) {
  367. UNIT_CHECK_GENERATED_EXCEPTION(MakeFuture<int>().ExtractValue(), TFutureException);
  368. struct TStorage {
  369. TString String = TString(100, 'a');
  370. };
  371. try {
  372. TString s = MakeFuture<TStorage>().ExtractValue().String;
  373. Y_UNUSED(s);
  374. } catch (TFutureException) {
  375. // pass
  376. }
  377. UNIT_ASSERT_VALUES_EQUAL(MakeFuture<TStorage>().GetValue().String, TString(100, 'a'));
  378. }
  379. Y_UNIT_TEST(HandlingRepetitiveSet) {
  380. TPromise<int> promise = NewPromise<int>();
  381. promise.SetValue(42);
  382. UNIT_CHECK_GENERATED_EXCEPTION(promise.SetValue(42), TFutureException);
  383. }
  384. Y_UNIT_TEST(HandlingRepetitiveTrySet) {
  385. TPromise<int> promise = NewPromise<int>();
  386. UNIT_ASSERT(promise.TrySetValue(42));
  387. UNIT_ASSERT(!promise.TrySetValue(42));
  388. }
  389. Y_UNIT_TEST(HandlingRepetitiveSetException) {
  390. TPromise<int> promise = NewPromise<int>();
  391. promise.SetException("test");
  392. UNIT_CHECK_GENERATED_EXCEPTION(promise.SetException("test"), TFutureException);
  393. }
  394. Y_UNIT_TEST(HandlingRepetitiveTrySetException) {
  395. TPromise<int> promise = NewPromise<int>();
  396. UNIT_ASSERT(promise.TrySetException(std::make_exception_ptr("test")));
  397. UNIT_ASSERT(!promise.TrySetException(std::make_exception_ptr("test")));
  398. }
  399. Y_UNIT_TEST(ShouldAllowToMakeFutureWithException)
  400. {
  401. auto future1 = MakeErrorFuture<void>(std::make_exception_ptr(TFutureException()));
  402. UNIT_ASSERT(future1.HasException());
  403. UNIT_ASSERT(future1.IsReady());
  404. UNIT_CHECK_GENERATED_EXCEPTION(future1.GetValue(), TFutureException);
  405. auto future2 = MakeErrorFuture<int>(std::make_exception_ptr(TFutureException()));
  406. UNIT_ASSERT(future2.HasException());
  407. UNIT_CHECK_GENERATED_EXCEPTION(future2.GetValue(), TFutureException);
  408. auto future3 = MakeFuture<std::exception_ptr>(std::make_exception_ptr(TFutureException()));
  409. UNIT_ASSERT(future3.HasValue());
  410. UNIT_CHECK_GENERATED_NO_EXCEPTION(future3.GetValue(), TFutureException);
  411. auto future4 = MakeFuture<std::unique_ptr<int>>(nullptr);
  412. UNIT_ASSERT(future4.HasValue());
  413. UNIT_CHECK_GENERATED_NO_EXCEPTION(future4.GetValue(), TFutureException);
  414. }
  415. Y_UNIT_TEST(WaitAllowsExtract) {
  416. auto future = MakeFuture<int>(42);
  417. TVector vec{future, future, future};
  418. WaitExceptionOrAll(vec).GetValue();
  419. WaitAny(vec).GetValue();
  420. UNIT_ASSERT_EQUAL(future.ExtractValue(), 42);
  421. }
  422. Y_UNIT_TEST(IgnoreAllowsExtract) {
  423. auto future = MakeFuture<int>(42);
  424. future.IgnoreResult().GetValue();
  425. UNIT_ASSERT_EQUAL(future.ExtractValue(), 42);
  426. }
  427. Y_UNIT_TEST(WaitExceptionOrAllException) {
  428. auto promise1 = NewPromise();
  429. auto promise2 = NewPromise();
  430. auto future1 = promise1.GetFuture();
  431. auto future2 = promise2.GetFuture();
  432. auto wait = WaitExceptionOrAll(future1, future2);
  433. promise2.SetException("foo-exception");
  434. wait.Wait();
  435. UNIT_ASSERT(future2.HasException());
  436. UNIT_ASSERT(!future1.IsReady());
  437. UNIT_ASSERT(!future1.HasValue() && !future1.HasException());
  438. }
  439. Y_UNIT_TEST(WaitAllException) {
  440. auto promise1 = NewPromise();
  441. auto promise2 = NewPromise();
  442. auto future1 = promise1.GetFuture();
  443. auto future2 = promise2.GetFuture();
  444. auto wait = WaitAll(future1, future2);
  445. promise2.SetException("foo-exception");
  446. UNIT_ASSERT(!wait.HasValue() && !wait.HasException());
  447. promise1.SetValue();
  448. UNIT_ASSERT_EXCEPTION_CONTAINS(wait.GetValueSync(), yexception, "foo-exception");
  449. }
  450. Y_UNIT_TEST(FutureStateId) {
  451. TestFutureStateId<void>();
  452. TestFutureStateId<int>();
  453. }
  454. template <typename T>
  455. void TestApplyNoRvalueCopyImpl() {
  456. size_t numCopies = 0;
  457. TCopyCounter copyCounter(&numCopies);
  458. auto promise = MakePromise<T>();
  459. const auto future = promise.GetFuture().Apply(
  460. [copyCounter = std::move(copyCounter)] (const auto&) {}
  461. );
  462. if constexpr (std::is_same_v<T, void>) {
  463. promise.SetValue();
  464. } else {
  465. promise.SetValue(T());
  466. }
  467. future.GetValueSync();
  468. UNIT_ASSERT_VALUES_EQUAL(numCopies, 0);
  469. }
  470. Y_UNIT_TEST(ApplyNoRvalueCopy) {
  471. TestApplyNoRvalueCopyImpl<void>();
  472. TestApplyNoRvalueCopyImpl<int>();
  473. }
  474. template <typename T>
  475. void TestApplyLvalueCopyImpl() {
  476. size_t numCopies = 0;
  477. TCopyCounter copyCounter(&numCopies);
  478. auto promise = MakePromise<T>();
  479. auto func = [copyCounter = std::move(copyCounter)] (const auto&) {};
  480. const auto future = promise.GetFuture().Apply(func);
  481. if constexpr (std::is_same_v<T, void>) {
  482. promise.SetValue();
  483. } else {
  484. promise.SetValue(T());
  485. }
  486. future.GetValueSync();
  487. UNIT_ASSERT_VALUES_EQUAL(numCopies, 1);
  488. }
  489. Y_UNIT_TEST(ApplyLvalueCopy) {
  490. TestApplyLvalueCopyImpl<void>();
  491. TestApplyLvalueCopyImpl<int>();
  492. }
  493. Y_UNIT_TEST(ReturnForwardingTypeDeduction) {
  494. const TString e = TString(80, 'a');
  495. TString l = TString(80, 'a');
  496. TFuture<TString> futureL = MakeFuture().Return(l);
  497. UNIT_ASSERT_VALUES_EQUAL(futureL.GetValue(), e);
  498. UNIT_ASSERT_VALUES_EQUAL(l, e);
  499. TFuture<TString> futureR = MakeFuture().Return(std::move(l));
  500. UNIT_ASSERT_VALUES_EQUAL(futureR.GetValue(), e);
  501. }
  502. Y_UNIT_TEST(ReturnForwardingCopiesCount) {
  503. size_t numCopies = 0;
  504. TCopyCounter copyCounter(&numCopies);
  505. auto returnedCounter = MakeFuture().Return(std::move(copyCounter)).ExtractValueSync();
  506. Y_DO_NOT_OPTIMIZE_AWAY(returnedCounter);
  507. UNIT_ASSERT_VALUES_EQUAL(numCopies, 0);
  508. }
  509. }
  510. }