xref: /aosp_15_r20/external/grpc-grpc/test/core/surface/init_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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/surface/init.h"
20 
21 #include <chrono>
22 #include <memory>
23 
24 #include "absl/time/clock.h"
25 #include "absl/time/time.h"
26 #include "gtest/gtest.h"
27 
28 #include <grpc/event_engine/event_engine.h>
29 #include <grpc/grpc.h>
30 
31 #include "src/core/lib/event_engine/default_event_engine.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "test/core/util/test_config.h"
34 
test(int rounds)35 static void test(int rounds) {
36   int i;
37   for (i = 0; i < rounds; i++) {
38     grpc_init();
39   }
40   for (i = 0; i < rounds; i++) {
41     grpc_shutdown();
42   }
43   EXPECT_FALSE(grpc_is_initialized());
44 }
45 
TEST(Init,test)46 TEST(Init, test) {
47   test(1);
48   test(2);
49   test(3);
50 }
51 
test_blocking(int rounds)52 static void test_blocking(int rounds) {
53   int i;
54   for (i = 0; i < rounds; i++) {
55     grpc_init();
56   }
57   for (i = 0; i < rounds; i++) {
58     grpc_shutdown_blocking();
59   }
60   EXPECT_FALSE(grpc_is_initialized());
61 }
62 
TEST(Init,blocking)63 TEST(Init, blocking) {
64   test_blocking(1);
65   test_blocking(2);
66   test_blocking(3);
67 }
68 
TEST(Init,ShutdownWithThread)69 TEST(Init, ShutdownWithThread) {
70   grpc_init();
71   {
72     grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
73         GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
74     grpc_shutdown();
75   }
76   grpc_maybe_wait_for_async_shutdown();
77   EXPECT_FALSE(grpc_is_initialized());
78 }
79 
TEST(Init,mixed)80 TEST(Init, mixed) {
81   grpc_init();
82   grpc_init();
83   grpc_shutdown();
84   grpc_init();
85   grpc_shutdown();
86   grpc_shutdown();
87   EXPECT_FALSE(grpc_is_initialized());
88 }
89 
TEST(Init,MixedWithThread)90 TEST(Init, MixedWithThread) {
91   grpc_init();
92   {
93     grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
94         GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
95     grpc_init();
96     grpc_shutdown();
97     grpc_init();
98     grpc_shutdown();
99     grpc_shutdown();
100   }
101   grpc_maybe_wait_for_async_shutdown();
102   EXPECT_FALSE(grpc_is_initialized());
103 }
104 
TEST(Init,Repeatedly)105 TEST(Init, Repeatedly) {
106   for (int i = 0; i < 10; i++) {
107     grpc_init();
108     {
109       grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
110           GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
111       grpc_shutdown();
112     }
113   }
114   grpc_maybe_wait_for_async_shutdown();
115   EXPECT_FALSE(grpc_is_initialized());
116 }
117 
TEST(Init,RepeatedlyBlocking)118 TEST(Init, RepeatedlyBlocking) {
119   for (int i = 0; i < 10; i++) {
120     grpc_init();
121     {
122       grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
123           GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
124       grpc_shutdown_blocking();
125     }
126   }
127   EXPECT_FALSE(grpc_is_initialized());
128 }
129 
TEST(Init,TimerManagerHoldsLastInit)130 TEST(Init, TimerManagerHoldsLastInit) {
131   grpc_init();
132   // the temporary engine is deleted immediately, and the callback owns a copy.
133   auto engine = grpc_event_engine::experimental::GetDefaultEventEngine();
134   engine->RunAfter(
135       std::chrono::seconds(1),
136       [engine = grpc_event_engine::experimental::GetDefaultEventEngine()] {
137         grpc_core::ApplicationCallbackExecCtx app_exec_ctx;
138         grpc_core::ExecCtx exec_ctx;
139         grpc_shutdown();
140       });
141   while (engine.use_count() != 1) {
142     absl::SleepFor(absl::Microseconds(15));
143   }
144 }
145 
main(int argc,char ** argv)146 int main(int argc, char** argv) {
147   grpc::testing::TestEnvironment env(&argc, argv);
148   ::testing::InitGoogleTest(&argc, argv);
149   return RUN_ALL_TESTS();
150 }
151