29 #include <gtest/gtest.h>
39 struct TestThreadManagerSettings {
59 int thread_manager_count;
65 const TestThreadManagerSettings&
settings)
69 num_poll_for_work_(0),
73 void DoWork(
void* ,
bool ,
bool )
override {
74 num_do_work_.fetch_add(1, std::memory_order_relaxed);
77 std::this_thread::sleep_for(
78 std::chrono::milliseconds(settings_.work_duration_ms));
82 int num_poll_for_work()
const {
83 return num_poll_for_work_.load(std::memory_order_relaxed);
86 int num_work_found()
const {
87 return num_work_found_.load(std::memory_order_relaxed);
90 int num_do_work()
const {
91 return num_do_work_.load(std::memory_order_relaxed);
95 TestThreadManagerSettings settings_;
98 std::atomic_int num_do_work_;
99 std::atomic_int num_poll_for_work_;
100 std::atomic_int num_work_found_;
105 int call_num = num_poll_for_work_.fetch_add(1, std::memory_order_relaxed);
106 if (call_num >= settings_.max_poll_calls) {
112 std::this_thread::sleep_for(
113 std::chrono::milliseconds(settings_.poll_duration_ms));
119 if (call_num % 3 == 0) {
123 num_work_found_.fetch_add(1, std::memory_order_relaxed);
127 class ThreadManagerTest
130 void SetUp()
override {
132 if (GetParam().thread_limit > 0) {
135 for (
int i = 0;
i < GetParam().thread_manager_count;
i++) {
136 thread_manager_.emplace_back(
137 new TestThreadManager(
"TestThreadManager", rq, GetParam()));
140 for (
auto&
tm : thread_manager_) {
143 for (
auto&
tm : thread_manager_) {
148 std::vector<std::unique_ptr<TestThreadManager>> thread_manager_;
151 TestThreadManagerSettings
scenarios[] = {
162 TEST_P(ThreadManagerTest, TestPollAndWork) {
163 for (
auto&
tm : thread_manager_) {
167 EXPECT_GE(
tm->num_poll_for_work(), GetParam().max_poll_calls);
172 TEST_P(ThreadManagerTest, TestThreadQuota) {
173 if (GetParam().thread_limit > 0) {
174 for (
auto&
tm : thread_manager_) {
175 EXPECT_GE(
tm->num_poll_for_work(), GetParam().max_poll_calls);
176 EXPECT_LE(
tm->GetMaxActiveThreadsSoFar(), GetParam().thread_limit);
184 int main(
int argc,
char** argv) {
185 std::srand(std::time(
nullptr));