aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/cc_semaphore_ut.cpp
blob: 0b37c7b99062a5c1c23380c84f8807dfad1ce6fe (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 <util/system/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());
    }
}