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
111 lines
3.2 KiB
C++
111 lines
3.2 KiB
C++
//===-- ThreadPoolTaskDispatcherTest.cpp ----------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "orc-rt/ThreadPoolTaskDispatcher.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <atomic>
|
|
#include <future>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using namespace orc_rt;
|
|
|
|
namespace {
|
|
|
|
TEST(ThreadPoolTaskDispatcherTest, NoTasks) {
|
|
// Check that immediate shutdown works as expected.
|
|
ThreadPoolTaskDispatcher Dispatcher(1);
|
|
Dispatcher.shutdown();
|
|
}
|
|
|
|
TEST(ThreadPoolTaskDispatcherTest, BasicTaskExecution) {
|
|
// Smoke test: Check that we can run a single task on a single-threaded pool.
|
|
ThreadPoolTaskDispatcher Dispatcher(1);
|
|
std::atomic<bool> TaskRan = false;
|
|
|
|
Dispatcher.dispatch(makeGenericTask([&]() { TaskRan = true; }));
|
|
|
|
Dispatcher.shutdown();
|
|
|
|
EXPECT_TRUE(TaskRan);
|
|
}
|
|
|
|
TEST(ThreadPoolTaskDispatcherTest, SingleThreadMultipleTasks) {
|
|
// Check that multiple tasks in a single threaded pool run as expected.
|
|
ThreadPoolTaskDispatcher Dispatcher(1);
|
|
size_t NumTasksToRun = 10;
|
|
std::atomic<size_t> TasksRun = 0;
|
|
|
|
for (size_t I = 0; I != NumTasksToRun; ++I)
|
|
Dispatcher.dispatch(makeGenericTask([&]() { ++TasksRun; }));
|
|
|
|
Dispatcher.shutdown();
|
|
|
|
EXPECT_EQ(TasksRun, NumTasksToRun);
|
|
}
|
|
|
|
TEST(ThreadPoolTaskDispatcherTest, ConcurrentTasks) {
|
|
// Check that tasks are run concurrently when multiple workers are available.
|
|
// Adds two tasks that communicate a value back and forth using futures.
|
|
// Neither task should be able to complete without the other having started.
|
|
ThreadPoolTaskDispatcher Dispatcher(2);
|
|
|
|
std::promise<int> PInit;
|
|
std::future<int> FInit = PInit.get_future();
|
|
std::promise<int> P1;
|
|
std::future<int> F1 = P1.get_future();
|
|
std::promise<int> P2;
|
|
std::future<int> F2 = P2.get_future();
|
|
std::promise<int> PResult;
|
|
std::future<int> FResult = PResult.get_future();
|
|
|
|
// Task A gets the initial value, sends it via P1, waits for response on F2.
|
|
Dispatcher.dispatch(makeGenericTask([&]() {
|
|
P1.set_value(FInit.get());
|
|
PResult.set_value(F2.get());
|
|
}));
|
|
|
|
// Task B gets value from F1, sends it back on P2.
|
|
Dispatcher.dispatch(makeGenericTask([&]() { P2.set_value(F1.get()); }));
|
|
|
|
int ExpectedValue = 42;
|
|
PInit.set_value(ExpectedValue);
|
|
|
|
Dispatcher.shutdown();
|
|
|
|
EXPECT_EQ(FResult.get(), ExpectedValue);
|
|
}
|
|
|
|
TEST(ThreadPoolTaskDispatcherTest, TasksRejectedAfterShutdown) {
|
|
class TaskToReject : public Task {
|
|
public:
|
|
TaskToReject(bool &BodyRun, bool &DestructorRun)
|
|
: BodyRun(BodyRun), DestructorRun(DestructorRun) {}
|
|
~TaskToReject() { DestructorRun = true; }
|
|
void run() override { BodyRun = true; }
|
|
|
|
private:
|
|
bool &BodyRun;
|
|
bool &DestructorRun;
|
|
};
|
|
|
|
ThreadPoolTaskDispatcher Dispatcher(1);
|
|
Dispatcher.shutdown();
|
|
|
|
bool BodyRun = false;
|
|
bool DestructorRun = false;
|
|
|
|
Dispatcher.dispatch(std::make_unique<TaskToReject>(BodyRun, DestructorRun));
|
|
|
|
EXPECT_FALSE(BodyRun);
|
|
EXPECT_TRUE(DestructorRun);
|
|
}
|
|
|
|
} // end anonymous namespace
|