aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/flock_ut.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/flock_ut.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/flock_ut.cpp')
-rw-r--r--util/system/flock_ut.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/util/system/flock_ut.cpp b/util/system/flock_ut.cpp
new file mode 100644
index 0000000000..b5f6cb5328
--- /dev/null
+++ b/util/system/flock_ut.cpp
@@ -0,0 +1,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());
+ }
+ }
+}