Debian Codegraph @

test/core/surface/init_test.cc

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