clang-tools  9.0.0
CancellationTests.cpp
Go to the documentation of this file.
1 #include "Cancellation.h"
2 #include "Context.h"
3 #include "Threading.h"
4 #include "llvm/Support/Error.h"
5 #include "gmock/gmock.h"
6 #include "gtest/gtest.h"
7 #include <atomic>
8 #include <memory>
9 #include <thread>
10 
11 namespace clang {
12 namespace clangd {
13 namespace {
14 
15 TEST(CancellationTest, CancellationTest) {
16  auto Task = cancelableTask();
17  WithContext ContextWithCancellation(std::move(Task.first));
18  EXPECT_FALSE(isCancelled());
19  Task.second();
20  EXPECT_TRUE(isCancelled());
21 }
22 
23 TEST(CancellationTest, CancelerDiesContextLives) {
24  llvm::Optional<WithContext> ContextWithCancellation;
25  {
26  auto Task = cancelableTask();
27  ContextWithCancellation.emplace(std::move(Task.first));
28  EXPECT_FALSE(isCancelled());
29  Task.second();
30  EXPECT_TRUE(isCancelled());
31  }
32  EXPECT_TRUE(isCancelled());
33 }
34 
35 TEST(CancellationTest, TaskContextDiesHandleLives) {
36  auto Task = cancelableTask();
37  {
38  WithContext ContextWithCancellation(std::move(Task.first));
39  EXPECT_FALSE(isCancelled());
40  Task.second();
41  EXPECT_TRUE(isCancelled());
42  }
43  // Still should be able to cancel without any problems.
44  Task.second();
45 }
46 
47 TEST(CancellationTest, AsynCancellationTest) {
48  std::atomic<bool> HasCancelled(false);
49  Notification Cancelled;
50  auto TaskToBeCancelled = [&](Context Ctx) {
51  WithContext ContextGuard(std::move(Ctx));
52  Cancelled.wait();
53  HasCancelled = isCancelled();
54  };
55  auto Task = cancelableTask();
56  std::thread AsyncTask(TaskToBeCancelled, std::move(Task.first));
57  Task.second();
58  Cancelled.notify();
59  AsyncTask.join();
60 
61  EXPECT_TRUE(HasCancelled);
62 }
63 } // namespace
64 } // namespace clangd
65 } // namespace clang
bool isCancelled(const Context &Ctx)
True if the current context is within a cancelable task which was cancelled.
Context Ctx
TEST(BackgroundQueueTest, Priority)
std::pair< Context, Canceler > cancelableTask()
Defines a new task whose cancellation may be requested.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//