1 #include "support/Cancellation.h"
2 #include "support/Context.h"
3 #include "support/Threading.h"
4 #include "gmock/gmock.h"
5 #include "gtest/gtest.h"
15 TEST(CancellationTest
, CancellationTest
) {
16 auto Task
= cancelableTask();
17 WithContext
ContextWithCancellation(std::move(Task
.first
));
18 EXPECT_FALSE(isCancelled());
20 EXPECT_TRUE(isCancelled());
23 TEST(CancellationTest
, CancelerDiesContextLives
) {
24 std::optional
<WithContext
> ContextWithCancellation
;
26 auto Task
= cancelableTask();
27 ContextWithCancellation
.emplace(std::move(Task
.first
));
28 EXPECT_FALSE(isCancelled());
30 EXPECT_TRUE(isCancelled());
32 EXPECT_TRUE(isCancelled());
35 TEST(CancellationTest
, TaskContextDiesHandleLives
) {
36 auto Task
= cancelableTask();
38 WithContext
ContextWithCancellation(std::move(Task
.first
));
39 EXPECT_FALSE(isCancelled());
41 EXPECT_TRUE(isCancelled());
43 // Still should be able to cancel without any problems.
48 enum { OuterReason
= 1, InnerReason
= 2 };
49 std::pair
<Context
, Canceler
> Outer
, Inner
;
51 Outer
= cancelableTask(OuterReason
);
53 WithContext
WithOuter(Outer
.first
.clone());
54 Inner
= cancelableTask(InnerReason
);
59 TEST(CancellationTest
, Nested
) {
60 // Cancelling inner task works but leaves outer task unaffected.
61 NestedTasks CancelInner
;
62 CancelInner
.Inner
.second();
63 EXPECT_EQ(NestedTasks::InnerReason
, isCancelled(CancelInner
.Inner
.first
));
64 EXPECT_FALSE(isCancelled(CancelInner
.Outer
.first
));
65 // Cancellation of outer task is inherited by inner task.
66 NestedTasks CancelOuter
;
67 CancelOuter
.Outer
.second();
68 EXPECT_EQ(NestedTasks::OuterReason
, isCancelled(CancelOuter
.Inner
.first
));
69 EXPECT_EQ(NestedTasks::OuterReason
, isCancelled(CancelOuter
.Outer
.first
));
72 TEST(CancellationTest
, AsynCancellationTest
) {
73 std::atomic
<bool> HasCancelled(false);
74 Notification Cancelled
;
75 auto TaskToBeCancelled
= [&](Context Ctx
) {
76 WithContext
ContextGuard(std::move(Ctx
));
78 HasCancelled
= isCancelled();
80 auto Task
= cancelableTask();
81 std::thread
AsyncTask(TaskToBeCancelled
, std::move(Task
.first
));
86 EXPECT_TRUE(HasCancelled
);