Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/channel/channel_stack.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 40 | #include <grpc/support/string_util.h> |
| 41 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include "test/core/util/test_config.h" |
| 43 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | static void channel_init_func(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 93b9447 | 2015-11-02 14:18:30 -0800 | [diff] [blame] | 45 | grpc_channel_element *elem, |
| 46 | grpc_channel_element_args *args) { |
| 47 | GPR_ASSERT(args->channel_args->num_args == 1); |
| 48 | GPR_ASSERT(args->channel_args->args[0].type == GRPC_ARG_INTEGER); |
| 49 | GPR_ASSERT(0 == strcmp(args->channel_args->args[0].key, "test_key")); |
| 50 | GPR_ASSERT(args->channel_args->args[0].value.integer == 42); |
| 51 | GPR_ASSERT(args->is_first); |
| 52 | GPR_ASSERT(args->is_last); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | *(int *)(elem->channel_data) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Mark D. Roth | 76d2442 | 2016-06-23 13:22:10 -0700 | [diff] [blame^] | 56 | static grpc_error *call_init_func(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 57 | grpc_call_element *elem, |
| 58 | grpc_call_element_args *args) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | ++*(int *)(elem->channel_data); |
| 60 | *(int *)(elem->call_data) = 0; |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 61 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | static void channel_destroy_func(grpc_exec_ctx *exec_ctx, |
| 65 | grpc_channel_element *elem) {} |
| 66 | |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 67 | static void call_destroy_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | aea5f12 | 2016-05-03 23:20:49 -0700 | [diff] [blame] | 68 | const grpc_call_stats *stats, void *ignored) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 69 | ++*(int *)(elem->channel_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | static void call_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 73 | grpc_transport_stream_op *op) { |
| 74 | ++*(int *)(elem->call_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | static void channel_func(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 78 | grpc_transport_op *op) { |
| 79 | ++*(int *)(elem->channel_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | static char *get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 83 | return gpr_strdup("peer"); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 84 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 85 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 86 | static void free_channel(grpc_exec_ctx *exec_ctx, void *arg, |
| 87 | grpc_error *error) { |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 88 | grpc_channel_stack_destroy(exec_ctx, arg); |
| 89 | gpr_free(arg); |
| 90 | } |
| 91 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 92 | static void free_call(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
David Garcia Quintas | 580987a | 2016-04-29 17:26:33 -0700 | [diff] [blame] | 93 | grpc_call_stack_destroy(exec_ctx, arg, NULL, NULL); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 94 | gpr_free(arg); |
| 95 | } |
| 96 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | static void test_create_channel_stack(void) { |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 98 | const grpc_channel_filter filter = { |
| 99 | call_func, |
| 100 | channel_func, |
| 101 | sizeof(int), |
| 102 | call_init_func, |
| 103 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 104 | call_destroy_func, |
| 105 | sizeof(int), |
| 106 | channel_init_func, |
| 107 | channel_destroy_func, |
| 108 | get_peer, |
| 109 | "some_test_filter"}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | const grpc_channel_filter *filters = &filter; |
| 111 | grpc_channel_stack *channel_stack; |
| 112 | grpc_call_stack *call_stack; |
| 113 | grpc_channel_element *channel_elem; |
| 114 | grpc_call_element *call_elem; |
| 115 | grpc_arg arg; |
| 116 | grpc_channel_args chan_args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | int *channel_data; |
| 118 | int *call_data; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 119 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | arg.type = GRPC_ARG_INTEGER; |
| 122 | arg.key = "test_key"; |
| 123 | arg.value.integer = 42; |
| 124 | |
| 125 | chan_args.num_args = 1; |
| 126 | chan_args.args = &arg; |
| 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1)); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 129 | grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters, |
Craig Tiller | 9d69e80 | 2016-06-06 11:37:50 -0700 | [diff] [blame] | 130 | 1, &chan_args, NULL, "test", channel_stack); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | GPR_ASSERT(channel_stack->count == 1); |
| 132 | channel_elem = grpc_channel_stack_element(channel_stack, 0); |
| 133 | channel_data = (int *)channel_elem->channel_data; |
| 134 | GPR_ASSERT(*channel_data == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | call_stack = gpr_malloc(channel_stack->call_stack_size); |
Mark D. Roth | 76d2442 | 2016-06-23 13:22:10 -0700 | [diff] [blame^] | 137 | grpc_error *error = |
| 138 | grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, |
| 139 | NULL, NULL, call_stack); |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 140 | GPR_ASSERT(error == GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | GPR_ASSERT(call_stack->count == 1); |
| 142 | call_elem = grpc_call_stack_element(call_stack, 0); |
| 143 | GPR_ASSERT(call_elem->filter == channel_elem->filter); |
| 144 | GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data); |
| 145 | call_data = (int *)call_elem->call_data; |
| 146 | GPR_ASSERT(*call_data == 0); |
| 147 | GPR_ASSERT(*channel_data == 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 148 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 149 | GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done"); |
| 150 | grpc_exec_ctx_flush(&exec_ctx); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | GPR_ASSERT(*channel_data == 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 153 | GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 155 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | int main(int argc, char **argv) { |
| 159 | grpc_test_init(argc, argv); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 160 | grpc_init(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | test_create_channel_stack(); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 162 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 164 | } |