aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/misc/test_sync.h
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 /library/cpp/messagebus/misc/test_sync.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/messagebus/misc/test_sync.h')
-rw-r--r--library/cpp/messagebus/misc/test_sync.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/library/cpp/messagebus/misc/test_sync.h b/library/cpp/messagebus/misc/test_sync.h
new file mode 100644
index 0000000000..be3f4f20b8
--- /dev/null
+++ b/library/cpp/messagebus/misc/test_sync.h
@@ -0,0 +1,75 @@
+#pragma once
+
+#include <util/system/condvar.h>
+#include <util/system/mutex.h>
+
+class TTestSync {
+private:
+ unsigned Current;
+
+ TMutex Mutex;
+ TCondVar CondVar;
+
+public:
+ TTestSync()
+ : Current(0)
+ {
+ }
+
+ void Inc() {
+ TGuard<TMutex> guard(Mutex);
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ unsigned Get() {
+ TGuard<TMutex> guard(Mutex);
+
+ return Current;
+ }
+
+ void WaitFor(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
+ Y_VERIFY(Current <= n, "too late, waiting for %d, already %d", n, Current);
+
+ while (n > Current) {
+ CondVar.WaitI(Mutex);
+ }
+ }
+
+ void WaitForAndIncrement(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
+ Y_VERIFY(Current <= n, "too late, waiting for %d, already %d", n, Current);
+
+ while (n > Current) {
+ CondVar.WaitI(Mutex);
+ }
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ void CheckAndIncrement(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
+ Y_VERIFY(Current == n, "must be %d, currently %d", n, Current);
+
+ DoInc();
+ CondVar.BroadCast();
+ }
+
+ void Check(unsigned n) {
+ TGuard<TMutex> guard(Mutex);
+
+ Y_VERIFY(Current == n, "must be %d, currently %d", n, Current);
+ }
+
+private:
+ void DoInc() {
+ unsigned r = ++Current;
+ Y_UNUSED(r);
+ }
+};