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