blob: 6b371e60a77f042fa70b1aa6cf60e7395d2edc91 [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
David Garcia Quintas9ef0e1c2016-04-14 12:44:30 -070065static void call_destroy_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas580987a2016-04-29 17:26:33 -070066 const grpc_call_stats *stats,
Craig Tiller2c8063c2016-03-22 22:12:15 -070067 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 Tiller6c396862016-01-28 13:53:40 -080085static void free_channel(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller7b435612015-11-24 08:15:05 -080086 grpc_channel_stack_destroy(exec_ctx, arg);
87 gpr_free(arg);
88}
89
Craig Tiller6c396862016-01-28 13:53:40 -080090static void free_call(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
David Garcia Quintas580987a2016-04-29 17:26:33 -070091 grpc_call_stack_destroy(exec_ctx, arg, NULL, NULL);
Craig Tiller7b435612015-11-24 08:15:05 -080092 gpr_free(arg);
93}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095static void test_create_channel_stack(void) {
Craig Tillerf40df232016-03-25 13:38:14 -070096 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800107 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800114 int *channel_data;
115 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -0700116 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118 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 Tillera82950e2015-09-22 12:33:20 -0700125 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
Craig Tiller7b435612015-11-24 08:15:05 -0800126 grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters,
Craig Tiller50ec2672015-11-27 21:45:11 -0800127 1, &chan_args, "test", channel_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700128 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Craig Tillera82950e2015-09-22 12:33:20 -0700133 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller7b435612015-11-24 08:15:05 -0800134 grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, NULL,
135 NULL, call_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700136 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800143
Craig Tiller7b435612015-11-24 08:15:05 -0800144 GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done");
145 grpc_exec_ctx_flush(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700146 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Craig Tiller7b435612015-11-24 08:15:05 -0800148 GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tillerb8b1a462015-09-22 12:50:03 -0700150 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800151}
152
Craig Tillera82950e2015-09-22 12:33:20 -0700153int main(int argc, char **argv) {
154 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800155 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700156 test_create_channel_stack();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800157 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800159}