Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / cpp / thread_manager / thread_manager_test.cc
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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
17  */
18
19 #include <atomic>
20 #include <chrono>
21 #include <climits>
22 #include <memory>
23 #include <thread>
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/port_platform.h>
27 #include <grpcpp/grpcpp.h>
28
29 #include "src/cpp/thread_manager/thread_manager.h"
30 #include "test/core/util/test_config.h"
31
32 #include <gtest/gtest.h>
33
34 namespace grpc {
35 namespace {
36
37 struct TestThreadManagerSettings {
38   // The min number of pollers that SHOULD be active in ThreadManager
39   int min_pollers;
40
41   // The max number of pollers that could be active in ThreadManager
42   int max_pollers;
43
44   // The sleep duration in PollForWork() function to simulate "polling"
45   int poll_duration_ms;
46
47   // The sleep duration in DoWork() function to simulate "work"
48   int work_duration_ms;
49
50   // Max number of times PollForWork() is called before shutting down
51   int max_poll_calls;
52
53   // The thread limit (for use in resource quote)
54   int thread_limit;
55
56   // How many should be instantiated
57   int thread_manager_count;
58 };
59
60 class TestThreadManager final : public grpc::ThreadManager {
61  public:
62   TestThreadManager(const char* name, grpc_resource_quota* rq,
63                     const TestThreadManagerSettings& settings)
64       : ThreadManager(name, rq, settings.min_pollers, settings.max_pollers),
65         settings_(settings),
66         num_do_work_(0),
67         num_poll_for_work_(0),
68         num_work_found_(0) {}
69
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);
73
74     // Simulate work by sleeping
75     std::this_thread::sleep_for(
76         std::chrono::milliseconds(settings_.work_duration_ms));
77   }
78
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);
82   }
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);
86   }
87   // Get number of times DoWork() was called
88   int num_do_work() const {
89     return num_do_work_.load(std::memory_order_relaxed);
90   }
91
92  private:
93   TestThreadManagerSettings settings_;
94
95   // Counters
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
99 };
100
101 grpc::ThreadManager::WorkStatus TestThreadManager::PollForWork(void** tag,
102                                                                bool* ok) {
103   int call_num = num_poll_for_work_.fetch_add(1, std::memory_order_relaxed);
104   if (call_num >= settings_.max_poll_calls) {
105     Shutdown();
106     return SHUTDOWN;
107   }
108
109   // Simulate "polling" duration
110   std::this_thread::sleep_for(
111       std::chrono::milliseconds(settings_.poll_duration_ms));
112   *tag = nullptr;
113   *ok = true;
114
115   // Return timeout roughly 1 out of every 3 calls just to make the test a bit
116   // more interesting
117   if (call_num % 3 == 0) {
118     return TIMEOUT;
119   }
120
121   num_work_found_.fetch_add(1, std::memory_order_relaxed);
122   return WORK_FOUND;
123 }
124
125 class ThreadManagerTest
126     : public ::testing::TestWithParam<TestThreadManagerSettings> {
127  protected:
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);
132     }
133     for (int i = 0; i < GetParam().thread_manager_count; i++) {
134       thread_manager_.emplace_back(
135           new TestThreadManager("TestThreadManager", rq, GetParam()));
136     }
137     grpc_resource_quota_unref(rq);
138     for (auto& tm : thread_manager_) {
139       tm->Initialize();
140     }
141     for (auto& tm : thread_manager_) {
142       tm->Wait();
143     }
144   }
145
146   std::vector<std::unique_ptr<TestThreadManager>> thread_manager_;
147 };
148
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 */}};
156
157 INSTANTIATE_TEST_SUITE_P(ThreadManagerTest, ThreadManagerTest,
158                          ::testing::ValuesIn(scenarios));
159
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());
167   }
168 }
169
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);
175     }
176   }
177 }
178
179 }  // namespace
180 }  // namespace grpc
181
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);
186
187   grpc_init();
188   auto ret = RUN_ALL_TESTS();
189   grpc_shutdown();
190
191   return ret;
192 }