work_serializer_test.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. *
  3. * Copyright 2019 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. *
  17. */
  18. #include "src/core/lib/iomgr/work_serializer.h"
  19. #include <memory>
  20. #include <thread>
  21. #include <gtest/gtest.h>
  22. #include "absl/memory/memory.h"
  23. #include "absl/synchronization/barrier.h"
  24. #include "absl/synchronization/notification.h"
  25. #include <grpc/grpc.h>
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/log.h>
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/gprpp/thd.h"
  30. #include "src/core/lib/iomgr/executor.h"
  31. #include "test/core/util/test_config.h"
  32. namespace {
  33. TEST(WorkSerializerTest, NoOp) { grpc_core::WorkSerializer lock; }
  34. TEST(WorkSerializerTest, ExecuteOneRun) {
  35. grpc_core::WorkSerializer lock;
  36. gpr_event done;
  37. gpr_event_init(&done);
  38. lock.Run([&done]() { gpr_event_set(&done, reinterpret_cast<void*>(1)); },
  39. DEBUG_LOCATION);
  40. EXPECT_TRUE(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)) !=
  41. nullptr);
  42. }
  43. TEST(WorkSerializerTest, ExecuteOneScheduleAndDrain) {
  44. grpc_core::WorkSerializer lock;
  45. gpr_event done;
  46. gpr_event_init(&done);
  47. lock.Schedule([&done]() { gpr_event_set(&done, reinterpret_cast<void*>(1)); },
  48. DEBUG_LOCATION);
  49. EXPECT_EQ(gpr_event_get(&done), nullptr);
  50. lock.DrainQueue();
  51. EXPECT_TRUE(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)) !=
  52. nullptr);
  53. }
  54. class TestThread {
  55. public:
  56. explicit TestThread(grpc_core::WorkSerializer* lock)
  57. : lock_(lock), thread_("grpc_execute_many", ExecuteManyLoop, this) {
  58. gpr_event_init(&done_);
  59. thread_.Start();
  60. }
  61. ~TestThread() {
  62. EXPECT_NE(gpr_event_wait(&done_, gpr_inf_future(GPR_CLOCK_REALTIME)),
  63. nullptr);
  64. thread_.Join();
  65. }
  66. private:
  67. static void ExecuteManyLoop(void* arg) {
  68. TestThread* self = static_cast<TestThread*>(arg);
  69. size_t n = 1;
  70. for (size_t i = 0; i < 10; i++) {
  71. for (size_t j = 0; j < 10000; j++) {
  72. struct ExecutionArgs {
  73. size_t* counter;
  74. size_t value;
  75. };
  76. ExecutionArgs* c = new ExecutionArgs;
  77. c->counter = &self->counter_;
  78. c->value = n++;
  79. self->lock_->Run(
  80. [c]() {
  81. EXPECT_TRUE(*c->counter == c->value - 1);
  82. *c->counter = c->value;
  83. delete c;
  84. },
  85. DEBUG_LOCATION);
  86. }
  87. // sleep for a little bit, to test other threads picking up the load
  88. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  89. }
  90. self->lock_->Run(
  91. [self]() { gpr_event_set(&self->done_, reinterpret_cast<void*>(1)); },
  92. DEBUG_LOCATION);
  93. }
  94. grpc_core::WorkSerializer* lock_ = nullptr;
  95. grpc_core::Thread thread_;
  96. size_t counter_ = 0;
  97. gpr_event done_;
  98. };
  99. TEST(WorkSerializerTest, ExecuteMany) {
  100. grpc_core::WorkSerializer lock;
  101. {
  102. std::vector<std::unique_ptr<TestThread>> threads;
  103. for (size_t i = 0; i < 100; ++i) {
  104. threads.push_back(absl::make_unique<TestThread>(&lock));
  105. }
  106. }
  107. }
  108. class TestThreadScheduleAndDrain {
  109. public:
  110. explicit TestThreadScheduleAndDrain(grpc_core::WorkSerializer* lock)
  111. : lock_(lock), thread_("grpc_execute_many", ExecuteManyLoop, this) {
  112. gpr_event_init(&done_);
  113. thread_.Start();
  114. }
  115. ~TestThreadScheduleAndDrain() {
  116. EXPECT_NE(gpr_event_wait(&done_, gpr_inf_future(GPR_CLOCK_REALTIME)),
  117. nullptr);
  118. thread_.Join();
  119. }
  120. private:
  121. static void ExecuteManyLoop(void* arg) {
  122. TestThreadScheduleAndDrain* self =
  123. static_cast<TestThreadScheduleAndDrain*>(arg);
  124. size_t n = 1;
  125. for (size_t i = 0; i < 10; i++) {
  126. for (size_t j = 0; j < 10000; j++) {
  127. struct ExecutionArgs {
  128. size_t* counter;
  129. size_t value;
  130. };
  131. ExecutionArgs* c = new ExecutionArgs;
  132. c->counter = &self->counter_;
  133. c->value = n++;
  134. self->lock_->Schedule(
  135. [c]() {
  136. EXPECT_TRUE(*c->counter == c->value - 1);
  137. *c->counter = c->value;
  138. delete c;
  139. },
  140. DEBUG_LOCATION);
  141. }
  142. self->lock_->DrainQueue();
  143. // sleep for a little bit, to test other threads picking up the load
  144. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  145. }
  146. self->lock_->Run(
  147. [self]() { gpr_event_set(&self->done_, reinterpret_cast<void*>(1)); },
  148. DEBUG_LOCATION);
  149. }
  150. grpc_core::WorkSerializer* lock_ = nullptr;
  151. grpc_core::Thread thread_;
  152. size_t counter_ = 0;
  153. gpr_event done_;
  154. };
  155. TEST(WorkSerializerTest, ExecuteManyScheduleAndDrain) {
  156. grpc_core::WorkSerializer lock;
  157. {
  158. std::vector<std::unique_ptr<TestThreadScheduleAndDrain>> threads;
  159. for (size_t i = 0; i < 100; ++i) {
  160. threads.push_back(absl::make_unique<TestThreadScheduleAndDrain>(&lock));
  161. }
  162. }
  163. }
  164. TEST(WorkSerializerTest, ExecuteManyMixedRunScheduleAndDrain) {
  165. grpc_core::WorkSerializer lock;
  166. {
  167. std::vector<std::unique_ptr<TestThread>> run_threads;
  168. std::vector<std::unique_ptr<TestThreadScheduleAndDrain>> schedule_threads;
  169. for (size_t i = 0; i < 50; ++i) {
  170. run_threads.push_back(absl::make_unique<TestThread>(&lock));
  171. schedule_threads.push_back(
  172. absl::make_unique<TestThreadScheduleAndDrain>(&lock));
  173. }
  174. }
  175. }
  176. // Tests that work serializers allow destruction from the last callback
  177. TEST(WorkSerializerTest, CallbackDestroysWorkSerializer) {
  178. auto lock = std::make_shared<grpc_core::WorkSerializer>();
  179. lock->Run([&]() { lock.reset(); }, DEBUG_LOCATION);
  180. }
  181. // Tests additional racy conditions when the last callback triggers work
  182. // serializer destruction.
  183. TEST(WorkSerializerTest, WorkSerializerDestructionRace) {
  184. for (int i = 0; i < 1000; ++i) {
  185. auto lock = std::make_shared<grpc_core::WorkSerializer>();
  186. absl::Notification notification;
  187. std::thread t1([&]() {
  188. notification.WaitForNotification();
  189. lock.reset();
  190. });
  191. lock->Run([&]() { notification.Notify(); }, DEBUG_LOCATION);
  192. t1.join();
  193. }
  194. }
  195. // Tests racy conditions when the last callback triggers work
  196. // serializer destruction.
  197. TEST(WorkSerializerTest, WorkSerializerDestructionRaceMultipleThreads) {
  198. auto lock = std::make_shared<grpc_core::WorkSerializer>();
  199. absl::Barrier barrier(51);
  200. std::vector<std::thread> threads;
  201. threads.reserve(50);
  202. for (int i = 0; i < 50; ++i) {
  203. threads.emplace_back([lock, &barrier]() mutable {
  204. barrier.Block();
  205. lock->Run([lock]() mutable { lock.reset(); }, DEBUG_LOCATION);
  206. });
  207. }
  208. barrier.Block();
  209. lock.reset();
  210. for (auto& thread : threads) {
  211. thread.join();
  212. }
  213. }
  214. } // namespace
  215. int main(int argc, char** argv) {
  216. grpc::testing::TestEnvironment env(argc, argv);
  217. ::testing::InitGoogleTest(&argc, argv);
  218. grpc_init();
  219. int retval = RUN_ALL_TESTS();
  220. grpc_shutdown();
  221. return retval;
  222. }