diff options
author | nocomer <nocomer@yandex-team.com> | 2022-10-14 15:48:41 +0300 |
---|---|---|
committer | nocomer <nocomer@yandex-team.com> | 2022-10-14 15:48:41 +0300 |
commit | 71316eadc4b07f0334a6b87e4b9133a3ce81810b (patch) | |
tree | 0a4ae9189c2ec04544f8addfa574a2de83808ff8 /library/cpp/coroutine/engine/coroutine_ut.cpp | |
parent | 11cf1c4aa90b19d0a23b3a645ade2d53e4cadb53 (diff) | |
download | ydb-71316eadc4b07f0334a6b87e4b9133a3ce81810b.tar.gz |
Allow to pass exception to the coroutine to be cancelled
Diffstat (limited to 'library/cpp/coroutine/engine/coroutine_ut.cpp')
-rw-r--r-- | library/cpp/coroutine/engine/coroutine_ut.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/library/cpp/coroutine/engine/coroutine_ut.cpp b/library/cpp/coroutine/engine/coroutine_ut.cpp index 35b7d6353a..c8a1852f7c 100644 --- a/library/cpp/coroutine/engine/coroutine_ut.cpp +++ b/library/cpp/coroutine/engine/coroutine_ut.cpp @@ -47,6 +47,7 @@ class TCoroTest: public TTestBase { UNIT_TEST(TestUserEvent); UNIT_TEST(TestPause); UNIT_TEST(TestOverrideTime); + UNIT_TEST(TestCancelWithException); UNIT_TEST_SUITE_END(); public: @@ -79,6 +80,7 @@ public: void TestUserEvent(); void TestPause(); void TestOverrideTime(); + void TestCancelWithException(); }; void TCoroTest::TestException() { @@ -1005,4 +1007,38 @@ void TCoroTest::TestOverrideTime() { executor.Execute(); } + +void TCoroTest::TestCancelWithException() { + TContExecutor exec(32000); + + TString excText = "test exception"; + THolder<std::exception> excep = MakeHolder<yexception>(yexception() << excText); + std::exception* excPtr = excep.Get(); + + exec.CreateOwned([&](TCont* cont){ + TCont *cont1 = cont->Executor()->CreateOwned([&](TCont* c) { + int result = c->SleepD(TDuration::MilliSeconds(200).ToDeadLine()); + UNIT_ASSERT_EQUAL(result, ECANCELED); + UNIT_ASSERT_EQUAL(c->Cancelled(), true); + THolder<std::exception> exc = c->TakeException(); + UNIT_ASSERT_EQUAL(exc.Get(), excPtr); + UNIT_ASSERT_EQUAL(exc->what(), excText); + UNIT_ASSERT(dynamic_cast<yexception*>(exc.Get()) != nullptr); + }, "cancelExc"); + cont1->Cancel(std::move(excep)); + + TCont* cont2 = cont->Executor()->CreateOwned([&](TCont* c) { + int result = c->SleepD(TDuration::MilliSeconds(200).ToDeadLine()); + UNIT_ASSERT_EQUAL(result, ECANCELED); + UNIT_ASSERT_EQUAL(c->Cancelled(), true); + THolder<std::exception> exc = c->TakeException(); + UNIT_ASSERT_EQUAL(exc.Get(), nullptr); + }, "cancelTwice"); + cont2->Cancel(); + THolder<std::exception> e = MakeHolder<yexception>(yexception() << "another exception"); + cont2->Cancel(std::move(e)); + }, "coro"); + + exec.Execute(); +} UNIT_TEST_SUITE_REGISTRATION(TCoroTest); |