• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27 
28 #include "src/core/lib/slice/slice_internal.h"
29 #include "src/core/lib/surface/channel_init.h"
30 #include "test/core/util/test_config.h"
31 
channel_init_func(grpc_channel_element *,grpc_channel_element_args *)32 static grpc_error* channel_init_func(grpc_channel_element* /*elem*/,
33                                      grpc_channel_element_args* /*args*/) {
34   return GRPC_ERROR_NONE;
35 }
36 
call_init_func(grpc_call_element *,const grpc_call_element_args *)37 static grpc_error* call_init_func(grpc_call_element* /*elem*/,
38                                   const grpc_call_element_args* /*args*/) {
39   return GRPC_ERROR_NONE;
40 }
41 
channel_destroy_func(grpc_channel_element *)42 static void channel_destroy_func(grpc_channel_element* /*elem*/) {}
43 
call_destroy_func(grpc_call_element *,const grpc_call_final_info *,grpc_closure *)44 static void call_destroy_func(grpc_call_element* /*elem*/,
45                               const grpc_call_final_info* /*final_info*/,
46                               grpc_closure* /*ignored*/) {}
47 
48 bool g_replacement_fn_called = false;
49 bool g_original_fn_called = false;
set_arg_once_fn(grpc_channel_stack *,grpc_channel_element *,void * arg)50 void set_arg_once_fn(grpc_channel_stack* /*channel_stack*/,
51                      grpc_channel_element* /*elem*/, void* arg) {
52   bool* called = static_cast<bool*>(arg);
53   // Make sure this function is only called once per arg.
54   GPR_ASSERT(*called == false);
55   *called = true;
56 }
57 
test_channel_stack_builder_filter_replace(void)58 static void test_channel_stack_builder_filter_replace(void) {
59   grpc_channel* channel =
60       grpc_insecure_channel_create("target name isn't used", nullptr, nullptr);
61   GPR_ASSERT(channel != nullptr);
62   // Make sure the high priority filter has been created.
63   GPR_ASSERT(g_replacement_fn_called);
64   // ... and that the low priority one hasn't.
65   GPR_ASSERT(!g_original_fn_called);
66   grpc_channel_destroy(channel);
67 }
68 
69 const grpc_channel_filter replacement_filter = {
70     grpc_call_next_op,
71     grpc_channel_next_op,
72     0,
73     call_init_func,
74     grpc_call_stack_ignore_set_pollset_or_pollset_set,
75     call_destroy_func,
76     0,
77     channel_init_func,
78     channel_destroy_func,
79     grpc_channel_next_get_info,
80     "filter_name"};
81 
82 const grpc_channel_filter original_filter = {
83     grpc_call_next_op,
84     grpc_channel_next_op,
85     0,
86     call_init_func,
87     grpc_call_stack_ignore_set_pollset_or_pollset_set,
88     call_destroy_func,
89     0,
90     channel_init_func,
91     channel_destroy_func,
92     grpc_channel_next_get_info,
93     "filter_name"};
94 
add_replacement_filter(grpc_channel_stack_builder * builder,void * arg)95 static bool add_replacement_filter(grpc_channel_stack_builder* builder,
96                                    void* arg) {
97   const grpc_channel_filter* filter =
98       static_cast<const grpc_channel_filter*>(arg);
99   // Get rid of any other version of the filter, as determined by having the
100   // same name.
101   GPR_ASSERT(grpc_channel_stack_builder_remove_filter(builder, filter->name));
102   return grpc_channel_stack_builder_prepend_filter(
103       builder, filter, set_arg_once_fn, &g_replacement_fn_called);
104 }
105 
add_original_filter(grpc_channel_stack_builder * builder,void * arg)106 static bool add_original_filter(grpc_channel_stack_builder* builder,
107                                 void* arg) {
108   return grpc_channel_stack_builder_prepend_filter(
109       builder, static_cast<const grpc_channel_filter*>(arg), set_arg_once_fn,
110       &g_original_fn_called);
111 }
112 
init_plugin(void)113 static void init_plugin(void) {
114   grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
115                                    add_original_filter,
116                                    (void*)&original_filter);
117   grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
118                                    add_replacement_filter,
119                                    (void*)&replacement_filter);
120 }
121 
destroy_plugin(void)122 static void destroy_plugin(void) {}
123 
main(int argc,char ** argv)124 int main(int argc, char** argv) {
125   grpc::testing::TestEnvironment env(argc, argv);
126   grpc_register_plugin(init_plugin, destroy_plugin);
127   grpc_init();
128   test_channel_stack_builder_filter_replace();
129   grpc_shutdown();
130   return 0;
131 }
132