blob: 49e9c7e969bfb9a0b6bc4d2ea31a918478d17974 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, 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
65static void call_destroy_func(grpc_exec_ctx *exec_ctx,
66 grpc_call_element *elem) {
67 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068}
69
Craig Tillera82950e2015-09-22 12:33:20 -070070static void call_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
71 grpc_transport_stream_op *op) {
72 ++*(int *)(elem->call_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
74
Craig Tillera82950e2015-09-22 12:33:20 -070075static void channel_func(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
76 grpc_transport_op *op) {
77 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078}
79
Craig Tillera82950e2015-09-22 12:33:20 -070080static char *get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
81 return gpr_strdup("peer");
Craig Tillerdfff1b82015-09-21 14:39:57 -070082}
Craig Tiller1b22b9d2015-07-20 13:42:22 -070083
Craig Tiller6c396862016-01-28 13:53:40 -080084static void free_channel(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller7b435612015-11-24 08:15:05 -080085 grpc_channel_stack_destroy(exec_ctx, arg);
86 gpr_free(arg);
87}
88
Craig Tiller6c396862016-01-28 13:53:40 -080089static void free_call(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller7b435612015-11-24 08:15:05 -080090 grpc_call_stack_destroy(exec_ctx, arg);
91 gpr_free(arg);
92}
93
Craig Tillera82950e2015-09-22 12:33:20 -070094static void test_create_channel_stack(void) {
Craig Tillerf40df232016-03-25 13:38:14 -070095 const grpc_channel_filter filter = {call_func,
96 channel_func,
97 sizeof(int),
98 call_init_func,
99 grpc_call_stack_ignore_set_pollset,
100 call_destroy_func,
101 sizeof(int),
102 channel_init_func,
103 channel_destroy_func,
104 get_peer,
105 "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 const grpc_channel_filter *filters = &filter;
107 grpc_channel_stack *channel_stack;
108 grpc_call_stack *call_stack;
109 grpc_channel_element *channel_elem;
110 grpc_call_element *call_elem;
111 grpc_arg arg;
112 grpc_channel_args chan_args;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 int *channel_data;
114 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -0700115 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 arg.type = GRPC_ARG_INTEGER;
118 arg.key = "test_key";
119 arg.value.integer = 42;
120
121 chan_args.num_args = 1;
122 chan_args.args = &arg;
123
Craig Tillera82950e2015-09-22 12:33:20 -0700124 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
Craig Tiller7b435612015-11-24 08:15:05 -0800125 grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters,
Craig Tiller50ec2672015-11-27 21:45:11 -0800126 1, &chan_args, "test", channel_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700127 GPR_ASSERT(channel_stack->count == 1);
128 channel_elem = grpc_channel_stack_element(channel_stack, 0);
129 channel_data = (int *)channel_elem->channel_data;
130 GPR_ASSERT(*channel_data == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
Craig Tillera82950e2015-09-22 12:33:20 -0700132 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller7b435612015-11-24 08:15:05 -0800133 grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, NULL,
134 NULL, call_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700135 GPR_ASSERT(call_stack->count == 1);
136 call_elem = grpc_call_stack_element(call_stack, 0);
137 GPR_ASSERT(call_elem->filter == channel_elem->filter);
138 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
139 call_data = (int *)call_elem->call_data;
140 GPR_ASSERT(*call_data == 0);
141 GPR_ASSERT(*channel_data == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142
Craig Tiller7b435612015-11-24 08:15:05 -0800143 GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done");
144 grpc_exec_ctx_flush(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700145 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146
Craig Tiller7b435612015-11-24 08:15:05 -0800147 GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800148
Craig Tillerb8b1a462015-09-22 12:50:03 -0700149 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150}
151
Craig Tillera82950e2015-09-22 12:33:20 -0700152int main(int argc, char **argv) {
153 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800154 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700155 test_create_channel_stack();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800156 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800158}