Debian Codegraph @

test/core/gprpp/fork_test.cc

raw
1 /*
2 *
3 * Copyright 2017 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
19 #include "src/core/lib/gprpp/fork.h"
20
21 #include <stdint.h>
22
23 #include "gtest/gtest.h"
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/time.h>
27
28 #include "src/core/lib/gprpp/thd.h"
29 #include "test/core/util/test_config.h"
30
31 TEST(ForkTest, Init) {
32 ASSERT_FALSE(grpc_core::Fork::Enabled());
33
34 // Default fork support (disabled)
35 grpc_core::Fork::GlobalInit();
36 ASSERT_FALSE(grpc_core::Fork::Enabled());
37
38 // Explicitly disabled fork support
39 grpc_core::Fork::Enable(false);
40 grpc_core::Fork::GlobalInit();
41 ASSERT_FALSE(grpc_core::Fork::Enabled());
42
43 // Explicitly enabled fork support
44 grpc_core::Fork::Enable(true);
45 grpc_core::Fork::GlobalInit();
46 ASSERT_TRUE(grpc_core::Fork::Enabled());
47 }
48
49 // This spawns CONCURRENT_TEST_THREADS that last up to
50 // THREAD_DELAY_MS, and checks that the Fork::AwaitThreads()
51 // returns roughly after THREAD_DELAY_MS. The epsilon is high
52 // because tsan threads can take a while to spawn/join.
53 #define THREAD_DELAY_MS 6000
54 #define THREAD_DELAY_EPSILON 1500
55 #define CONCURRENT_TEST_THREADS 10
56
57 static void sleeping_thd(void* arg) {
58 int64_t sleep_ms = reinterpret_cast<int64_t>(arg);
59 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
60 gpr_time_from_millis(sleep_ms, GPR_TIMESPAN)));
61 }
62
63 TEST(ForkTest, ThdCount) {
64 // Test no active threads
65 grpc_core::Fork::Enable(true);
66 grpc_core::Fork::GlobalInit();
67 grpc_core::Fork::AwaitThreads();
68
69 grpc_core::Fork::Enable(true);
70 grpc_core::Fork::GlobalInit();
71 grpc_core::Thread thds[CONCURRENT_TEST_THREADS];
72 gpr_timespec est_end_time =
73 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
74 gpr_time_from_millis(THREAD_DELAY_MS, GPR_TIMESPAN));
75 gpr_timespec tolerance = gpr_time_from_millis(
76 THREAD_DELAY_EPSILON * grpc_test_slowdown_factor(), GPR_TIMESPAN);
77 for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) {
78 intptr_t sleep_time_ms =
79 (i * THREAD_DELAY_MS) / (CONCURRENT_TEST_THREADS - 1);
80 thds[i] = grpc_core::Thread("grpc_fork_test", sleeping_thd,
81 reinterpret_cast<void*>(sleep_time_ms));
82 thds[i].Start();
83 }
84 grpc_core::Fork::AwaitThreads();
85 gpr_timespec end_time = gpr_now(GPR_CLOCK_REALTIME);
86 for (auto& thd : thds) {
87 thd.Join();
88 }
89 ASSERT_TRUE(gpr_time_similar(end_time, est_end_time, tolerance));
90 }
91
92 static void exec_ctx_thread(void* arg) {
93 bool* exec_ctx_created = static_cast<bool*>(arg);
94 grpc_core::Fork::IncExecCtxCount();
95 *exec_ctx_created = true;
96 }
97
98 TEST(ForkTest, ExecCount) {
99 grpc_core::Fork::Enable(true);
100 grpc_core::Fork::GlobalInit();
101
102 grpc_core::Fork::IncExecCtxCount();
103 ASSERT_TRUE(grpc_core::Fork::BlockExecCtx());
104 grpc_core::Fork::DecExecCtxCount();
105 grpc_core::Fork::AllowExecCtx();
106
107 grpc_core::Fork::IncExecCtxCount();
108 grpc_core::Fork::IncExecCtxCount();
109 ASSERT_FALSE(grpc_core::Fork::BlockExecCtx());
110 grpc_core::Fork::DecExecCtxCount();
111 grpc_core::Fork::DecExecCtxCount();
112
113 grpc_core::Fork::IncExecCtxCount();
114 ASSERT_TRUE(grpc_core::Fork::BlockExecCtx());
115 grpc_core::Fork::DecExecCtxCount();
116 grpc_core::Fork::AllowExecCtx();
117
118 // Test that block_exec_ctx() blocks grpc_core::Fork::IncExecCtxCount
119 bool exec_ctx_created = false;
120 grpc_core::Thread thd =
121 grpc_core::Thread("grpc_fork_test", exec_ctx_thread, &exec_ctx_created);
122 grpc_core::Fork::IncExecCtxCount();
123 ASSERT_TRUE(grpc_core::Fork::BlockExecCtx());
124 grpc_core::Fork::DecExecCtxCount();
125 thd.Start();
126 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
127 gpr_time_from_seconds(1, GPR_TIMESPAN)));
128 ASSERT_FALSE(exec_ctx_created);
129 grpc_core::Fork::AllowExecCtx();
130 thd.Join(); // This ensure that the call got un-blocked
131 }
132
133 int main(int argc, char** argv) {
134 grpc::testing::TestEnvironment env(&argc, argv);
135 ::testing::InitGoogleTest(&argc, argv);
136 return RUN_ALL_TESTS();
137 }