• 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 <grpc/grpc.h>
20 #include <grpc/support/log.h>
21 #include <grpc/support/time.h>
22 
23 #include "src/core/lib/surface/init.h"
24 #include "test/core/util/test_config.h"
25 
26 static int g_flag;
27 
test(int rounds)28 static void test(int rounds) {
29   int i;
30   for (i = 0; i < rounds; i++) {
31     grpc_init();
32   }
33   for (i = 0; i < rounds; i++) {
34     grpc_shutdown();
35   }
36   grpc_maybe_wait_for_async_shutdown();
37 }
38 
test_blocking(int rounds)39 static void test_blocking(int rounds) {
40   int i;
41   for (i = 0; i < rounds; i++) {
42     grpc_init();
43   }
44   for (i = 0; i < rounds; i++) {
45     grpc_shutdown_blocking();
46   }
47 }
48 
test_mixed(void)49 static void test_mixed(void) {
50   grpc_init();
51   grpc_init();
52   grpc_shutdown();
53   grpc_init();
54   grpc_shutdown();
55   grpc_shutdown();
56   grpc_maybe_wait_for_async_shutdown();
57 }
58 
plugin_init(void)59 static void plugin_init(void) { g_flag = 1; }
plugin_destroy(void)60 static void plugin_destroy(void) { g_flag = 2; }
61 
test_plugin()62 static void test_plugin() {
63   grpc_register_plugin(plugin_init, plugin_destroy);
64   grpc_init();
65   GPR_ASSERT(g_flag == 1);
66   grpc_shutdown_blocking();
67   GPR_ASSERT(g_flag == 2);
68 }
69 
test_repeatedly()70 static void test_repeatedly() {
71   for (int i = 0; i < 1000; i++) {
72     grpc_init();
73     grpc_shutdown();
74   }
75   grpc_maybe_wait_for_async_shutdown();
76 }
77 
test_repeatedly_blocking()78 static void test_repeatedly_blocking() {
79   for (int i = 0; i < 1000; i++) {
80     grpc_init();
81     grpc_shutdown_blocking();
82   }
83 }
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   grpc::testing::TestEnvironment env(argc, argv);
87   test(1);
88   test(2);
89   test(3);
90   test_blocking(1);
91   test_blocking(2);
92   test_blocking(3);
93   test_mixed();
94   test_plugin();
95   test_repeatedly();
96   test_repeatedly_blocking();
97   return 0;
98 }
99