Debian Codegraph @

test/core/channel/channel_stack_builder_test.cc

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