aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/multi_resource_lock_ut.cpp
blob: 0af9cea3fff60e6b876be02b9f4a1365191aecc4 (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
#include "multi_resource_lock.h"
#include <util/generic/xrange.h>
#include <library/cpp/threading/future/async.h>
#include <library/cpp/testing/unittest/registar.h>

namespace NYql {
using namespace NThreading;

Y_UNIT_TEST_SUITE(TMultiResourceLock) {
    Y_UNIT_TEST(ManyResources) {
        TMultiResourceLock multiLock;
        const int workersCount = 3;
        TVector<TVector<int>> workersData;
        workersData.resize(workersCount);

        TAdaptiveThreadPool queue;
        queue.Start(0);

        TVector<NThreading::TFuture<void>> workers;
        workers.reserve(workersCount);
        TManualEvent startEvent;

        for (int i = 0; i < workersCount; ++i) {
            TString resourceId = ToString(i);
            TVector<int>& data = workersData.at(i);
            NThreading::TFuture<void> f = NThreading::Async([&, resourceId]() {
                startEvent.Wait();

                for (int j = 0; j < 1000; ++j) {
                    const auto& l = multiLock.Acquire(resourceId);
                    Y_UNUSED(l);
                    data.push_back(j);
                }
            }, queue);

            workers.push_back(std::move(f));
        }

        startEvent.Signal();

        NThreading::TFuture<void> all = NThreading::WaitExceptionOrAll(workers);
        all.GetValueSync();
        queue.Stop();

        // analyze workersData:
        auto range0_999 = xrange(0, 1000);
        for (auto& w : workersData) {
            UNIT_ASSERT_VALUES_EQUAL(w.size(), 1000);
            UNIT_ASSERT(std::equal(range0_999.begin(), range0_999.end(), w.begin()));
        }
    }
}

}