guard_ut.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "guard.h"
  2. #include "rwlock.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/thread/pool.h>
  5. struct TTestGuard: public TTestBase {
  6. UNIT_TEST_SUITE(TTestGuard);
  7. UNIT_TEST(TestGuard)
  8. UNIT_TEST(TestTryGuard)
  9. UNIT_TEST(TestMove)
  10. UNIT_TEST(TestSync)
  11. UNIT_TEST(TestUnguard)
  12. UNIT_TEST(TestTryReadGuard)
  13. UNIT_TEST(TestWithLock)
  14. UNIT_TEST(TestWithLockScope);
  15. UNIT_TEST_SUITE_END();
  16. struct TGuardChecker {
  17. TGuardChecker()
  18. : guarded(false)
  19. {
  20. }
  21. void Acquire() {
  22. guarded = true;
  23. }
  24. void Release() {
  25. guarded = false;
  26. }
  27. bool TryAcquire() {
  28. if (guarded) {
  29. return false;
  30. } else {
  31. guarded = true;
  32. return true;
  33. }
  34. }
  35. bool guarded;
  36. };
  37. void TestUnguard() {
  38. TGuardChecker m;
  39. {
  40. auto guard = Guard(m);
  41. UNIT_ASSERT(m.guarded);
  42. {
  43. auto unguard = Unguard(guard);
  44. UNIT_ASSERT(!m.guarded);
  45. }
  46. UNIT_ASSERT(m.guarded);
  47. }
  48. {
  49. auto guard = Guard(m);
  50. UNIT_ASSERT(m.guarded);
  51. {
  52. auto unguard = Unguard(m);
  53. UNIT_ASSERT(!m.guarded);
  54. }
  55. UNIT_ASSERT(m.guarded);
  56. }
  57. }
  58. void TestMove() {
  59. TGuardChecker m;
  60. size_t n = 0;
  61. {
  62. auto guard = Guard(m);
  63. UNIT_ASSERT(m.guarded);
  64. ++n;
  65. }
  66. UNIT_ASSERT(!m.guarded);
  67. UNIT_ASSERT_VALUES_EQUAL(n, 1);
  68. }
  69. void TestSync() {
  70. TGuardChecker m;
  71. size_t n = 0;
  72. with_lock (m) {
  73. UNIT_ASSERT(m.guarded);
  74. ++n;
  75. }
  76. UNIT_ASSERT(!m.guarded);
  77. UNIT_ASSERT_VALUES_EQUAL(n, 1);
  78. }
  79. void TestGuard() {
  80. TGuardChecker checker;
  81. UNIT_ASSERT(!checker.guarded);
  82. {
  83. TGuard<TGuardChecker> guard(checker);
  84. UNIT_ASSERT(checker.guarded);
  85. }
  86. UNIT_ASSERT(!checker.guarded);
  87. }
  88. void TestTryGuard() {
  89. TGuardChecker checker;
  90. UNIT_ASSERT(!checker.guarded);
  91. {
  92. TTryGuard<TGuardChecker> guard(checker);
  93. UNIT_ASSERT(checker.guarded);
  94. UNIT_ASSERT(guard.WasAcquired());
  95. {
  96. TTryGuard<TGuardChecker> guard2(checker);
  97. UNIT_ASSERT(checker.guarded);
  98. UNIT_ASSERT(!guard2.WasAcquired());
  99. }
  100. UNIT_ASSERT(checker.guarded);
  101. }
  102. UNIT_ASSERT(!checker.guarded);
  103. }
  104. void TestTryReadGuard() {
  105. TRWMutex mutex;
  106. {
  107. TTryReadGuard tryGuard(mutex);
  108. UNIT_ASSERT(tryGuard.WasAcquired());
  109. TReadGuard readGuard(mutex);
  110. TTryReadGuard anotherTryGuard(mutex);
  111. UNIT_ASSERT(tryGuard.WasAcquired());
  112. }
  113. {
  114. TReadGuard readGuard(mutex);
  115. TTryReadGuard tryGuard(mutex);
  116. UNIT_ASSERT(tryGuard.WasAcquired());
  117. }
  118. {
  119. TWriteGuard writeGuard(mutex);
  120. TTryReadGuard tryGuard(mutex);
  121. UNIT_ASSERT(!tryGuard.WasAcquired());
  122. }
  123. TTryReadGuard tryGuard(mutex);
  124. UNIT_ASSERT(tryGuard.WasAcquired());
  125. }
  126. int WithLockIncrement(TGuardChecker& m, int n) {
  127. with_lock (m) {
  128. UNIT_ASSERT(m.guarded);
  129. return n + 1;
  130. }
  131. }
  132. void TestWithLock() {
  133. TGuardChecker m;
  134. int n = 42;
  135. n = WithLockIncrement(m, n);
  136. UNIT_ASSERT(!m.guarded);
  137. UNIT_ASSERT_EQUAL(n, 43);
  138. }
  139. void TestWithLockScope() {
  140. auto Guard = [](auto) { UNIT_FAIL("Non global Guard used"); return 0; };
  141. TGuardChecker m;
  142. with_lock (m) {
  143. Y_UNUSED(Guard);
  144. }
  145. }
  146. };
  147. UNIT_TEST_SUITE_REGISTRATION(TTestGuard)