aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/coroutine_ut.cpp
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:15 +0300
commit72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch)
treeda2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/coroutine/engine/coroutine_ut.cpp
parent778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff)
downloadydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/coroutine/engine/coroutine_ut.cpp')
-rw-r--r--library/cpp/coroutine/engine/coroutine_ut.cpp388
1 files changed, 194 insertions, 194 deletions
diff --git a/library/cpp/coroutine/engine/coroutine_ut.cpp b/library/cpp/coroutine/engine/coroutine_ut.cpp
index 8b372496a2..29e0d1dfc5 100644
--- a/library/cpp/coroutine/engine/coroutine_ut.cpp
+++ b/library/cpp/coroutine/engine/coroutine_ut.cpp
@@ -1,34 +1,34 @@
-#include "impl.h"
+#include "impl.h"
#include "condvar.h"
#include "network.h"
-
+
#include <library/cpp/testing/unittest/registar.h>
-
-#include <util/string/cast.h>
+
+#include <util/string/cast.h>
#include <util/system/pipe.h>
#include <util/system/env.h>
#include <util/system/info.h>
#include <util/system/thread.h>
#include <util/generic/xrange.h>
#include <util/generic/serialized_enum.h>
-
+
// TODO (velavokr): BALANCER-1345 add more tests on pollers
-class TCoroTest: public TTestBase {
- UNIT_TEST_SUITE(TCoroTest);
- UNIT_TEST(TestSimpleX1);
+class TCoroTest: public TTestBase {
+ UNIT_TEST_SUITE(TCoroTest);
+ UNIT_TEST(TestSimpleX1);
UNIT_TEST(TestSimpleX1MultiThread);
- UNIT_TEST(TestSimpleX2);
- UNIT_TEST(TestSimpleX3);
- UNIT_TEST(TestMemFun);
- UNIT_TEST(TestMutex);
- UNIT_TEST(TestCondVar);
+ UNIT_TEST(TestSimpleX2);
+ UNIT_TEST(TestSimpleX3);
+ UNIT_TEST(TestMemFun);
+ UNIT_TEST(TestMutex);
+ UNIT_TEST(TestCondVar);
UNIT_TEST(TestJoinDefault);
UNIT_TEST(TestJoinEpoll);
UNIT_TEST(TestJoinKqueue);
UNIT_TEST(TestJoinPoll);
UNIT_TEST(TestJoinSelect);
- UNIT_TEST(TestException);
+ UNIT_TEST(TestException);
UNIT_TEST(TestJoinCancelExitRaceBug);
UNIT_TEST(TestWaitWakeLivelockBug);
UNIT_TEST(TestFastPathWakeDefault)
@@ -46,17 +46,17 @@ class TCoroTest: public TTestBase {
UNIT_TEST(TestUserEvent);
UNIT_TEST(TestPause);
UNIT_TEST(TestOverrideTime);
- UNIT_TEST_SUITE_END();
-
-public:
- void TestException();
- void TestSimpleX1();
+ UNIT_TEST_SUITE_END();
+
+public:
+ void TestException();
+ void TestSimpleX1();
void TestSimpleX1MultiThread();
- void TestSimpleX2();
- void TestSimpleX3();
- void TestMemFun();
- void TestMutex();
- void TestCondVar();
+ void TestSimpleX2();
+ void TestSimpleX3();
+ void TestMemFun();
+ void TestMutex();
+ void TestCondVar();
void TestJoinDefault();
void TestJoinEpoll();
void TestJoinKqueue();
@@ -78,72 +78,72 @@ public:
void TestUserEvent();
void TestPause();
void TestOverrideTime();
-};
-
-void TCoroTest::TestException() {
- TContExecutor e(1000000);
-
- bool f2run = false;
-
- auto f1 = [&f2run](TCont* c) {
- struct TCtx {
- ~TCtx() {
+};
+
+void TCoroTest::TestException() {
+ TContExecutor e(1000000);
+
+ bool f2run = false;
+
+ auto f1 = [&f2run](TCont* c) {
+ struct TCtx {
+ ~TCtx() {
Y_VERIFY(!*F2);
-
- C->Yield();
- }
-
- TCont* C;
- bool* F2;
- };
-
- try {
- TCtx ctx = {c, &f2run};
-
- throw 1;
- } catch (...) {
- }
- };
-
- bool unc = true;
-
- auto f2 = [&unc, &f2run](TCont*) {
- f2run = true;
- unc = std::uncaught_exception();
-
- // check segfault
- try {
- throw 2;
- } catch (int) {
- }
- };
-
- e.Create(f1, "f1");
- e.Create(f2, "f2");
- e.Execute();
-
- UNIT_ASSERT(!unc);
-}
-
+
+ C->Yield();
+ }
+
+ TCont* C;
+ bool* F2;
+ };
+
+ try {
+ TCtx ctx = {c, &f2run};
+
+ throw 1;
+ } catch (...) {
+ }
+ };
+
+ bool unc = true;
+
+ auto f2 = [&unc, &f2run](TCont*) {
+ f2run = true;
+ unc = std::uncaught_exception();
+
+ // check segfault
+ try {
+ throw 2;
+ } catch (int) {
+ }
+ };
+
+ e.Create(f1, "f1");
+ e.Create(f2, "f2");
+ e.Execute();
+
+ UNIT_ASSERT(!unc);
+}
+
static int i0;
-
-static void CoRun(TCont* c, void* /*run*/) {
+
+static void CoRun(TCont* c, void* /*run*/) {
while (i0 < 100000) {
++i0;
UNIT_ASSERT(RunningCont() == c);
- c->Yield();
+ c->Yield();
UNIT_ASSERT(RunningCont() == c);
- }
-}
-
-static void CoMain(TCont* c, void* /*arg*/) {
+ }
+}
+
+static void CoMain(TCont* c, void* /*arg*/) {
for (volatile size_t i2 = 0; i2 < 10; ++i2) {
UNIT_ASSERT(RunningCont() == c);
c->Executor()->Create(CoRun, nullptr, "run");
UNIT_ASSERT(RunningCont() == c);
- }
-}
-
+ }
+}
+
void TCoroTest::TestSimpleX1() {
i0 = 0;
TContExecutor e(32000);
@@ -178,17 +178,17 @@ void TCoroTest::TestSimpleX1MultiThread() {
UNIT_ASSERT_EQUAL(c, nThreads);
}
-struct TTestObject {
+struct TTestObject {
int i = 0;
int j = 0;
-
+
public:
- void RunTask1(TCont*) {
- i = 1;
- }
- void RunTask2(TCont*) {
- j = 2;
- }
+ void RunTask1(TCont*) {
+ i = 1;
+ }
+ void RunTask2(TCont*) {
+ j = 2;
+ }
};
void TCoroTest::TestMemFun() {
@@ -201,130 +201,130 @@ void TCoroTest::TestMemFun() {
UNIT_ASSERT_EQUAL(obj.j, 2);
}
-void TCoroTest::TestSimpleX2() {
- {
+void TCoroTest::TestSimpleX2() {
+ {
i0 = 0;
-
- {
- TContExecutor e(32000);
- e.Execute(CoMain);
- }
-
+
+ {
+ TContExecutor e(32000);
+ e.Execute(CoMain);
+ }
+
UNIT_ASSERT_EQUAL(i0, 100000);
- }
-
- {
+ }
+
+ {
i0 = 0;
-
- {
- TContExecutor e(32000);
- e.Execute(CoMain);
- }
-
+
+ {
+ TContExecutor e(32000);
+ e.Execute(CoMain);
+ }
+
UNIT_ASSERT_EQUAL(i0, 100000);
- }
-}
-
-struct TRunner {
- inline TRunner()
- : Runs(0)
- {
- }
-
- inline void operator()(TCont* c) {
- ++Runs;
- c->Yield();
- }
-
- size_t Runs;
-};
-
-void TCoroTest::TestSimpleX3() {
- TContExecutor e(32000);
- TRunner runner;
-
+ }
+}
+
+struct TRunner {
+ inline TRunner()
+ : Runs(0)
+ {
+ }
+
+ inline void operator()(TCont* c) {
+ ++Runs;
+ c->Yield();
+ }
+
+ size_t Runs;
+};
+
+void TCoroTest::TestSimpleX3() {
+ TContExecutor e(32000);
+ TRunner runner;
+
for (volatile size_t i3 = 0; i3 < 1000; ++i3) {
- e.Create(runner, "runner");
- }
-
- e.Execute();
-
- UNIT_ASSERT_EQUAL(runner.Runs, 1000);
-}
-
+ e.Create(runner, "runner");
+ }
+
+ e.Execute();
+
+ UNIT_ASSERT_EQUAL(runner.Runs, 1000);
+}
+
static TString res;
-static TContMutex mutex;
-
-static void CoMutex(TCont* c, void* /*run*/) {
- {
- mutex.LockI(c);
- c->Yield();
- res += c->Name();
- mutex.UnLock();
- }
-
- c->Yield();
-
- {
- mutex.LockI(c);
- c->Yield();
- res += c->Name();
- mutex.UnLock();
- }
-}
-
-static void CoMutexTest(TCont* c, void* /*run*/) {
+static TContMutex mutex;
+
+static void CoMutex(TCont* c, void* /*run*/) {
+ {
+ mutex.LockI(c);
+ c->Yield();
+ res += c->Name();
+ mutex.UnLock();
+ }
+
+ c->Yield();
+
+ {
+ mutex.LockI(c);
+ c->Yield();
+ res += c->Name();
+ mutex.UnLock();
+ }
+}
+
+static void CoMutexTest(TCont* c, void* /*run*/) {
c->Executor()->Create(CoMutex, nullptr, "1");
c->Executor()->Create(CoMutex, nullptr, "2");
-}
-
-void TCoroTest::TestMutex() {
- TContExecutor e(32000);
- e.Execute(CoMutexTest);
- UNIT_ASSERT_EQUAL(res, "1212");
+}
+
+void TCoroTest::TestMutex() {
+ TContExecutor e(32000);
+ e.Execute(CoMutexTest);
+ UNIT_ASSERT_EQUAL(res, "1212");
res.clear();
-}
-
-static TContMutex m1;
-static TContCondVar c1;
-
-static void CoCondVar(TCont* c, void* /*run*/) {
+}
+
+static TContMutex m1;
+static TContCondVar c1;
+
+static void CoCondVar(TCont* c, void* /*run*/) {
for (size_t i4 = 0; i4 < 3; ++i4) {
- UNIT_ASSERT_EQUAL(m1.LockI(c), 0);
- UNIT_ASSERT_EQUAL(c1.WaitI(c, &m1), 0);
- res += c->Name();
- m1.UnLock();
- }
-}
-
-static void CoCondVarTest(TCont* c, void* /*run*/) {
+ UNIT_ASSERT_EQUAL(m1.LockI(c), 0);
+ UNIT_ASSERT_EQUAL(c1.WaitI(c, &m1), 0);
+ res += c->Name();
+ m1.UnLock();
+ }
+}
+
+static void CoCondVarTest(TCont* c, void* /*run*/) {
c->Executor()->Create(CoCondVar, nullptr, "1");
- c->Yield();
+ c->Yield();
c->Executor()->Create(CoCondVar, nullptr, "2");
- c->Yield();
+ c->Yield();
c->Executor()->Create(CoCondVar, nullptr, "3");
- c->Yield();
+ c->Yield();
c->Executor()->Create(CoCondVar, nullptr, "4");
- c->Yield();
+ c->Yield();
c->Executor()->Create(CoCondVar, nullptr, "5");
- c->Yield();
+ c->Yield();
c->Executor()->Create(CoCondVar, nullptr, "6");
- c->Yield();
-
+ c->Yield();
+
for (size_t i5 = 0; i5 < 3; ++i5) {
res += ToString((size_t)i5) + "^";
- c1.BroadCast();
- c->Yield();
- }
-}
-
-void TCoroTest::TestCondVar() {
- TContExecutor e(32000);
- e.Execute(CoCondVarTest);
- UNIT_ASSERT_EQUAL(res, "0^1234561^1234562^123456");
+ c1.BroadCast();
+ c->Yield();
+ }
+}
+
+void TCoroTest::TestCondVar() {
+ TContExecutor e(32000);
+ e.Execute(CoCondVarTest);
+ UNIT_ASSERT_EQUAL(res, "0^1234561^1234562^123456");
res.clear();
-}
-
+}
+
namespace NCoroTestJoin {
struct TSleepCont {
const TInstant Deadline;
@@ -1004,4 +1004,4 @@ void TCoroTest::TestOverrideTime() {
executor.Execute();
}
-UNIT_TEST_SUITE_REGISTRATION(TCoroTest);
+UNIT_TEST_SUITE_REGISTRATION(TCoroTest);