Debian Codegraph @

test/core/event_engine/thread_pool_test.cc

raw
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/event_engine/thread_pool.h"
16
17 #include <stdlib.h>
18
19 #include <chrono>
20 #include <thread>
21
22 #include "gtest/gtest.h"
23
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/gprpp/notification.h"
27
28 namespace grpc_event_engine {
29 namespace experimental {
30
31 TEST(ThreadPoolTest, CanRunClosure) {
32 ThreadPool p;
33 grpc_core::Notification n;
34 p.Run([&n] { n.Notify(); });
35 n.WaitForNotification();
36 p.Quiesce();
37 }
38
39 TEST(ThreadPoolTest, CanDestroyInsideClosure) {
40 auto p = std::make_shared<ThreadPool>();
41 grpc_core::Notification n;
42 p->Run([p, &n]() mutable {
43 std::this_thread::sleep_for(std::chrono::seconds(1));
44 // This should delete the thread pool and not deadlock
45 p->Quiesce();
46 p.reset();
47 n.Notify();
48 });
49 // Make sure we're not keeping the thread pool alive from outside the loop
50 p.reset();
51 n.WaitForNotification();
52 }
53
54 TEST(ThreadPoolTest, CanSurviveFork) {
55 ThreadPool p;
56 grpc_core::Notification n;
57 gpr_log(GPR_INFO, "run callback 1");
58 p.Run([&n, &p] {
59 std::this_thread::sleep_for(std::chrono::seconds(1));
60 gpr_log(GPR_INFO, "run callback 2");
61 p.Run([&n] {
62 std::this_thread::sleep_for(std::chrono::seconds(1));
63 gpr_log(GPR_INFO, "notify");
64 n.Notify();
65 });
66 });
67 gpr_log(GPR_INFO, "prepare fork");
68 p.PrepareFork();
69 gpr_log(GPR_INFO, "wait for notification");
70 n.WaitForNotification();
71 gpr_log(GPR_INFO, "postfork child");
72 p.PostforkChild();
73 grpc_core::Notification n2;
74 gpr_log(GPR_INFO, "run callback 3");
75 p.Run([&n2] {
76 gpr_log(GPR_INFO, "notify");
77 n2.Notify();
78 });
79 gpr_log(GPR_INFO, "wait for notification");
80 n2.WaitForNotification();
81 p.Quiesce();
82 }
83
84 void ScheduleSelf(ThreadPool* p) {
85 p->Run([p] { ScheduleSelf(p); });
86 }
87
88 TEST(ThreadPoolDeathTest, CanDetectStucknessAtFork) {
89 ASSERT_DEATH_IF_SUPPORTED(
90 [] {
91 gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR);
92 ThreadPool p;
93 ScheduleSelf(&p);
94 std::thread terminator([] {
95 std::this_thread::sleep_for(std::chrono::seconds(10));
96 abort();
97 });
98 p.PrepareFork();
99 }(),
100 "Waiting for thread pool to idle before forking");
101 }
102
103 void ScheduleTwiceUntilZero(ThreadPool* p, int n) {
104 if (n == 0) return;
105 p->Run([p, n] {
106 ScheduleTwiceUntilZero(p, n - 1);
107 ScheduleTwiceUntilZero(p, n - 1);
108 });
109 }
110
111 TEST(ThreadPoolTest, CanStartLotsOfClosures) {
112 ThreadPool p;
113 // Our first thread pool implementation tried to create ~1M threads for this
114 // test.
115 ScheduleTwiceUntilZero(&p, 20);
116 p.Quiesce();
117 }
118
119 } // namespace experimental
120 } // namespace grpc_event_engine
121
122 int main(int argc, char** argv) {
123 gpr_log_verbosity_init();
124 ::testing::InitGoogleTest(&argc, argv);
125 return RUN_ALL_TESTS();
126 }