blob: 806fd0a6cc1ac671d4a7c25a9d496811ec3d4c43 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/channel/channel_stack.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <string.h>
37
38#include <grpc/support/alloc.h>
39#include <grpc/support/log.h>
Craig Tiller1b22b9d2015-07-20 13:42:22 -070040#include <grpc/support/string_util.h>
41
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include "test/core/util/test_config.h"
43
Craig Tillera82950e2015-09-22 12:33:20 -070044static void channel_init_func(grpc_exec_ctx *exec_ctx,
Craig Tiller93b94472015-11-02 14:18:30 -080045 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 Tillera82950e2015-09-22 12:33:20 -070053 *(int *)(elem->channel_data) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054}
55
Craig Tillera82950e2015-09-22 12:33:20 -070056static void call_init_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller93b94472015-11-02 14:18:30 -080057 grpc_call_element_args *args) {
Craig Tillera82950e2015-09-22 12:33:20 -070058 ++*(int *)(elem->channel_data);
59 *(int *)(elem->call_data) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060}
61
Craig Tillera82950e2015-09-22 12:33:20 -070062static void channel_destroy_func(grpc_exec_ctx *exec_ctx,
63 grpc_channel_element *elem) {}
64
Craig Tiller2c8063c2016-03-22 22:12:15 -070065static void call_destroy_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas54fe31a2016-08-01 14:56:26 -070066 const grpc_call_final_info *final_info,
67 void *ignored) {
Craig Tillera82950e2015-09-22 12:33:20 -070068 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069}
70
Craig Tillera82950e2015-09-22 12:33:20 -070071static void call_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
72 grpc_transport_stream_op *op) {
73 ++*(int *)(elem->call_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074}
75
Craig Tillera82950e2015-09-22 12:33:20 -070076static void channel_func(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
77 grpc_transport_op *op) {
78 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079}
80
Craig Tillera82950e2015-09-22 12:33:20 -070081static char *get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
82 return gpr_strdup("peer");
Craig Tillerdfff1b82015-09-21 14:39:57 -070083}
Craig Tiller1b22b9d2015-07-20 13:42:22 -070084
Craig Tillerf707d622016-05-06 14:26:12 -070085static void free_channel(grpc_exec_ctx *exec_ctx, void *arg,
86 grpc_error *error) {
Craig Tiller7b435612015-11-24 08:15:05 -080087 grpc_channel_stack_destroy(exec_ctx, arg);
88 gpr_free(arg);
89}
90
Craig Tillerf707d622016-05-06 14:26:12 -070091static void free_call(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
David Garcia Quintas580987a2016-04-29 17:26:33 -070092 grpc_call_stack_destroy(exec_ctx, arg, NULL, NULL);
Craig Tiller7b435612015-11-24 08:15:05 -080093 gpr_free(arg);
94}
95
Craig Tillera82950e2015-09-22 12:33:20 -070096static void test_create_channel_stack(void) {
David Garcia Quintas4afce7e2016-04-18 16:25:17 -070097 const grpc_channel_filter filter = {
98 call_func,
99 channel_func,
100 sizeof(int),
101 call_init_func,
102 grpc_call_stack_ignore_set_pollset_or_pollset_set,
103 call_destroy_func,
104 sizeof(int),
105 channel_init_func,
106 channel_destroy_func,
107 get_peer,
108 "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 const grpc_channel_filter *filters = &filter;
110 grpc_channel_stack *channel_stack;
111 grpc_call_stack *call_stack;
112 grpc_channel_element *channel_elem;
113 grpc_call_element *call_elem;
114 grpc_arg arg;
115 grpc_channel_args chan_args;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 int *channel_data;
117 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -0700118 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 arg.type = GRPC_ARG_INTEGER;
121 arg.key = "test_key";
122 arg.value.integer = 42;
123
124 chan_args.num_args = 1;
125 chan_args.args = &arg;
126
Craig Tillera82950e2015-09-22 12:33:20 -0700127 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
Craig Tiller7b435612015-11-24 08:15:05 -0800128 grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters,
Craig Tiller9d69e802016-06-06 11:37:50 -0700129 1, &chan_args, NULL, "test", channel_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700130 GPR_ASSERT(channel_stack->count == 1);
131 channel_elem = grpc_channel_stack_element(channel_stack, 0);
132 channel_data = (int *)channel_elem->channel_data;
133 GPR_ASSERT(*channel_data == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134
Craig Tillera82950e2015-09-22 12:33:20 -0700135 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller7b435612015-11-24 08:15:05 -0800136 grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, NULL,
137 NULL, call_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700138 GPR_ASSERT(call_stack->count == 1);
139 call_elem = grpc_call_stack_element(call_stack, 0);
140 GPR_ASSERT(call_elem->filter == channel_elem->filter);
141 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
142 call_data = (int *)call_elem->call_data;
143 GPR_ASSERT(*call_data == 0);
144 GPR_ASSERT(*channel_data == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145
Craig Tiller7b435612015-11-24 08:15:05 -0800146 GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done");
147 grpc_exec_ctx_flush(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700148 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tiller7b435612015-11-24 08:15:05 -0800150 GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800151
Craig Tillerb8b1a462015-09-22 12:50:03 -0700152 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153}
154
Craig Tillera82950e2015-09-22 12:33:20 -0700155int main(int argc, char **argv) {
156 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800157 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700158 test_create_channel_stack();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800159 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800161}