aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/cc_semaphore_ut.cpp
blob: 24c6ec0400d7637ef440ef586b852b5bfbf4ec7d (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
#include <library/cpp/testing/unittest/registar.h>

#include "cc_semaphore.h"

#include <library/cpp/deprecated/atomic/atomic.h>

namespace {
    struct TTestSemaphore: public TComplexConditionSemaphore<TTestSemaphore> {
        TAtomic Current;

        TTestSemaphore()
            : Current(0)
        {
        }

        bool TryWait() {
            return AtomicGet(Current) > 0;
        }

        void Aquire() {
            Wait();
            AtomicDecrement(Current);
        }

        void Release() {
            AtomicIncrement(Current);
            Updated();
        }
    };
}

Y_UNIT_TEST_SUITE(TComplexConditionSemaphore) {
    Y_UNIT_TEST(Simple) {
        TTestSemaphore sema;
        UNIT_ASSERT(!sema.TryWait());
        sema.Release();
        UNIT_ASSERT(sema.TryWait());
        sema.Release();
        UNIT_ASSERT(sema.TryWait());
        sema.Aquire();
        UNIT_ASSERT(sema.TryWait());
        sema.Aquire();
        UNIT_ASSERT(!sema.TryWait());
    }
}