xref: /aosp_15_r20/external/grpc-grpc/test/core/channel/channel_stack_builder_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <map>
22 #include <utility>
23 
24 #include "absl/status/status.h"
25 #include "gtest/gtest.h"
26 
27 #include <grpc/grpc.h>
28 
29 #include "src/core/lib/channel/channel_stack.h"
30 #include "src/core/lib/channel/channel_stack_builder_impl.h"
31 #include "src/core/lib/iomgr/closure.h"
32 #include "src/core/lib/iomgr/error.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "test/core/util/test_config.h"
35 
36 namespace grpc_core {
37 namespace testing {
38 namespace {
39 
ChannelInitFunc(grpc_channel_element *,grpc_channel_element_args *)40 grpc_error_handle ChannelInitFunc(grpc_channel_element* /*elem*/,
41                                   grpc_channel_element_args* /*args*/) {
42   return absl::OkStatus();
43 }
44 
CallInitFunc(grpc_call_element *,const grpc_call_element_args *)45 grpc_error_handle CallInitFunc(grpc_call_element* /*elem*/,
46                                const grpc_call_element_args* /*args*/) {
47   return absl::OkStatus();
48 }
49 
ChannelDestroyFunc(grpc_channel_element *)50 void ChannelDestroyFunc(grpc_channel_element* /*elem*/) {}
51 
CallDestroyFunc(grpc_call_element *,const grpc_call_final_info *,grpc_closure *)52 void CallDestroyFunc(grpc_call_element* /*elem*/,
53                      const grpc_call_final_info* /*final_info*/,
54                      grpc_closure* /*ignored*/) {}
55 
FilterNamed(const char * name)56 const grpc_channel_filter* FilterNamed(const char* name) {
57   static auto* filters =
58       new std::map<absl::string_view, const grpc_channel_filter*>;
59   auto it = filters->find(name);
60   if (it != filters->end()) return it->second;
61   return filters
62       ->emplace(
63           name,
64           new grpc_channel_filter{
65               grpc_call_next_op, nullptr, nullptr, grpc_channel_next_op, 0,
66               CallInitFunc, grpc_call_stack_ignore_set_pollset_or_pollset_set,
67               CallDestroyFunc, 0, ChannelInitFunc,
68               [](grpc_channel_stack*, grpc_channel_element*) {},
69               ChannelDestroyFunc, grpc_channel_next_get_info, name})
70       .first->second;
71 }
72 
TEST(ChannelStackBuilder,UnknownTarget)73 TEST(ChannelStackBuilder, UnknownTarget) {
74   ChannelStackBuilderImpl builder("alpha-beta-gamma", GRPC_CLIENT_CHANNEL,
75                                   ChannelArgs());
76   EXPECT_EQ(builder.target(), "unknown");
77 }
78 
TEST(ChannelStackBuilder,CanPrepend)79 TEST(ChannelStackBuilder, CanPrepend) {
80   ExecCtx exec_ctx;
81   ChannelStackBuilderImpl builder("alpha-beta-gamma", GRPC_CLIENT_CHANNEL,
82                                   ChannelArgs());
83   builder.PrependFilter(FilterNamed("filter1"));
84   builder.PrependFilter(FilterNamed("filter2"));
85   auto stack = builder.Build();
86   EXPECT_TRUE(stack.ok());
87   EXPECT_EQ((*stack)->count, 2);
88   EXPECT_EQ(grpc_channel_stack_element(stack->get(), 0)->filter,
89             FilterNamed("filter2"));
90   EXPECT_EQ(grpc_channel_stack_element(stack->get(), 1)->filter,
91             FilterNamed("filter1"));
92 }
93 
TEST(ChannelStackBuilder,CanAppend)94 TEST(ChannelStackBuilder, CanAppend) {
95   ExecCtx exec_ctx;
96   ChannelStackBuilderImpl builder("alpha-beta-gamma", GRPC_CLIENT_CHANNEL,
97                                   ChannelArgs());
98   builder.AppendFilter(FilterNamed("filter1"));
99   builder.AppendFilter(FilterNamed("filter2"));
100   auto stack = builder.Build();
101   EXPECT_TRUE(stack.ok());
102   EXPECT_EQ((*stack)->count, 2);
103   EXPECT_EQ(grpc_channel_stack_element(stack->get(), 0)->filter,
104             FilterNamed("filter1"));
105   EXPECT_EQ(grpc_channel_stack_element(stack->get(), 1)->filter,
106             FilterNamed("filter2"));
107 }
108 
109 }  // namespace
110 }  // namespace testing
111 }  // namespace grpc_core
112 
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114   ::testing::InitGoogleTest(&argc, argv);
115   grpc::testing::TestEnvironment env(&argc, argv);
116   grpc_init();
117   int ret = RUN_ALL_TESTS();
118   grpc_shutdown();
119   return ret;
120 }
121