blob: cab31bc69d18d3e6f40a7493dfab2fb95ced4b94 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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
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 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 Tiller7b435612015-11-24 08:15:05 -080084static void free_channel(grpc_exec_ctx *exec_ctx, void *arg, int success) {
85 grpc_channel_stack_destroy(exec_ctx, arg);
86 gpr_free(arg);
87}
88
89static void free_call(grpc_exec_ctx *exec_ctx, void *arg, int success) {
90 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 Tiller71a0f9d2015-09-28 17:22:01 -070095 const grpc_channel_filter filter = {
Craig Tiller93b94472015-11-02 14:18:30 -080096 call_func, channel_func, sizeof(int), call_init_func,
97 grpc_call_stack_ignore_set_pollset, call_destroy_func, sizeof(int),
98 channel_init_func, channel_destroy_func, get_peer, "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099 const grpc_channel_filter *filters = &filter;
100 grpc_channel_stack *channel_stack;
101 grpc_call_stack *call_stack;
102 grpc_channel_element *channel_elem;
103 grpc_call_element *call_elem;
104 grpc_arg arg;
105 grpc_channel_args chan_args;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 int *channel_data;
107 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -0700108 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 arg.type = GRPC_ARG_INTEGER;
111 arg.key = "test_key";
112 arg.value.integer = 42;
113
114 chan_args.num_args = 1;
115 chan_args.args = &arg;
116
Craig Tillera82950e2015-09-22 12:33:20 -0700117 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
Craig Tiller7b435612015-11-24 08:15:05 -0800118 grpc_channel_stack_init(&exec_ctx, 1, free_channel, channel_stack, &filters,
119 1, &chan_args, channel_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700120 GPR_ASSERT(channel_stack->count == 1);
121 channel_elem = grpc_channel_stack_element(channel_stack, 0);
122 channel_data = (int *)channel_elem->channel_data;
123 GPR_ASSERT(*channel_data == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124
Craig Tillera82950e2015-09-22 12:33:20 -0700125 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller7b435612015-11-24 08:15:05 -0800126 grpc_call_stack_init(&exec_ctx, channel_stack, 1, free_call, call_stack, NULL,
127 NULL, call_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700128 GPR_ASSERT(call_stack->count == 1);
129 call_elem = grpc_call_stack_element(call_stack, 0);
130 GPR_ASSERT(call_elem->filter == channel_elem->filter);
131 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
132 call_data = (int *)call_elem->call_data;
133 GPR_ASSERT(*call_data == 0);
134 GPR_ASSERT(*channel_data == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tiller7b435612015-11-24 08:15:05 -0800136 GRPC_CALL_STACK_UNREF(&exec_ctx, call_stack, "done");
137 grpc_exec_ctx_flush(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700138 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139
Craig Tiller7b435612015-11-24 08:15:05 -0800140 GRPC_CHANNEL_STACK_UNREF(&exec_ctx, channel_stack, "done");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141
Craig Tillerb8b1a462015-09-22 12:50:03 -0700142 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143}
144
Craig Tillera82950e2015-09-22 12:33:20 -0700145int main(int argc, char **argv) {
146 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800147 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700148 test_create_channel_stack();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800149 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800151}