File: CancellationTests.cpp

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (65 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
#include "Cancellation.h"
#include "Context.h"
#include "Threading.h"
#include "llvm/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <atomic>
#include <memory>
#include <thread>

namespace clang {
namespace clangd {
namespace {

TEST(CancellationTest, CancellationTest) {
  auto Task = cancelableTask();
  WithContext ContextWithCancellation(std::move(Task.first));
  EXPECT_FALSE(isCancelled());
  Task.second();
  EXPECT_TRUE(isCancelled());
}

TEST(CancellationTest, CancelerDiesContextLives) {
  llvm::Optional<WithContext> ContextWithCancellation;
  {
    auto Task = cancelableTask();
    ContextWithCancellation.emplace(std::move(Task.first));
    EXPECT_FALSE(isCancelled());
    Task.second();
    EXPECT_TRUE(isCancelled());
  }
  EXPECT_TRUE(isCancelled());
}

TEST(CancellationTest, TaskContextDiesHandleLives) {
  auto Task = cancelableTask();
  {
    WithContext ContextWithCancellation(std::move(Task.first));
    EXPECT_FALSE(isCancelled());
    Task.second();
    EXPECT_TRUE(isCancelled());
  }
  // Still should be able to cancel without any problems.
  Task.second();
}

TEST(CancellationTest, AsynCancellationTest) {
  std::atomic<bool> HasCancelled(false);
  Notification Cancelled;
  auto TaskToBeCancelled = [&](Context Ctx) {
    WithContext ContextGuard(std::move(Ctx));
    Cancelled.wait();
    HasCancelled = isCancelled();
  };
  auto Task = cancelableTask();
  std::thread AsyncTask(TaskToBeCancelled, std::move(Task.first));
  Task.second();
  Cancelled.notify();
  AsyncTask.join();

  EXPECT_TRUE(HasCancelled);
}
} // namespace
} // namespace clangd
} // namespace clang