• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/event_engine/event_engine.h>
22 #include <grpc/grpc.h>
23 
24 #include <chrono>
25 #include <memory>
26 
27 #include "absl/time/clock.h"
28 #include "absl/time/time.h"
29 #include "gtest/gtest.h"
30 #include "src/core/lib/event_engine/default_event_engine.h"
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/util/thd.h"
33 #include "test/core/test_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,WaitForShutdownBeforeInit)118 TEST(Init, WaitForShutdownBeforeInit) {
119   EXPECT_TRUE(grpc_wait_for_shutdown_with_timeout(absl::ZeroDuration()));
120 }
121 
TEST(Init,WaitForShutdownAfterShutdown)122 TEST(Init, WaitForShutdownAfterShutdown) {
123   grpc_init();
124   grpc_shutdown();
125   EXPECT_TRUE(grpc_wait_for_shutdown_with_timeout(absl::ZeroDuration()));
126 }
127 
TEST(Init,WaitForShutdownWithTimeout)128 TEST(Init, WaitForShutdownWithTimeout) {
129   grpc_init();
130   grpc_init();
131   grpc_shutdown();
132   grpc_core::Thread t0(
133       "init_test",
134       [](void*) {
135         EXPECT_FALSE(grpc_wait_for_shutdown_with_timeout(absl::Seconds(0.5)));
136       },
137       nullptr);
138   grpc_core::Thread t1(
139       "init_test",
140       [](void*) {
141         EXPECT_TRUE(grpc_wait_for_shutdown_with_timeout(absl::Seconds(1.5)));
142       },
143       nullptr);
144   t0.Start();
145   t1.Start();
146   absl::SleepFor(absl::Seconds(1));
147   grpc_shutdown();
148   t0.Join();
149   t1.Join();
150 }
151 
TEST(Init,RepeatedlyBlocking)152 TEST(Init, RepeatedlyBlocking) {
153   for (int i = 0; i < 10; i++) {
154     grpc_init();
155     {
156       grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
157           GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
158       grpc_shutdown_blocking();
159     }
160   }
161   EXPECT_FALSE(grpc_is_initialized());
162 }
163 
TEST(Init,TimerManagerHoldsLastInit)164 TEST(Init, TimerManagerHoldsLastInit) {
165   grpc_init();
166   // the temporary engine is deleted immediately, and the callback owns a copy.
167   auto engine = grpc_event_engine::experimental::GetDefaultEventEngine();
168   engine->RunAfter(
169       std::chrono::seconds(1),
170       [engine = grpc_event_engine::experimental::GetDefaultEventEngine()] {
171         grpc_core::ApplicationCallbackExecCtx app_exec_ctx;
172         grpc_core::ExecCtx exec_ctx;
173         grpc_shutdown();
174       });
175   while (engine.use_count() != 1) {
176     absl::SleepFor(absl::Microseconds(15));
177   }
178 }
179 
main(int argc,char ** argv)180 int main(int argc, char** argv) {
181   grpc::testing::TestEnvironment env(&argc, argv);
182   ::testing::InitGoogleTest(&argc, argv);
183   return RUN_ALL_TESTS();
184 }
185