aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/async_ut.cpp
blob: 6901ed26212b9c26a2581cdf92a23d9e53ca9ec1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "async.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/ptr.h>
#include <util/generic/vector.h>

namespace {
    struct TMySuperTaskQueue {
    };

}

namespace NThreading {
    /* Here we provide an Async overload for TMySuperTaskQueue indide NThreading namespace
 * so that we can call it in the way
 *
 * TMySuperTaskQueue queue;
 * NThreading::Async([](){}, queue);
 *
 * See also ExtensionExample unittest.
 */
    template <typename Func>
    TFuture<TFunctionResult<Func>> Async(Func func, TMySuperTaskQueue&) {
        return MakeFuture(func());
    }

}

Y_UNIT_TEST_SUITE(Async) {
    Y_UNIT_TEST(ExtensionExample) {
        TMySuperTaskQueue queue;
        auto future = NThreading::Async([]() { return 5; }, queue);
        future.Wait();
        UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5);
    }

    Y_UNIT_TEST(WorksWithIMtpQueue) {
        auto queue = MakeHolder<TThreadPool>(); 
        queue->Start(1);

        auto future = NThreading::Async([]() { return 5; }, *queue);
        future.Wait();
        UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5);
    }

    Y_UNIT_TEST(ProperlyDeducesFutureType) {
        // Compileability test
        auto queue = CreateThreadPool(1); 

        NThreading::TFuture<void> f1 = NThreading::Async([]() {}, *queue);
        NThreading::TFuture<int> f2 = NThreading::Async([]() { return 5; }, *queue);
        NThreading::TFuture<double> f3 = NThreading::Async([]() { return 5.0; }, *queue);
        NThreading::TFuture<TVector<int>> f4 = NThreading::Async([]() { return TVector<int>(); }, *queue);
        NThreading::TFuture<int> f5 = NThreading::Async([]() { return NThreading::MakeFuture(5); }, *queue);
    }
}