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 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | static void call_init_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 93b9447 | 2015-11-02 14:18:30 -0800 | [diff] [blame] | 57 | grpc_call_element_args *args) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | ++*(int *)(elem->channel_data); |
| 59 | *(int *)(elem->call_data) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | static void channel_destroy_func(grpc_exec_ctx *exec_ctx, |
| 63 | grpc_channel_element *elem) {} |
| 64 | |
David Garcia Quintas | 9ef0e1c | 2016-04-14 12:44:30 -0700 | [diff] [blame] | 65 | static void call_destroy_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 580987a | 2016-04-29 17:26:33 -0700 | [diff] [blame^] | 66 | const grpc_call_stats *stats, |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 67 | void *ignored) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | ++*(int *)(elem->channel_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | static void call_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 72 | grpc_transport_stream_op *op) { |
| 73 | ++*(int *)(elem->call_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | static void channel_func(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 77 | grpc_transport_op *op) { |
| 78 | ++*(int *)(elem->channel_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | static char *get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 82 | return gpr_strdup("peer"); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 83 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 84 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 85 | static void free_channel(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 86 | grpc_channel_stack_destroy(exec_ctx, arg); |
| 87 | gpr_free(arg); |
| 88 | } |
| 89 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 90 | static void free_call(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
David Garcia Quintas | 580987a | 2016-04-29 17:26:33 -0700 | [diff] [blame^] | 91 | grpc_call_stack_destroy(exec_ctx, arg, NULL, NULL); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 92 | gpr_free(arg); |
| 93 | } |
| 94 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | static void test_create_channel_stack(void) { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 96 | const grpc_channel_filter filter = {call_func, |
| 97 | channel_func, |
| 98 | sizeof(int), |
| 99 | call_init_func, |
| 100 | grpc_call_stack_ignore_set_pollset, |
| 101 | call_destroy_func, |
| 102 | sizeof(int), |
| 103 | channel_init_func, |
| 104 | channel_destroy_func, |
| 105 | get_peer, |
| 106 | "some_test_filter"}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | const grpc_channel_filter *filters = &filter; |
| 108 | grpc_channel_stack *channel_stack; |
| 109 | grpc_call_stack *call_stack; |
| 110 | grpc_channel_element *channel_elem; |
| 111 | grpc_call_element *call_elem; |
| 112 | grpc_arg arg; |
| 113 | grpc_channel_args chan_args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | int *channel_data; |
| 115 | int *call_data; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 116 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | arg.type = GRPC_ARG_INTEGER; |
| 119 | arg.key = "test_key"; |
| 120 | arg.value.integer = 42; |
| 121 | |
| 122 | chan_args.num_args = 1; |
| 123 | chan_args.args = &arg; |
| 124 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1)); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 126 | grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters, |
Craig Tiller | 50ec267 | 2015-11-27 21:45:11 -0800 | [diff] [blame] | 127 | 1, &chan_args, "test", channel_stack); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | GPR_ASSERT(channel_stack->count == 1); |
| 129 | channel_elem = grpc_channel_stack_element(channel_stack, 0); |
| 130 | channel_data = (int *)channel_elem->channel_data; |
| 131 | GPR_ASSERT(*channel_data == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | call_stack = gpr_malloc(channel_stack->call_stack_size); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 134 | grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, NULL, |
| 135 | NULL, call_stack); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | GPR_ASSERT(call_stack->count == 1); |
| 137 | call_elem = grpc_call_stack_element(call_stack, 0); |
| 138 | GPR_ASSERT(call_elem->filter == channel_elem->filter); |
| 139 | GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data); |
| 140 | call_data = (int *)call_elem->call_data; |
| 141 | GPR_ASSERT(*call_data == 0); |
| 142 | GPR_ASSERT(*channel_data == 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 144 | GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done"); |
| 145 | grpc_exec_ctx_flush(&exec_ctx); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | GPR_ASSERT(*channel_data == 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 148 | GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 150 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | int main(int argc, char **argv) { |
| 154 | grpc_test_init(argc, argv); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 155 | grpc_init(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | test_create_channel_stack(); |
Craig Tiller | 0e72ede | 2015-11-19 07:48:53 -0800 | [diff] [blame] | 157 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 158 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 159 | } |