summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/clangd/CancellationTests.cpp
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2018-08-24 13:09:41 +0000
committerKadir Cetinkaya <kadircet@google.com>2018-08-24 13:09:41 +0000
commit689bf93b2ff10ee1d94249274353bb819618a16d (patch)
tree935ff3b9f11d8e9f359ad8ddbc859530965f36e8 /clang-tools-extra/unittests/clangd/CancellationTests.cpp
parent406f1ff1cdfe3ef065a3ac3414ff1672dcfb9049 (diff)
downloadbcm5719-llvm-689bf93b2ff10ee1d94249274353bb819618a16d.tar.gz
bcm5719-llvm-689bf93b2ff10ee1d94249274353bb819618a16d.zip
[clangd] Initial cancellation mechanism for LSP requests.
Reviewers: ilya-biryukov, ioeric, hokein Reviewed By: ilya-biryukov Subscribers: mgorny, ioeric, MaskRay, jkorous, arphaman, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D50502 llvm-svn: 340607
Diffstat (limited to 'clang-tools-extra/unittests/clangd/CancellationTests.cpp')
-rw-r--r--clang-tools-extra/unittests/clangd/CancellationTests.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clangd/CancellationTests.cpp b/clang-tools-extra/unittests/clangd/CancellationTests.cpp
new file mode 100644
index 00000000000..7afd1086ac7
--- /dev/null
+++ b/clang-tools-extra/unittests/clangd/CancellationTests.cpp
@@ -0,0 +1,74 @@
+#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) {
+ TaskHandle TH = Task::createHandle();
+ WithContext ContextWithCancellation(setCurrentTask(TH));
+ EXPECT_FALSE(isCancelled());
+ TH->cancel();
+ EXPECT_TRUE(isCancelled());
+}
+
+TEST(CancellationTest, TaskTestHandleDiesContextLives) {
+ llvm::Optional<WithContext> ContextWithCancellation;
+ {
+ TaskHandle TH = Task::createHandle();
+ ContextWithCancellation.emplace(setCurrentTask(TH));
+ EXPECT_FALSE(isCancelled());
+ TH->cancel();
+ EXPECT_TRUE(isCancelled());
+ }
+ EXPECT_TRUE(isCancelled());
+}
+
+TEST(CancellationTest, TaskContextDiesHandleLives) {
+ TaskHandle TH = Task::createHandle();
+ {
+ WithContext ContextWithCancellation(setCurrentTask(TH));
+ EXPECT_FALSE(isCancelled());
+ TH->cancel();
+ EXPECT_TRUE(isCancelled());
+ }
+ // Still should be able to cancel without any problems.
+ TH->cancel();
+}
+
+TEST(CancellationTest, CancellationToken) {
+ TaskHandle TH = Task::createHandle();
+ WithContext ContextWithCancellation(setCurrentTask(TH));
+ const auto &CT = getCurrentTask();
+ EXPECT_FALSE(CT.isCancelled());
+ TH->cancel();
+ EXPECT_TRUE(CT.isCancelled());
+}
+
+TEST(CancellationTest, AsynCancellationTest) {
+ std::atomic<bool> HasCancelled(false);
+ Notification Cancelled;
+ auto TaskToBeCancelled = [&](ConstTaskHandle CT) {
+ WithContext ContextGuard(setCurrentTask(std::move(CT)));
+ Cancelled.wait();
+ HasCancelled = isCancelled();
+ };
+ TaskHandle TH = Task::createHandle();
+ std::thread AsyncTask(TaskToBeCancelled, TH);
+ TH->cancel();
+ Cancelled.notify();
+ AsyncTask.join();
+
+ EXPECT_TRUE(HasCancelled);
+}
+} // namespace
+} // namespace clangd
+} // namespace clang
OpenPOWER on IntegriCloud