CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
91 lines
2.4 KiB
C++
91 lines
2.4 KiB
C++
#include "support/Cancellation.h"
|
|
#include "support/Context.h"
|
|
#include "support/Threading.h"
|
|
#include "gmock/gmock.h"
|
|
#include "gtest/gtest.h"
|
|
#include <atomic>
|
|
#include <memory>
|
|
#include <optional>
|
|
#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) {
|
|
std::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();
|
|
}
|
|
|
|
struct NestedTasks {
|
|
enum { OuterReason = 1, InnerReason = 2 };
|
|
std::pair<Context, Canceler> Outer, Inner;
|
|
NestedTasks() {
|
|
Outer = cancelableTask(OuterReason);
|
|
{
|
|
WithContext WithOuter(Outer.first.clone());
|
|
Inner = cancelableTask(InnerReason);
|
|
}
|
|
}
|
|
};
|
|
|
|
TEST(CancellationTest, Nested) {
|
|
// Cancelling inner task works but leaves outer task unaffected.
|
|
NestedTasks CancelInner;
|
|
CancelInner.Inner.second();
|
|
EXPECT_EQ(NestedTasks::InnerReason, isCancelled(CancelInner.Inner.first));
|
|
EXPECT_FALSE(isCancelled(CancelInner.Outer.first));
|
|
// Cancellation of outer task is inherited by inner task.
|
|
NestedTasks CancelOuter;
|
|
CancelOuter.Outer.second();
|
|
EXPECT_EQ(NestedTasks::OuterReason, isCancelled(CancelOuter.Inner.first));
|
|
EXPECT_EQ(NestedTasks::OuterReason, isCancelled(CancelOuter.Outer.first));
|
|
}
|
|
|
|
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
|