diff options
| author | robot-piglet <[email protected]> | 2026-01-26 04:33:13 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2026-01-26 04:46:51 +0300 |
| commit | 1f29108384ee8ca6307a257c6ef67bb75967135c (patch) | |
| tree | adb6f8be66ab3282bdd252783a767137b86f5c3f /library/cpp/threading/cancellation/ut.cpp | |
| parent | 92b20e43d02c35280520dfbf7fd635d5d96c32e4 (diff) | |
Intermediate changes
commit_hash:4373219e5a3e59d3b6020a500b3953d735a28ef0
Diffstat (limited to 'library/cpp/threading/cancellation/ut.cpp')
| -rw-r--r-- | library/cpp/threading/cancellation/ut.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/library/cpp/threading/cancellation/ut.cpp b/library/cpp/threading/cancellation/ut.cpp new file mode 100644 index 00000000000..c744795a081 --- /dev/null +++ b/library/cpp/threading/cancellation/ut.cpp @@ -0,0 +1,56 @@ +#include "cancellation_token.h" + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NThreading; + +Y_UNIT_TEST_SUITE(Cancellation) { + Y_UNIT_TEST(IsCancellationRequested) { + TCancellationTokenSource source; + auto const token = source.Token(); + UNIT_ASSERT(!source.IsCancellationRequested()); + UNIT_ASSERT(!token.IsCancellationRequested()); + source.Cancel(); + UNIT_ASSERT(source.IsCancellationRequested()); + UNIT_ASSERT(token.IsCancellationRequested()); + } + + Y_UNIT_TEST(ThrowIfCancellationRequested) { + TCancellationTokenSource source; + auto const token = source.Token(); + UNIT_ASSERT_NO_EXCEPTION(token.ThrowIfCancellationRequested()); + source.Cancel(); + UNIT_ASSERT_EXCEPTION(token.ThrowIfCancellationRequested(), TOperationCancelledException); + } + + Y_UNIT_TEST(Wait) { + TCancellationTokenSource source; + auto const token = source.Token(); + UNIT_ASSERT(!token.Wait(TDuration::MilliSeconds(10))); + source.Cancel(); + UNIT_ASSERT(token.Wait(TDuration::MilliSeconds(10))); + } + + Y_UNIT_TEST(Future) { + TCancellationTokenSource source; + auto const future = source.Token().Future(); + UNIT_ASSERT(!future.HasValue()); + UNIT_ASSERT(!future.HasException()); + source.Cancel(); + UNIT_ASSERT(future.HasValue()); + } + + Y_UNIT_TEST(Default) { + auto const& token = TCancellationToken::Default(); + UNIT_ASSERT(!token.IsCancellationRequested()); + } + + Y_UNIT_TEST(ThrowIfDeadlineReached) { + TCancellationTokenSource source; + auto token = source.Token(); + UNIT_ASSERT_NO_EXCEPTION(token.ThrowIfCancellationRequested()); + token.SetDeadline(TInstant::Now() - TDuration::Minutes(1)); + UNIT_ASSERT_NO_EXCEPTION(token.ThrowIfCancellationRequested()); + UNIT_ASSERT_EXCEPTION(token.ThrowIfDeadlineReached(), TOperationCancelledException); + } +} |
