David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 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 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 32 | static grpc_error* channel_init_func(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 33 | grpc_channel_element_args* args) { |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 34 | return GRPC_ERROR_NONE; |
| 35 | } |
| 36 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 37 | static grpc_error* call_init_func(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 38 | const grpc_call_element_args* args) { |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 39 | return GRPC_ERROR_NONE; |
| 40 | } |
| 41 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 42 | static void channel_destroy_func(grpc_channel_element* elem) {} |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 43 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 44 | static void call_destroy_func(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 45 | const grpc_call_final_info* final_info, |
| 46 | grpc_closure* ignored) {} |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 47 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 48 | static void call_func(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 49 | grpc_transport_stream_op_batch* op) {} |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 50 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 51 | static void channel_func(grpc_channel_element* elem, grpc_transport_op* op) { |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 52 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 53 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
| 54 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 55 | GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 58 | bool g_replacement_fn_called = false; |
| 59 | bool g_original_fn_called = false; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 60 | void set_arg_once_fn(grpc_channel_stack* channel_stack, |
| 61 | grpc_channel_element* elem, void* arg) { |
Yash Tibrewal | 40422d5 | 2017-11-06 14:39:17 -0800 | [diff] [blame] | 62 | bool* called = static_cast<bool*>(arg); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 63 | // Make sure this function is only called once per arg. |
| 64 | GPR_ASSERT(*called == false); |
| 65 | *called = true; |
| 66 | } |
| 67 | |
| 68 | static void test_channel_stack_builder_filter_replace(void) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 69 | grpc_channel* channel = |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 70 | grpc_insecure_channel_create("target name isn't used", nullptr, nullptr); |
| 71 | GPR_ASSERT(channel != nullptr); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 72 | // 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 | |
| 79 | const 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 Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 89 | grpc_channel_next_get_info, |
| 90 | "filter_name"}; |
| 91 | |
| 92 | const 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 Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 102 | grpc_channel_next_get_info, |
| 103 | "filter_name"}; |
| 104 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 105 | static bool add_replacement_filter(grpc_channel_stack_builder* builder, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 106 | void* arg) { |
Yash Tibrewal | 40422d5 | 2017-11-06 14:39:17 -0800 | [diff] [blame] | 107 | const grpc_channel_filter* filter = |
| 108 | static_cast<const grpc_channel_filter*>(arg); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 109 | // 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 Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 116 | static bool add_original_filter(grpc_channel_stack_builder* builder, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 117 | void* arg) { |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 118 | return grpc_channel_stack_builder_prepend_filter( |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 119 | builder, static_cast<const grpc_channel_filter*>(arg), set_arg_once_fn, |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 120 | &g_original_fn_called); |
| 121 | } |
| 122 | |
| 123 | static void init_plugin(void) { |
| 124 | grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, |
| 125 | add_original_filter, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 126 | (void*)&original_filter); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 127 | grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, |
| 128 | add_replacement_filter, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 129 | (void*)&replacement_filter); |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void destroy_plugin(void) {} |
| 133 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 134 | int main(int argc, char** argv) { |
David Garcia Quintas | 243fe9d | 2017-08-24 14:16:37 -0700 | [diff] [blame] | 135 | 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 | } |