thread_manager_test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <grpc/support/port_platform.h>
  19. #include "src/cpp/thread_manager/thread_manager.h"
  20. #include <atomic>
  21. #include <chrono>
  22. #include <climits>
  23. #include <memory>
  24. #include <thread>
  25. #include <gtest/gtest.h>
  26. #include <grpc/support/log.h>
  27. #include <grpcpp/grpcpp.h>
  28. #include "test/core/util/test_config.h"
  29. namespace grpc {
  30. namespace {
  31. struct TestThreadManagerSettings {
  32. // The min number of pollers that SHOULD be active in ThreadManager
  33. int min_pollers;
  34. // The max number of pollers that could be active in ThreadManager
  35. int max_pollers;
  36. // The sleep duration in PollForWork() function to simulate "polling"
  37. int poll_duration_ms;
  38. // The sleep duration in DoWork() function to simulate "work"
  39. int work_duration_ms;
  40. // Max number of times PollForWork() is called before shutting down
  41. int max_poll_calls;
  42. // The thread limit (for use in resource quote)
  43. int thread_limit;
  44. // How many should be instantiated
  45. int thread_manager_count;
  46. };
  47. class TestThreadManager final : public grpc::ThreadManager {
  48. public:
  49. TestThreadManager(const char* name, grpc_resource_quota* rq,
  50. const TestThreadManagerSettings& settings)
  51. : ThreadManager(name, rq, settings.min_pollers, settings.max_pollers),
  52. settings_(settings),
  53. num_do_work_(0),
  54. num_poll_for_work_(0),
  55. num_work_found_(0) {}
  56. grpc::ThreadManager::WorkStatus PollForWork(void** tag, bool* ok) override;
  57. void DoWork(void* /* tag */, bool /*ok*/, bool /*resources*/) override {
  58. num_do_work_.fetch_add(1, std::memory_order_relaxed);
  59. // Simulate work by sleeping
  60. std::this_thread::sleep_for(
  61. std::chrono::milliseconds(settings_.work_duration_ms));
  62. }
  63. // Get number of times PollForWork() was called
  64. int num_poll_for_work() const {
  65. return num_poll_for_work_.load(std::memory_order_relaxed);
  66. }
  67. // Get number of times PollForWork() returned WORK_FOUND
  68. int num_work_found() const {
  69. return num_work_found_.load(std::memory_order_relaxed);
  70. }
  71. // Get number of times DoWork() was called
  72. int num_do_work() const {
  73. return num_do_work_.load(std::memory_order_relaxed);
  74. }
  75. private:
  76. TestThreadManagerSettings settings_;
  77. // Counters
  78. std::atomic_int num_do_work_; // Number of calls to DoWork
  79. std::atomic_int num_poll_for_work_; // Number of calls to PollForWork
  80. std::atomic_int num_work_found_; // Number of times WORK_FOUND was returned
  81. };
  82. grpc::ThreadManager::WorkStatus TestThreadManager::PollForWork(void** tag,
  83. bool* ok) {
  84. int call_num = num_poll_for_work_.fetch_add(1, std::memory_order_relaxed);
  85. if (call_num >= settings_.max_poll_calls) {
  86. Shutdown();
  87. return SHUTDOWN;
  88. }
  89. // Simulate "polling" duration
  90. std::this_thread::sleep_for(
  91. std::chrono::milliseconds(settings_.poll_duration_ms));
  92. *tag = nullptr;
  93. *ok = true;
  94. // Return timeout roughly 1 out of every 3 calls just to make the test a bit
  95. // more interesting
  96. if (call_num % 3 == 0) {
  97. return TIMEOUT;
  98. }
  99. num_work_found_.fetch_add(1, std::memory_order_relaxed);
  100. return WORK_FOUND;
  101. }
  102. class ThreadManagerTest
  103. : public ::testing::TestWithParam<TestThreadManagerSettings> {
  104. protected:
  105. void SetUp() override {
  106. grpc_resource_quota* rq = grpc_resource_quota_create("Thread manager test");
  107. if (GetParam().thread_limit > 0) {
  108. grpc_resource_quota_set_max_threads(rq, GetParam().thread_limit);
  109. }
  110. for (int i = 0; i < GetParam().thread_manager_count; i++) {
  111. thread_manager_.emplace_back(
  112. new TestThreadManager("TestThreadManager", rq, GetParam()));
  113. }
  114. grpc_resource_quota_unref(rq);
  115. for (auto& tm : thread_manager_) {
  116. tm->Initialize();
  117. }
  118. for (auto& tm : thread_manager_) {
  119. tm->Wait();
  120. }
  121. }
  122. std::vector<std::unique_ptr<TestThreadManager>> thread_manager_;
  123. };
  124. TestThreadManagerSettings scenarios[] = {
  125. {2 /* min_pollers */, 10 /* max_pollers */, 10 /* poll_duration_ms */,
  126. 1 /* work_duration_ms */, 50 /* max_poll_calls */,
  127. INT_MAX /* thread_limit */, 1 /* thread_manager_count */},
  128. {1 /* min_pollers */, 1 /* max_pollers */, 1 /* poll_duration_ms */,
  129. 10 /* work_duration_ms */, 50 /* max_poll_calls */, 3 /* thread_limit */,
  130. 2 /* thread_manager_count */}};
  131. INSTANTIATE_TEST_SUITE_P(ThreadManagerTest, ThreadManagerTest,
  132. ::testing::ValuesIn(scenarios));
  133. TEST_P(ThreadManagerTest, TestPollAndWork) {
  134. for (auto& tm : thread_manager_) {
  135. // Verify that The number of times DoWork() was called is equal to the
  136. // number of times WORK_FOUND was returned
  137. gpr_log(GPR_DEBUG, "DoWork() called %d times", tm->num_do_work());
  138. EXPECT_GE(tm->num_poll_for_work(), GetParam().max_poll_calls);
  139. EXPECT_EQ(tm->num_do_work(), tm->num_work_found());
  140. }
  141. }
  142. TEST_P(ThreadManagerTest, TestThreadQuota) {
  143. if (GetParam().thread_limit > 0) {
  144. for (auto& tm : thread_manager_) {
  145. EXPECT_GE(tm->num_poll_for_work(), GetParam().max_poll_calls);
  146. EXPECT_LE(tm->GetMaxActiveThreadsSoFar(), GetParam().thread_limit);
  147. }
  148. }
  149. }
  150. } // namespace
  151. } // namespace grpc
  152. int main(int argc, char** argv) {
  153. std::srand(std::time(nullptr));
  154. grpc::testing::TestEnvironment env(argc, argv);
  155. ::testing::InitGoogleTest(&argc, argv);
  156. grpc_init();
  157. auto ret = RUN_ALL_TESTS();
  158. grpc_shutdown();
  159. return ret;
  160. }