aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/flock_ut.cpp
blob: 2bb705944c2d0eb604c36ab8094cf80875c4dcd4 (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
#include "flock.h"
#include "file_lock.h" 
#include "guard.h" 
#include "tempfile.h"

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

Y_UNIT_TEST_SUITE(TFileLockTest) {
    Y_UNIT_TEST(TestFlock) {
        TTempFileHandle tmp("./file");

        UNIT_ASSERT_EQUAL(Flock(tmp.GetHandle(), LOCK_EX), 0);
        UNIT_ASSERT_EQUAL(Flock(tmp.GetHandle(), LOCK_UN), 0);
    }

    Y_UNIT_TEST(TestFileLocker) {
        TTempFileHandle tmp("./file.locker"); 
        TFileLock fileLockExclusive1("./file.locker");
        TFileLock fileLockExclusive2("./file.locker");
        TFileLock fileLockShared1("./file.locker", EFileLockType::Shared);
        TFileLock fileLockShared2("./file.locker", EFileLockType::Shared);
        TFileLock fileLockShared3("./file.locker", EFileLockType::Shared);
        { 
            TGuard<TFileLock> guard(fileLockExclusive1);
        } 
        {
            TTryGuard<TFileLock> tryGuard(fileLockExclusive1);
            UNIT_ASSERT(tryGuard.WasAcquired());
        }
        {
            TGuard<TFileLock> guard1(fileLockExclusive1);
            TTryGuard<TFileLock> guard2(fileLockExclusive2);
            UNIT_ASSERT(!guard2.WasAcquired());
        }
        {
            TGuard<TFileLock> guard1(fileLockShared1);
            TTryGuard<TFileLock> guard2(fileLockShared2);
            TTryGuard<TFileLock> guard3(fileLockShared3);
            UNIT_ASSERT(guard2.WasAcquired());
            UNIT_ASSERT(guard3.WasAcquired());
        }
        {
            TGuard<TFileLock> guard1(fileLockExclusive1);
            TTryGuard<TFileLock> guard2(fileLockShared1);
            TTryGuard<TFileLock> guard3(fileLockShared2);
            UNIT_ASSERT(!guard2.WasAcquired());
            UNIT_ASSERT(!guard3.WasAcquired());
        }
        {
            TGuard<TFileLock> guard1(fileLockShared1);
            TTryGuard<TFileLock> guard2(fileLockExclusive1);
            TTryGuard<TFileLock> guard3(fileLockShared2);
            UNIT_ASSERT(!guard2.WasAcquired());
            UNIT_ASSERT(guard3.WasAcquired());
        }
    } 
}