Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
| 34 | #include "src/core/channel/channel_stack.h" |
| 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 | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 44 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 45 | channel_init_func (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 46 | { |
| 47 | GPR_ASSERT (args->num_args == 1); |
| 48 | GPR_ASSERT (args->args[0].type == GRPC_ARG_INTEGER); |
| 49 | GPR_ASSERT (0 == strcmp (args->args[0].key, "test_key")); |
| 50 | GPR_ASSERT (args->args[0].value.integer == 42); |
| 51 | GPR_ASSERT (is_first); |
| 52 | GPR_ASSERT (is_last); |
| 53 | *(int *) (elem->channel_data) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 56 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 57 | call_init_func (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 58 | { |
| 59 | ++*(int *) (elem->channel_data); |
| 60 | *(int *) (elem->call_data) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 63 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 64 | channel_destroy_func (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 65 | { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 68 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 69 | call_destroy_func (grpc_exec_ctx * exec_ctx, grpc_call_element * elem) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 70 | { |
| 71 | ++*(int *) (elem->channel_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 74 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 75 | call_func (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 76 | { |
| 77 | ++*(int *) (elem->call_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 80 | static void |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 81 | channel_func (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 82 | { |
| 83 | ++*(int *) (elem->channel_data); |
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 | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 86 | static char * |
Craig Tiller | 1be70cc | 2015-09-22 10:45:28 -0700 | [diff] [blame] | 87 | get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem) |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 88 | { |
| 89 | return gpr_strdup ("peer"); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | test_create_channel_stack (void) |
| 94 | { |
| 95 | const grpc_channel_filter filter = { call_func, channel_func, |
| 96 | sizeof (int), call_init_func, |
| 97 | call_destroy_func, sizeof (int), |
| 98 | channel_init_func, channel_destroy_func, |
| 99 | get_peer, "some_test_filter" |
| 100 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | const grpc_channel_filter *filters = &filter; |
| 102 | grpc_channel_stack *channel_stack; |
| 103 | grpc_call_stack *call_stack; |
| 104 | grpc_channel_element *channel_elem; |
| 105 | grpc_call_element *call_elem; |
| 106 | grpc_arg arg; |
| 107 | grpc_channel_args chan_args; |
| 108 | grpc_mdctx *metadata_context; |
| 109 | int *channel_data; |
| 110 | int *call_data; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame^] | 111 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 113 | metadata_context = grpc_mdctx_create (); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
| 115 | arg.type = GRPC_ARG_INTEGER; |
| 116 | arg.key = "test_key"; |
| 117 | arg.value.integer = 42; |
| 118 | |
| 119 | chan_args.num_args = 1; |
| 120 | chan_args.args = &arg; |
| 121 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 122 | channel_stack = gpr_malloc (grpc_channel_stack_size (&filters, 1)); |
| 123 | grpc_channel_stack_init (&filters, 1, NULL, &chan_args, metadata_context, channel_stack, &closure_list); |
| 124 | GPR_ASSERT (channel_stack->count == 1); |
| 125 | channel_elem = grpc_channel_stack_element (channel_stack, 0); |
| 126 | channel_data = (int *) channel_elem->channel_data; |
| 127 | GPR_ASSERT (*channel_data == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 129 | call_stack = gpr_malloc (channel_stack->call_stack_size); |
| 130 | grpc_call_stack_init (channel_stack, NULL, NULL, call_stack, &closure_list); |
| 131 | GPR_ASSERT (call_stack->count == 1); |
| 132 | call_elem = grpc_call_stack_element (call_stack, 0); |
| 133 | GPR_ASSERT (call_elem->filter == channel_elem->filter); |
| 134 | GPR_ASSERT (call_elem->channel_data == channel_elem->channel_data); |
| 135 | call_data = (int *) call_elem->call_data; |
| 136 | GPR_ASSERT (*call_data == 0); |
| 137 | GPR_ASSERT (*channel_data == 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 139 | grpc_call_stack_destroy (call_stack, &closure_list); |
| 140 | gpr_free (call_stack); |
| 141 | GPR_ASSERT (*channel_data == 2); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 143 | grpc_channel_stack_destroy (channel_stack, &closure_list); |
| 144 | gpr_free (channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 146 | grpc_mdctx_unref (metadata_context); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 147 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 148 | GPR_ASSERT (grpc_closure_list_empty (closure_list)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 151 | int |
| 152 | main (int argc, char **argv) |
| 153 | { |
| 154 | grpc_test_init (argc, argv); |
| 155 | test_create_channel_stack (); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 157 | } |