aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/guard_ut.cpp
blob: d3501cba4e6d5eb31298cc9479ef7cbf0907953d (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
175
176
177
178
179
180
#include "guard.h" 
#include "rwlock.h"
 
#include <library/cpp/testing/unittest/registar.h>

#include <util/thread/pool.h>

struct TTestGuard: public TTestBase { 
    UNIT_TEST_SUITE(TTestGuard);
    UNIT_TEST(TestGuard) 
    UNIT_TEST(TestTryGuard) 
    UNIT_TEST(TestMove) 
    UNIT_TEST(TestSync) 
    UNIT_TEST(TestUnguard) 
    UNIT_TEST(TestTryReadGuard)
    UNIT_TEST(TestWithLock)
    UNIT_TEST(TestWithLockScope);
    UNIT_TEST_SUITE_END();

    struct TGuardChecker {
        TGuardChecker()
            : guarded(false)
        {
        }

        void Acquire() {
            guarded = true;
        }
        void Release() {
            guarded = false;
        }
        bool TryAcquire() {
            if (guarded) {
                return false;
            } else {
                guarded = true;
                return true;
            }
        }

        bool guarded;
    };

    void TestUnguard() { 
        TGuardChecker m; 
 
        { 
            auto guard = Guard(m); 
 
            UNIT_ASSERT(m.guarded); 
 
            { 
                auto unguard = Unguard(guard); 
 
                UNIT_ASSERT(!m.guarded); 
            } 
 
            UNIT_ASSERT(m.guarded); 
        } 
 
        { 
            auto guard = Guard(m); 
 
            UNIT_ASSERT(m.guarded); 
 
            { 
                auto unguard = Unguard(m); 
 
                UNIT_ASSERT(!m.guarded); 
            } 
 
            UNIT_ASSERT(m.guarded); 
        } 
    } 
 
    void TestMove() { 
        TGuardChecker m; 
        size_t n = 0; 
 
        { 
            auto guard = Guard(m); 
 
            UNIT_ASSERT(m.guarded); 
            ++n; 
        } 
 
        UNIT_ASSERT(!m.guarded); 
        UNIT_ASSERT_VALUES_EQUAL(n, 1); 
    } 
 
    void TestSync() { 
        TGuardChecker m; 
        size_t n = 0; 
 
        with_lock (m) { 
            UNIT_ASSERT(m.guarded); 
            ++n; 
        } 
 
        UNIT_ASSERT(!m.guarded); 
        UNIT_ASSERT_VALUES_EQUAL(n, 1); 
    } 
 
    void TestGuard() {
        TGuardChecker checker;

        UNIT_ASSERT(!checker.guarded);
        {
            TGuard<TGuardChecker> guard(checker);
            UNIT_ASSERT(checker.guarded);
        }
        UNIT_ASSERT(!checker.guarded);
    }

    void TestTryGuard() {
        TGuardChecker checker;

        UNIT_ASSERT(!checker.guarded);
        {
            TTryGuard<TGuardChecker> guard(checker);
            UNIT_ASSERT(checker.guarded);
            UNIT_ASSERT(guard.WasAcquired());
            {
                TTryGuard<TGuardChecker> guard2(checker);
                UNIT_ASSERT(checker.guarded);
                UNIT_ASSERT(!guard2.WasAcquired());
            }
            UNIT_ASSERT(checker.guarded);
        }
        UNIT_ASSERT(!checker.guarded);
    }

    void TestTryReadGuard() {
        TRWMutex mutex;
        {
            TTryReadGuard tryGuard(mutex);
            UNIT_ASSERT(tryGuard.WasAcquired());
            TReadGuard readGuard(mutex);
            TTryReadGuard anotherTryGuard(mutex);
            UNIT_ASSERT(tryGuard.WasAcquired());
        }
        {
            TReadGuard readGuard(mutex);
            TTryReadGuard tryGuard(mutex);
            UNIT_ASSERT(tryGuard.WasAcquired());
        }
        {
            TWriteGuard writeGuard(mutex);
            TTryReadGuard tryGuard(mutex);
            UNIT_ASSERT(!tryGuard.WasAcquired());
        }
        TTryReadGuard tryGuard(mutex);
        UNIT_ASSERT(tryGuard.WasAcquired());
    }

    int WithLockIncrement(TGuardChecker& m, int n) {
        with_lock (m) { 
            UNIT_ASSERT(m.guarded);
            return n + 1;
        }
    }

    void TestWithLock() {
        TGuardChecker m;
        int n = 42;
        n = WithLockIncrement(m, n);
        UNIT_ASSERT(!m.guarded);
        UNIT_ASSERT_EQUAL(n, 43);
    }

    void TestWithLockScope() {
        auto Guard = [](auto) { UNIT_FAIL("Non global Guard used"); return 0; };
        TGuardChecker m;
        with_lock (m) { 
            Y_UNUSED(Guard);
        }
    }
};

UNIT_TEST_SUITE_REGISTRATION(TTestGuard)