channel_stack_builder_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. *
  3. * Copyright 2017 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/channel/channel_stack_builder.h"
  19. #include <limits.h>
  20. #include <string.h>
  21. #include <gtest/gtest.h>
  22. #include <grpc/grpc_security.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include "src/core/lib/config/core_configuration.h"
  27. #include "src/core/lib/slice/slice_internal.h"
  28. #include "src/core/lib/surface/channel_init.h"
  29. #include "test/core/util/test_config.h"
  30. namespace grpc_core {
  31. namespace testing {
  32. namespace {
  33. grpc_error_handle ChannelInitFunc(grpc_channel_element* /*elem*/,
  34. grpc_channel_element_args* /*args*/) {
  35. return GRPC_ERROR_NONE;
  36. }
  37. grpc_error_handle CallInitFunc(grpc_call_element* /*elem*/,
  38. const grpc_call_element_args* /*args*/) {
  39. return GRPC_ERROR_NONE;
  40. }
  41. void ChannelDestroyFunc(grpc_channel_element* /*elem*/) {}
  42. void CallDestroyFunc(grpc_call_element* /*elem*/,
  43. const grpc_call_final_info* /*final_info*/,
  44. grpc_closure* /*ignored*/) {}
  45. bool g_replacement_fn_called = false;
  46. bool g_original_fn_called = false;
  47. TEST(ChannelStackBuilderTest, ReplaceFilter) {
  48. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  49. grpc_channel* channel =
  50. grpc_channel_create("target name isn't used", creds, nullptr);
  51. grpc_channel_credentials_release(creds);
  52. GPR_ASSERT(channel != nullptr);
  53. // Make sure the high priority filter has been created.
  54. GPR_ASSERT(g_replacement_fn_called);
  55. // ... and that the low priority one hasn't.
  56. GPR_ASSERT(!g_original_fn_called);
  57. grpc_channel_destroy(channel);
  58. }
  59. const grpc_channel_filter replacement_filter = {
  60. grpc_call_next_op,
  61. nullptr,
  62. grpc_channel_next_op,
  63. 0,
  64. CallInitFunc,
  65. grpc_call_stack_ignore_set_pollset_or_pollset_set,
  66. CallDestroyFunc,
  67. 0,
  68. ChannelInitFunc,
  69. ChannelDestroyFunc,
  70. grpc_channel_next_get_info,
  71. "filter_name"};
  72. const grpc_channel_filter original_filter = {
  73. grpc_call_next_op,
  74. nullptr,
  75. grpc_channel_next_op,
  76. 0,
  77. CallInitFunc,
  78. grpc_call_stack_ignore_set_pollset_or_pollset_set,
  79. CallDestroyFunc,
  80. 0,
  81. ChannelInitFunc,
  82. ChannelDestroyFunc,
  83. grpc_channel_next_get_info,
  84. "filter_name"};
  85. bool AddReplacementFilter(ChannelStackBuilder* builder) {
  86. // Get rid of any other version of the filter, as determined by having the
  87. // same name.
  88. auto* stk = builder->mutable_stack();
  89. stk->erase(std::remove_if(stk->begin(), stk->end(),
  90. [](const ChannelStackBuilder::StackEntry& entry) {
  91. return strcmp(entry.filter->name,
  92. "filter_name") == 0;
  93. }),
  94. stk->end());
  95. builder->PrependFilter(&replacement_filter,
  96. [](grpc_channel_stack*, grpc_channel_element*) {
  97. g_replacement_fn_called = true;
  98. });
  99. return true;
  100. }
  101. bool AddOriginalFilter(ChannelStackBuilder* builder) {
  102. builder->PrependFilter(&original_filter,
  103. [](grpc_channel_stack*, grpc_channel_element*) {
  104. g_original_fn_called = true;
  105. });
  106. return true;
  107. }
  108. TEST(ChannelStackBuilder, UnknownTarget) {
  109. ChannelStackBuilder builder("alpha-beta-gamma");
  110. EXPECT_EQ(builder.target(), "unknown");
  111. }
  112. } // namespace
  113. } // namespace testing
  114. } // namespace grpc_core
  115. int main(int argc, char** argv) {
  116. ::testing::InitGoogleTest(&argc, argv);
  117. grpc::testing::TestEnvironment env(argc, argv);
  118. grpc_core::CoreConfiguration::RegisterBuilder(
  119. [](grpc_core::CoreConfiguration::Builder* builder) {
  120. builder->channel_init()->RegisterStage(
  121. GRPC_CLIENT_CHANNEL, INT_MAX,
  122. grpc_core::testing::AddOriginalFilter);
  123. builder->channel_init()->RegisterStage(
  124. GRPC_CLIENT_CHANNEL, INT_MAX,
  125. grpc_core::testing::AddReplacementFilter);
  126. });
  127. grpc_init();
  128. int ret = RUN_ALL_TESTS();
  129. grpc_shutdown();
  130. return ret;
  131. }