3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *is % allowed in string
25 #include <grpc/support/log.h>
26 #include <grpc/support/port_platform.h>
27 #include <grpcpp/grpcpp.h>
29 #include "src/cpp/thread_manager/thread_manager.h"
30 #include "test/core/util/test_config.h"
32 #include <gtest/gtest.h>
37 struct TestThreadManagerSettings {
38 // The min number of pollers that SHOULD be active in ThreadManager
41 // The max number of pollers that could be active in ThreadManager
44 // The sleep duration in PollForWork() function to simulate "polling"
47 // The sleep duration in DoWork() function to simulate "work"
50 // Max number of times PollForWork() is called before shutting down
53 // The thread limit (for use in resource quote)
56 // How many should be instantiated
57 int thread_manager_count;
60 class TestThreadManager final : public grpc::ThreadManager {
62 TestThreadManager(const char* name, grpc_resource_quota* rq,
63 const TestThreadManagerSettings& settings)
64 : ThreadManager(name, rq, settings.min_pollers, settings.max_pollers),
67 num_poll_for_work_(0),
70 grpc::ThreadManager::WorkStatus PollForWork(void** tag, bool* ok) override;
71 void DoWork(void* /* tag */, bool /*ok*/, bool /*resources*/) override {
72 num_do_work_.fetch_add(1, std::memory_order_relaxed);
74 // Simulate work by sleeping
75 std::this_thread::sleep_for(
76 std::chrono::milliseconds(settings_.work_duration_ms));
79 // Get number of times PollForWork() was called
80 int num_poll_for_work() const {
81 return num_poll_for_work_.load(std::memory_order_relaxed);
83 // Get number of times PollForWork() returned WORK_FOUND
84 int num_work_found() const {
85 return num_work_found_.load(std::memory_order_relaxed);
87 // Get number of times DoWork() was called
88 int num_do_work() const {
89 return num_do_work_.load(std::memory_order_relaxed);
93 TestThreadManagerSettings settings_;
96 std::atomic_int num_do_work_; // Number of calls to DoWork
97 std::atomic_int num_poll_for_work_; // Number of calls to PollForWork
98 std::atomic_int num_work_found_; // Number of times WORK_FOUND was returned
101 grpc::ThreadManager::WorkStatus TestThreadManager::PollForWork(void** tag,
103 int call_num = num_poll_for_work_.fetch_add(1, std::memory_order_relaxed);
104 if (call_num >= settings_.max_poll_calls) {
109 // Simulate "polling" duration
110 std::this_thread::sleep_for(
111 std::chrono::milliseconds(settings_.poll_duration_ms));
115 // Return timeout roughly 1 out of every 3 calls just to make the test a bit
117 if (call_num % 3 == 0) {
121 num_work_found_.fetch_add(1, std::memory_order_relaxed);
125 class ThreadManagerTest
126 : public ::testing::TestWithParam<TestThreadManagerSettings> {
128 void SetUp() override {
129 grpc_resource_quota* rq = grpc_resource_quota_create("Thread manager test");
130 if (GetParam().thread_limit > 0) {
131 grpc_resource_quota_set_max_threads(rq, GetParam().thread_limit);
133 for (int i = 0; i < GetParam().thread_manager_count; i++) {
134 thread_manager_.emplace_back(
135 new TestThreadManager("TestThreadManager", rq, GetParam()));
137 grpc_resource_quota_unref(rq);
138 for (auto& tm : thread_manager_) {
141 for (auto& tm : thread_manager_) {
146 std::vector<std::unique_ptr<TestThreadManager>> thread_manager_;
149 TestThreadManagerSettings scenarios[] = {
150 {2 /* min_pollers */, 10 /* max_pollers */, 10 /* poll_duration_ms */,
151 1 /* work_duration_ms */, 50 /* max_poll_calls */,
152 INT_MAX /* thread_limit */, 1 /* thread_manager_count */},
153 {1 /* min_pollers */, 1 /* max_pollers */, 1 /* poll_duration_ms */,
154 10 /* work_duration_ms */, 50 /* max_poll_calls */, 3 /* thread_limit */,
155 2 /* thread_manager_count */}};
157 INSTANTIATE_TEST_SUITE_P(ThreadManagerTest, ThreadManagerTest,
158 ::testing::ValuesIn(scenarios));
160 TEST_P(ThreadManagerTest, TestPollAndWork) {
161 for (auto& tm : thread_manager_) {
162 // Verify that The number of times DoWork() was called is equal to the
163 // number of times WORK_FOUND was returned
164 gpr_log(GPR_DEBUG, "DoWork() called %d times", tm->num_do_work());
165 EXPECT_GE(tm->num_poll_for_work(), GetParam().max_poll_calls);
166 EXPECT_EQ(tm->num_do_work(), tm->num_work_found());
170 TEST_P(ThreadManagerTest, TestThreadQuota) {
171 if (GetParam().thread_limit > 0) {
172 for (auto& tm : thread_manager_) {
173 EXPECT_GE(tm->num_poll_for_work(), GetParam().max_poll_calls);
174 EXPECT_LE(tm->GetMaxActiveThreadsSoFar(), GetParam().thread_limit);
182 int main(int argc, char** argv) {
183 std::srand(std::time(nullptr));
184 grpc::testing::TestEnvironment env(argc, argv);
185 ::testing::InitGoogleTest(&argc, argv);
188 auto ret = RUN_ALL_TESTS();