forkable_test.cc
Go to the documentation of this file.
1 // Copyright 2022 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 
16 
17 #ifndef GRPC_ENABLE_FORK_SUPPORT
18 
19 // Test nothing, everything is fine
20 int main(int /* argc */, char** /* argv */) { return 0; }
21 
22 #else // GRPC_ENABLE_FORK_SUPPORT
23 
24 #include <sys/wait.h>
25 #include <unistd.h>
26 
27 #include <gtest/gtest.h>
28 
29 #include "absl/time/clock.h"
30 
31 #include <grpc/grpc.h>
32 #include <grpc/support/log.h>
33 
35 
36 namespace {
37 using ::grpc_event_engine::experimental::Forkable;
39 } // namespace
40 
41 class ForkableTest : public testing::Test {};
42 
43 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
44 TEST_F(ForkableTest, BasicPthreadAtForkOperations) {
45  class SomeForkable : public Forkable {
46  public:
47  void PrepareFork() override { prepare_called_ = true; }
48  void PostforkParent() override { parent_called_ = true; }
49  void PostforkChild() override { child_called_ = true; }
50 
51  void CheckParent() {
52  EXPECT_TRUE(prepare_called_);
53  EXPECT_TRUE(parent_called_);
54  EXPECT_FALSE(child_called_);
55  }
56 
57  void CheckChild() {
58  EXPECT_TRUE(prepare_called_);
59  EXPECT_FALSE(parent_called_);
60  EXPECT_TRUE(child_called_);
61  }
62 
63  private:
64  bool prepare_called_ = false;
65  bool parent_called_ = false;
66  bool child_called_ = false;
67  };
68 
69  SomeForkable forkable;
70  int child_pid = fork();
71  ASSERT_NE(child_pid, -1);
72  if (child_pid == 0) {
73  gpr_log(GPR_DEBUG, "I am child pid: %d", getpid());
74  forkable.CheckChild();
76  } else {
77  gpr_log(GPR_DEBUG, "I am parent pid: %d", getpid());
78  forkable.CheckParent();
79  int status;
80  gpr_log(GPR_DEBUG, "Waiting for child pid: %d", child_pid);
81  do {
82  // retry on EINTR, and fail otherwise
83  if (waitpid(child_pid, &status, 0) != -1) break;
84  ASSERT_EQ(errno, EINTR);
85  } while (true);
86  if (WIFEXITED(status)) {
87  ASSERT_EQ(WEXITSTATUS(status), 0);
88  } else {
89  // exited abnormally, fail and print the exit status
90  ASSERT_EQ(-99, status);
91  }
92  }
93 }
94 #endif // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
95 
96 int main(int argc, char** argv) {
97  testing::InitGoogleTest(&argc, argv);
99  auto result = RUN_ALL_TESTS();
100  return result;
101 }
102 
103 #endif // GRPC_ENABLE_FORK_SUPPORT
grpc_event_engine::experimental::RegisterForkHandlers
void RegisterForkHandlers()
Definition: forkable.cc:90
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
grpc_event_engine::experimental::PostforkChild
void PostforkChild()
Definition: forkable.cc:93
log.h
status
absl::Status status
Definition: rls.cc:251
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc_event_engine::experimental::PostforkParent
void PostforkParent()
Definition: forkable.cc:92
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
testing::Test::HasFailure
static bool HasFailure()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:446
grpc.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_event_engine::experimental::PrepareFork
void PrepareFork()
Definition: forkable.cc:91
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
main
int main(int, char **)
Definition: forkable_test.cc:20
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
forkable.h
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367
port_platform.h


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