channel_stack_builder_test.cc
Go to the documentation of this file.
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 
20 
21 #include <limits.h>
22 #include <string.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc_security.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 
37 
38 namespace grpc_core {
39 namespace testing {
40 namespace {
41 
42 grpc_error_handle ChannelInitFunc(grpc_channel_element* /*elem*/,
43  grpc_channel_element_args* /*args*/) {
44  return GRPC_ERROR_NONE;
45 }
46 
47 grpc_error_handle CallInitFunc(grpc_call_element* /*elem*/,
48  const grpc_call_element_args* /*args*/) {
49  return GRPC_ERROR_NONE;
50 }
51 
52 void ChannelDestroyFunc(grpc_channel_element* /*elem*/) {}
53 
54 void CallDestroyFunc(grpc_call_element* /*elem*/,
55  const grpc_call_final_info* /*final_info*/,
56  grpc_closure* /*ignored*/) {}
57 
58 bool g_replacement_fn_called = false;
59 bool g_original_fn_called = false;
60 
61 void SetReplacementFnCalled(grpc_channel_stack*, grpc_channel_element*) {
62  g_replacement_fn_called = true;
63 }
64 
65 void SetOriginalFnCalled(grpc_channel_stack*, grpc_channel_element*) {
66  g_original_fn_called = true;
67 }
68 
69 TEST(ChannelStackBuilderTest, ReplaceFilter) {
72  grpc_channel_create("target name isn't used", creds, nullptr);
74  GPR_ASSERT(channel != nullptr);
75  // Make sure the high priority filter has been created.
76  GPR_ASSERT(g_replacement_fn_called);
77  // ... and that the low priority one hasn't.
78  GPR_ASSERT(!g_original_fn_called);
80 }
81 
82 const grpc_channel_filter replacement_filter = {
83  grpc_call_next_op, nullptr,
86  CallDestroyFunc, 0,
87  ChannelInitFunc, SetReplacementFnCalled,
88  ChannelDestroyFunc, grpc_channel_next_get_info,
89  "filter_name"};
90 
91 const grpc_channel_filter original_filter = {
92  grpc_call_next_op, nullptr,
95  CallDestroyFunc, 0,
96  ChannelInitFunc, SetOriginalFnCalled,
97  ChannelDestroyFunc, grpc_channel_next_get_info,
98  "filter_name"};
99 
100 bool AddReplacementFilter(ChannelStackBuilder* builder) {
101  // Get rid of any other version of the filter, as determined by having the
102  // same name.
103  auto* stk = builder->mutable_stack();
104  stk->erase(std::remove_if(stk->begin(), stk->end(),
105  [](const grpc_channel_filter* entry) {
106  return strcmp(entry->name, "filter_name") == 0;
107  }),
108  stk->end());
109  builder->PrependFilter(&replacement_filter);
110  return true;
111 }
112 
113 bool AddOriginalFilter(ChannelStackBuilder* builder) {
114  builder->PrependFilter(&original_filter);
115  return true;
116 }
117 
118 TEST(ChannelStackBuilder, UnknownTarget) {
119  ChannelStackBuilderImpl builder("alpha-beta-gamma", GRPC_CLIENT_CHANNEL);
120  EXPECT_EQ(builder.target(), "unknown");
121 }
122 
123 } // namespace
124 } // namespace testing
125 } // namespace grpc_core
126 
127 int main(int argc, char** argv) {
128  ::testing::InitGoogleTest(&argc, argv);
129  grpc::testing::TestEnvironment env(&argc, argv);
132  builder->channel_init()->RegisterStage(
133  GRPC_CLIENT_CHANNEL, INT_MAX,
134  grpc_core::testing::AddOriginalFilter);
135  builder->channel_init()->RegisterStage(
136  GRPC_CLIENT_CHANNEL, INT_MAX,
137  grpc_core::testing::AddReplacementFilter);
138  });
139  grpc_init();
140  int ret = RUN_ALL_TESTS();
141  grpc_shutdown();
142  return ret;
143 }
testing
Definition: aws_request_signer_test.cc:25
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
core_configuration.h
generate.env
env
Definition: generate.py:37
grpc_channel_stack
Definition: channel_stack.h:202
grpc_channel_next_op
void grpc_channel_next_op(grpc_channel_element *elem, grpc_transport_op *op)
Definition: channel_stack.cc:264
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
string.h
grpc_channel_element
Definition: channel_stack.h:186
grpc_call_stack_ignore_set_pollset_or_pollset_set
void grpc_call_stack_ignore_set_pollset_or_pollset_set(grpc_call_element *, grpc_polling_entity *)
Definition: channel_stack.cc:233
grpc_security.h
grpc_call_element
Definition: channel_stack.h:194
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
string_util.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
channel_init.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
channel_stack.h
grpc_channel_next_get_info
void grpc_channel_next_get_info(grpc_channel_element *elem, const grpc_channel_info *channel_info)
Definition: channel_stack.cc:258
grpc_insecure_credentials_create
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
Definition: core/lib/security/credentials/insecure/insecure_credentials.cc:64
grpc_call_next_op
void grpc_call_next_op(grpc_call_element *elem, grpc_transport_stream_op_batch *op)
Definition: channel_stack.cc:251
grpc_call_element_args
Definition: channel_stack.h:80
slice_internal.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_channel_filter
Definition: channel_stack.h:111
grpc_core::CoreConfiguration::RegisterBuilder
static void RegisterBuilder(std::function< void(Builder *)> builder)
Definition: core_configuration.cc:48
test_config.h
grpc_channel_credentials_release
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)
Definition: credentials.cc:36
grpc_channel_create
GRPCAPI grpc_channel * grpc_channel_create(const char *target, grpc_channel_credentials *creds, const grpc_channel_args *args)
Definition: chttp2_connector.cc:366
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
GRPC_CLIENT_CHANNEL
@ GRPC_CLIENT_CHANNEL
Definition: channel_stack_type.h:26
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
channel_stack_builder_impl.h
grpc_channel_element_args
Definition: channel_stack.h:74
main
int main(int argc, char **argv)
Definition: channel_stack_builder_test.cc:127
grpc_call_final_info
Definition: channel_stack.h:95
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
channel_stack_builder.h
grpc_closure
Definition: closure.h:56
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:44