async_ut.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "async.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/ptr.h>
  4. #include <util/generic/vector.h>
  5. namespace {
  6. struct TMySuperTaskQueue {
  7. };
  8. }
  9. namespace NThreading {
  10. /* Here we provide an Async overload for TMySuperTaskQueue indide NThreading namespace
  11. * so that we can call it in the way
  12. *
  13. * TMySuperTaskQueue queue;
  14. * NThreading::Async([](){}, queue);
  15. *
  16. * See also ExtensionExample unittest.
  17. */
  18. template <typename Func>
  19. TFuture<TFunctionResult<Func>> Async(Func func, TMySuperTaskQueue&) {
  20. return MakeFuture(func());
  21. }
  22. }
  23. Y_UNIT_TEST_SUITE(Async) {
  24. Y_UNIT_TEST(ExtensionExample) {
  25. TMySuperTaskQueue queue;
  26. auto future = NThreading::Async([]() { return 5; }, queue);
  27. future.Wait();
  28. UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5);
  29. }
  30. Y_UNIT_TEST(WorksWithIMtpQueue) {
  31. auto queue = MakeHolder<TThreadPool>();
  32. queue->Start(1);
  33. auto future = NThreading::Async([]() { return 5; }, *queue);
  34. future.Wait();
  35. UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5);
  36. }
  37. Y_UNIT_TEST(ProperlyDeducesFutureType) {
  38. // Compileability test
  39. auto queue = CreateThreadPool(1);
  40. NThreading::TFuture<void> f1 = NThreading::Async([]() {}, *queue);
  41. NThreading::TFuture<int> f2 = NThreading::Async([]() { return 5; }, *queue);
  42. NThreading::TFuture<double> f3 = NThreading::Async([]() { return 5.0; }, *queue);
  43. NThreading::TFuture<TVector<int>> f4 = NThreading::Async([]() { return TVector<int>(); }, *queue);
  44. NThreading::TFuture<int> f5 = NThreading::Async([]() { return NThreading::MakeFuture(5); }, *queue);
  45. }
  46. }