init_test.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "src/core/lib/surface/init.h"
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/time.h>
  23. #include "src/core/lib/iomgr/exec_ctx.h"
  24. #include "test/core/util/test_config.h"
  25. static int g_plugin_state;
  26. static void plugin_init(void) { g_plugin_state = 1; }
  27. static void plugin_destroy(void) { g_plugin_state = 2; }
  28. static bool plugin_is_intialized(void) { return g_plugin_state == 1; }
  29. static bool plugin_is_destroyed(void) { return g_plugin_state == 2; }
  30. static void test(int rounds) {
  31. int i;
  32. for (i = 0; i < rounds; i++) {
  33. grpc_init();
  34. }
  35. for (i = 0; i < rounds; i++) {
  36. grpc_shutdown();
  37. }
  38. EXPECT_FALSE(grpc_is_initialized());
  39. }
  40. TEST(Init, test) {
  41. test(1);
  42. test(2);
  43. test(3);
  44. }
  45. static void test_blocking(int rounds) {
  46. int i;
  47. for (i = 0; i < rounds; i++) {
  48. grpc_init();
  49. }
  50. for (i = 0; i < rounds; i++) {
  51. grpc_shutdown_blocking();
  52. }
  53. EXPECT_FALSE(grpc_is_initialized());
  54. }
  55. TEST(Init, blocking) {
  56. test_blocking(1);
  57. test_blocking(2);
  58. test_blocking(3);
  59. }
  60. TEST(Init, shutdown_with_thread) {
  61. grpc_init();
  62. {
  63. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
  64. GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
  65. grpc_shutdown();
  66. }
  67. grpc_maybe_wait_for_async_shutdown();
  68. EXPECT_FALSE(grpc_is_initialized());
  69. }
  70. TEST(Init, mixed) {
  71. grpc_init();
  72. grpc_init();
  73. grpc_shutdown();
  74. grpc_init();
  75. grpc_shutdown();
  76. grpc_shutdown();
  77. EXPECT_FALSE(grpc_is_initialized());
  78. }
  79. TEST(Init, mixed_with_thread) {
  80. grpc_init();
  81. {
  82. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
  83. GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
  84. grpc_init();
  85. grpc_shutdown();
  86. grpc_init();
  87. grpc_shutdown();
  88. grpc_shutdown();
  89. }
  90. grpc_maybe_wait_for_async_shutdown();
  91. EXPECT_FALSE(grpc_is_initialized());
  92. }
  93. TEST(Init, plugin) {
  94. grpc_init();
  95. EXPECT_TRUE(plugin_is_intialized());
  96. grpc_shutdown_blocking();
  97. EXPECT_TRUE(plugin_is_destroyed());
  98. EXPECT_FALSE(grpc_is_initialized());
  99. }
  100. TEST(Init, repeatedly) {
  101. for (int i = 0; i < 10; i++) {
  102. grpc_init();
  103. {
  104. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
  105. GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
  106. grpc_shutdown();
  107. }
  108. }
  109. grpc_maybe_wait_for_async_shutdown();
  110. EXPECT_FALSE(grpc_is_initialized());
  111. }
  112. TEST(Init, repeatedly_blocking) {
  113. for (int i = 0; i < 10; i++) {
  114. grpc_init();
  115. {
  116. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx(
  117. GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD);
  118. grpc_shutdown_blocking();
  119. }
  120. }
  121. EXPECT_FALSE(grpc_is_initialized());
  122. }
  123. int main(int argc, char** argv) {
  124. grpc::testing::TestEnvironment env(argc, argv);
  125. ::testing::InitGoogleTest(&argc, argv);
  126. grpc_register_plugin(plugin_init, plugin_destroy);
  127. return RUN_ALL_TESTS();
  128. }