Debian Codegraph @

test/core/event_engine/forkable_test.cc

raw
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
15 #include <grpc/support/port_platform.h>
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
34 #include "src/core/lib/event_engine/forkable.h"
35
36 namespace {
37 using ::grpc_event_engine::experimental::Forkable;
38 using ::grpc_event_engine::experimental::RegisterForkHandlers;
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();
75 exit(testing::Test::HasFailure());
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);
98 RegisterForkHandlers();
99 auto result = RUN_ALL_TESTS();
100 return result;
101 }
102
103 #endif // GRPC_ENABLE_FORK_SUPPORT