aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/async_semaphore_ut.cpp
blob: 9f8817ea9c1dfaef26411c2d6df03e5c279d563c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "async_semaphore.h"
#include "async.h"

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

#include <util/generic/scope.h>
#include <util/generic/vector.h>
#include <util/thread/pool.h>

using namespace NThreading;

Y_UNIT_TEST_SUITE(TSemaphoreAsync) {
    Y_UNIT_TEST(SimplyAquired) {
        const size_t MAX_IN_PROGRESS = 5;

        TSimpleThreadPool pool(TThreadPool::TParams().SetCatching(false));
        pool.Start(MAX_IN_PROGRESS * 2);

        TVector<TFuture<size_t>> futures;
        auto semaphore = TAsyncSemaphore::Make(MAX_IN_PROGRESS);
        for (size_t i = 0; i < 100; ++i) {
            auto f = semaphore->AcquireAsync()
                .Apply([&pool, i](const auto& f) -> TFuture<size_t> {
                    return Async([i, semaphore = f.GetValue()] {
                        auto guard = semaphore->MakeAutoRelease();
                        Sleep(TDuration::MilliSeconds(100));
                        return i;
                    }, pool);
                });
            futures.push_back(f);
        }

        for (size_t i = 0; i < 100; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(futures[i].GetValueSync(), i);
        }
    }

    Y_UNIT_TEST(AutoReleasedOnException) {
        auto semaphore = TAsyncSemaphore::Make(1);

        auto lock = semaphore->AcquireAsync();
        UNIT_ASSERT(lock.HasValue());
        auto waitingLock = semaphore->AcquireAsync();
        UNIT_ASSERT(!waitingLock.HasValue() && !waitingLock.HasException());

        auto future = lock.Apply([](const auto& f) {
            auto guard = f.GetValue()->MakeAutoRelease();

            ythrow yexception() << "oops";
        });

        UNIT_ASSERT(future.HasException());
        UNIT_ASSERT(waitingLock.HasValue());
    }

    Y_UNIT_TEST(LimitsParallelism) {
        const size_t MAX_IN_PROGRESS = 5;

        TSimpleThreadPool pool(TThreadPool::TParams().SetCatching(false));
        pool.Start(MAX_IN_PROGRESS * 2);

        std::atomic_uint64_t inProgress = 0;

        TVector<TFuture<size_t>> futures;
        auto semaphore = TAsyncSemaphore::Make(MAX_IN_PROGRESS);
        for (size_t i = 0; i < 100; ++i) {
            auto f = semaphore->AcquireAsync()
                .Apply([&, i](const auto&) -> TFuture<size_t> {
                    auto currentInProgress = inProgress.fetch_add(1) + 1;

                    UNIT_ASSERT_GT(currentInProgress, 0);
                    UNIT_ASSERT_LE(currentInProgress, MAX_IN_PROGRESS);

                    return Async([i] {
                        Sleep(TDuration::MilliSeconds(100));
                        return i;
                    }, pool);
                });
            f.IgnoreResult().Subscribe([&](const auto&) {
                auto currentInProgress = inProgress.fetch_sub(1) - 1;

                UNIT_ASSERT_GE(currentInProgress, 0);
                UNIT_ASSERT_LE(currentInProgress, MAX_IN_PROGRESS);

                semaphore->Release();
            });
            futures.push_back(f);
        }

        WaitAll(futures).Wait();

        UNIT_ASSERT_EQUAL(inProgress.load(), 0);
    }

    Y_UNIT_TEST(AcquisitionOrder) {
        const size_t MAX_IN_PROGRESS = 5;

        TSimpleThreadPool pool(TThreadPool::TParams().SetCatching(false));
        pool.Start(MAX_IN_PROGRESS * 2);

        std::atomic_size_t latestId = 0;

        TVector<TFuture<size_t>> futures;
        auto semaphore = TAsyncSemaphore::Make(MAX_IN_PROGRESS);
        for (size_t i = 0; i < 100; ++i) {
            auto f = semaphore->AcquireAsync()
                .Apply([&](const auto& f) -> size_t {
                    auto guard = f.GetValue()->MakeAutoRelease();

                    auto currentId = latestId.fetch_add(1);

                    return currentId;
                });
            futures.push_back(f);
        }

        for (size_t i = 0; i < 100; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(futures[i].GetValueSync(), i);
        }
    }

    Y_UNIT_TEST(Cancel) {
        auto semaphore = TAsyncSemaphore::Make(1);
        auto firstLock = semaphore->AcquireAsync();
        auto canceledLock = semaphore->AcquireAsync();

        UNIT_ASSERT(firstLock.HasValue());

        UNIT_ASSERT(!canceledLock.HasValue());
        UNIT_ASSERT(!canceledLock.HasException());

        semaphore->Cancel();

        UNIT_ASSERT_EXCEPTION(canceledLock.TryRethrow(), TOperationCancelledException);

        UNIT_ASSERT_NO_EXCEPTION(firstLock.GetValue()->Release());
    }

    Y_UNIT_TEST(AcquireAfterCancel) {
        auto semaphore = TAsyncSemaphore::Make(1);

        semaphore->Cancel();

        auto lock = semaphore->AcquireAsync();

        UNIT_ASSERT_EXCEPTION(lock.TryRethrow(), TOperationCancelledException);
    }

    Y_UNIT_TEST(AutoReleaseDeferReleaseReleasesOnException) {
        auto semaphore = TAsyncSemaphore::Make(1);

        auto lock = semaphore->AcquireAsync();
        UNIT_ASSERT(lock.HasValue());
        auto waitingLock = semaphore->AcquireAsync();
        UNIT_ASSERT(!waitingLock.HasValue() && !waitingLock.HasException());

        auto asyncWork = lock.Apply([](const auto& lock) {
            lock.TryRethrow();

            ythrow yexception() << "oops";
        });

        asyncWork.Subscribe(semaphore->MakeAutoRelease().DeferRelease());

        UNIT_ASSERT(asyncWork.HasException());
        UNIT_ASSERT(waitingLock.HasValue());
    }

    Y_UNIT_TEST(AutoReleaseNotCopyable) {
        UNIT_ASSERT(!std::is_copy_constructible_v<TAsyncSemaphore::TAutoRelease>);
        UNIT_ASSERT(!std::is_copy_assignable_v<TAsyncSemaphore::TAutoRelease>);
    }
}