init_test.cc
Go to the documentation of this file.
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 
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26 
29 
30 static int g_plugin_state;
31 
32 static void plugin_init(void) { g_plugin_state = 1; }
33 static void plugin_destroy(void) { g_plugin_state = 2; }
34 static bool plugin_is_intialized(void) { return g_plugin_state == 1; }
35 static bool plugin_is_destroyed(void) { return g_plugin_state == 2; }
36 
37 static void test(int rounds) {
38  int i;
39  for (i = 0; i < rounds; i++) {
40  grpc_init();
41  }
42  for (i = 0; i < rounds; i++) {
43  grpc_shutdown();
44  }
46 }
47 
49  test(1);
50  test(2);
51  test(3);
52 }
53 
54 static void test_blocking(int rounds) {
55  int i;
56  for (i = 0; i < rounds; i++) {
57  grpc_init();
58  }
59  for (i = 0; i < rounds; i++) {
61  }
63 }
64 
65 TEST(Init, blocking) {
66  test_blocking(1);
67  test_blocking(2);
68  test_blocking(3);
69 }
70 
71 TEST(Init, shutdown_with_thread) {
72  grpc_init();
73  {
74  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
76  grpc_shutdown();
77  }
80 }
81 
82 TEST(Init, mixed) {
83  grpc_init();
84  grpc_init();
85  grpc_shutdown();
86  grpc_init();
87  grpc_shutdown();
88  grpc_shutdown();
90 }
91 
92 TEST(Init, mixed_with_thread) {
93  grpc_init();
94  {
95  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
97  grpc_init();
98  grpc_shutdown();
99  grpc_init();
100  grpc_shutdown();
101  grpc_shutdown();
102  }
105 }
106 
107 TEST(Init, plugin) {
108  grpc_init();
113 }
114 
115 TEST(Init, repeatedly) {
116  for (int i = 0; i < 10; i++) {
117  grpc_init();
118  {
119  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
121  grpc_shutdown();
122  }
123  }
126 }
127 
128 TEST(Init, repeatedly_blocking) {
129  for (int i = 0; i < 10; i++) {
130  grpc_init();
131  {
132  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
135  }
136  }
138 }
139 
140 int main(int argc, char** argv) {
141  grpc::testing::TestEnvironment env(&argc, argv);
142  ::testing::InitGoogleTest(&argc, argv);
144  return RUN_ALL_TESTS();
145 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
log.h
test
static void test(int rounds)
Definition: init_test.cc:37
generate.env
env
Definition: generate.py:37
plugin_init
static void plugin_init(void)
Definition: init_test.cc:32
main
int main(int argc, char **argv)
Definition: init_test.cc:140
grpc_shutdown_blocking
GRPCAPI void grpc_shutdown_blocking(void)
Definition: init.cc:238
test
Definition: spinlock_test.cc:36
google::protobuf::python::cmessage::Init
static int Init(CMessage *self, PyObject *args, PyObject *kwargs)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1287
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
time.h
init.h
grpc.h
TEST
TEST(Init, test)
Definition: init_test.cc:48
grpc_register_plugin
GRPCAPI void grpc_register_plugin(void(*init)(void), void(*destroy)(void))
Definition: init.cc:137
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
g_plugin_state
static int g_plugin_state
Definition: init_test.cc:30
test_config.h
GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
#define GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
Definition: exec_ctx.h:55
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_is_initialized
GRPCAPI int grpc_is_initialized(void)
Definition: init.cc:247
test_blocking
static void test_blocking(int rounds)
Definition: init_test.cc:54
plugin_is_intialized
static bool plugin_is_intialized(void)
Definition: init_test.cc:34
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
exec_ctx.h
grpc_maybe_wait_for_async_shutdown
void grpc_maybe_wait_for_async_shutdown(void)
Definition: init.cc:255
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
plugin_destroy
static void plugin_destroy(void)
Definition: init_test.cc:33
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
plugin_is_destroyed
static bool plugin_is_destroyed(void)
Definition: init_test.cc:35
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:03