blob: c744795a08171b78f828b81842288729df80dda6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
}
}
|