blob: aad6d6eee9a2527db0eefb4a1b19a46f1125c27c [file] [log] [blame]
David Garcia Quintas243fe9d2017-08-24 14:16:37 -07001/*
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
Yash Tibrewal8cf14702017-12-06 09:47:54 -080032static grpc_error* channel_init_func(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -070033 grpc_channel_element_args* args) {
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070034 return GRPC_ERROR_NONE;
35}
36
Yash Tibrewal8cf14702017-12-06 09:47:54 -080037static grpc_error* call_init_func(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -070038 const grpc_call_element_args* args) {
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070039 return GRPC_ERROR_NONE;
40}
41
Yash Tibrewal8cf14702017-12-06 09:47:54 -080042static void channel_destroy_func(grpc_channel_element* elem) {}
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070043
Yash Tibrewal8cf14702017-12-06 09:47:54 -080044static void call_destroy_func(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -070045 const grpc_call_final_info* final_info,
46 grpc_closure* ignored) {}
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070047
Yash Tibrewal8cf14702017-12-06 09:47:54 -080048static void call_func(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -070049 grpc_transport_stream_op_batch* op) {}
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070050
Yash Tibrewal8cf14702017-12-06 09:47:54 -080051static void channel_func(grpc_channel_element* elem, grpc_transport_op* op) {
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070052 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
53 GRPC_ERROR_UNREF(op->disconnect_with_error);
54 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -080055 GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070056}
57
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070058bool g_replacement_fn_called = false;
59bool g_original_fn_called = false;
Craig Tillerbaa14a92017-11-03 09:09:36 -070060void set_arg_once_fn(grpc_channel_stack* channel_stack,
61 grpc_channel_element* elem, void* arg) {
Yash Tibrewal40422d52017-11-06 14:39:17 -080062 bool* called = static_cast<bool*>(arg);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070063 // Make sure this function is only called once per arg.
64 GPR_ASSERT(*called == false);
65 *called = true;
66}
67
68static void test_channel_stack_builder_filter_replace(void) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070069 grpc_channel* channel =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080070 grpc_insecure_channel_create("target name isn't used", nullptr, nullptr);
71 GPR_ASSERT(channel != nullptr);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070072 // Make sure the high priority filter has been created.
73 GPR_ASSERT(g_replacement_fn_called);
74 // ... and that the low priority one hasn't.
75 GPR_ASSERT(!g_original_fn_called);
76 grpc_channel_destroy(channel);
77}
78
79const grpc_channel_filter replacement_filter = {
80 call_func,
81 channel_func,
82 0,
83 call_init_func,
84 grpc_call_stack_ignore_set_pollset_or_pollset_set,
85 call_destroy_func,
86 0,
87 channel_init_func,
88 channel_destroy_func,
David Garcia Quintas243fe9d2017-08-24 14:16:37 -070089 grpc_channel_next_get_info,
90 "filter_name"};
91
92const grpc_channel_filter original_filter = {
93 call_func,
94 channel_func,
95 0,
96 call_init_func,
97 grpc_call_stack_ignore_set_pollset_or_pollset_set,
98 call_destroy_func,
99 0,
100 channel_init_func,
101 channel_destroy_func,
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700102 grpc_channel_next_get_info,
103 "filter_name"};
104
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800105static bool add_replacement_filter(grpc_channel_stack_builder* builder,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700106 void* arg) {
Yash Tibrewal40422d52017-11-06 14:39:17 -0800107 const grpc_channel_filter* filter =
108 static_cast<const grpc_channel_filter*>(arg);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700109 // Get rid of any other version of the filter, as determined by having the
110 // same name.
111 GPR_ASSERT(grpc_channel_stack_builder_remove_filter(builder, filter->name));
112 return grpc_channel_stack_builder_prepend_filter(
113 builder, filter, set_arg_once_fn, &g_replacement_fn_called);
114}
115
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800116static bool add_original_filter(grpc_channel_stack_builder* builder,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700117 void* arg) {
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700118 return grpc_channel_stack_builder_prepend_filter(
Noah Eisenbe82e642018-02-09 09:16:55 -0800119 builder, static_cast<const grpc_channel_filter*>(arg), set_arg_once_fn,
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700120 &g_original_fn_called);
121}
122
123static void init_plugin(void) {
124 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
125 add_original_filter,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700126 (void*)&original_filter);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700127 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
128 add_replacement_filter,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700129 (void*)&replacement_filter);
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700130}
131
132static void destroy_plugin(void) {}
133
Craig Tillerbaa14a92017-11-03 09:09:36 -0700134int main(int argc, char** argv) {
David Garcia Quintas243fe9d2017-08-24 14:16:37 -0700135 grpc_test_init(argc, argv);
136 grpc_register_plugin(init_plugin, destroy_plugin);
137 grpc_init();
138 test_channel_stack_builder_filter_replace();
139 grpc_shutdown();
140 return 0;
141}