fork_test.cc
Go to the documentation of this file.
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 
20 
21 #include "src/core/lib/gprpp/thd.h"
23 
24 static void test_init() {
26 
27  // Default fork support (disabled)
31 
32  // Explicitly disabled fork support
37 
38  // Explicitly enabled fork support
43 }
44 
45 // This spawns CONCURRENT_TEST_THREADS that last up to
46 // THREAD_DELAY_MS, and checks that the Fork::AwaitThreads()
47 // returns roughly after THREAD_DELAY_MS. The epsilon is high
48 // because tsan threads can take a while to spawn/join.
49 #define THREAD_DELAY_MS 6000
50 #define THREAD_DELAY_EPSILON 1500
51 #define CONCURRENT_TEST_THREADS 100
52 
53 static void sleeping_thd(void* arg) {
54  int64_t sleep_ms = reinterpret_cast<int64_t>(arg);
57 }
58 
59 static void test_thd_count() {
60  // Test no active threads
65 
69  gpr_timespec est_end_time =
72  gpr_timespec tolerance =
74  for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) {
75  intptr_t sleep_time_ms =
77  thds[i] = grpc_core::Thread("grpc_fork_test", sleeping_thd,
78  reinterpret_cast<void*>(sleep_time_ms));
79  thds[i].Start();
80  }
83  for (auto& thd : thds) {
84  thd.Join();
85  }
86  GPR_ASSERT(gpr_time_similar(end_time, est_end_time, tolerance));
88 }
89 
90 static void exec_ctx_thread(void* arg) {
91  bool* exec_ctx_created = static_cast<bool*>(arg);
93  *exec_ctx_created = true;
94 }
95 
96 static void test_exec_count() {
99 
104 
110 
115 
116  // Test that block_exec_ctx() blocks grpc_core::Fork::IncExecCtxCount
117  bool exec_ctx_created = false;
119  grpc_core::Thread("grpc_fork_test", exec_ctx_thread, &exec_ctx_created);
123  thd.Start();
126  GPR_ASSERT(!exec_ctx_created);
128  thd.Join(); // This ensure that the call got un-blocked
130 }
131 
132 int main(int argc, char* argv[]) {
133  grpc::testing::TestEnvironment env(&argc, argv);
134  test_init();
135  test_thd_count();
136  test_exec_count();
137 
138  return 0;
139 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
fork.h
test_exec_count
static void test_exec_count()
Definition: fork_test.cc:96
gpr_time_similar
GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold)
Definition: src/core/lib/gpr/time.cc:206
grpc_core::Fork::DecExecCtxCount
static void DecExecCtxCount()
Definition: src/core/lib/gprpp/fork.h:57
generate.env
env
Definition: generate.py:37
THREAD_DELAY_MS
#define THREAD_DELAY_MS
Definition: fork_test.cc:49
test_init
static void test_init()
Definition: fork_test.cc:24
end_time
static int64_t end_time
Definition: benchmark-getaddrinfo.c:38
grpc_core::Fork::AllowExecCtx
static void AllowExecCtx()
Definition: fork.cc:213
grpc_core::Fork::Enable
static void Enable(bool enable)
Definition: fork.cc:189
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::Fork::AwaitThreads
static void AwaitThreads()
Definition: fork.cc:230
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
test_thd_count
static void test_thd_count()
Definition: fork_test.cc:59
main
int main(int argc, char *argv[])
Definition: fork_test.cc:132
arg
Definition: cmdline.cc:40
CONCURRENT_TEST_THREADS
#define CONCURRENT_TEST_THREADS
Definition: fork_test.cc:51
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
test_config.h
THREAD_DELAY_EPSILON
#define THREAD_DELAY_EPSILON
Definition: fork_test.cc:50
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
sleeping_thd
static void sleeping_thd(void *arg)
Definition: fork_test.cc:53
grpc_core::Fork::GlobalShutdown
static void GlobalShutdown()
Definition: fork.cc:177
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
thd.h
arg
struct arg arg
grpc_core::Fork::IncExecCtxCount
static void IncExecCtxCount()
Definition: src/core/lib/gprpp/fork.h:50
grpc_core::Thread
Definition: thd.h:43
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
exec_ctx_thread
static void exec_ctx_thread(void *arg)
Definition: fork_test.cc:90
grpc_core::Fork::BlockExecCtx
static bool BlockExecCtx()
Definition: fork.cc:206
profile_analyzer.thd
thd
Definition: profile_analyzer.py:168
gpr_timespec
Definition: gpr_types.h:50
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc_core::Fork::GlobalInit
static void GlobalInit()
Definition: fork.cc:166
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::Fork::Enabled
static bool Enabled()
Definition: fork.cc:184
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22