blob: eca2a40c979787353230e47b8a8506e0c2a50414 [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>
40#include "test/core/util/test_config.h"
41
Craig Tiller079a11b2015-06-30 10:07:15 -070042static void channel_init_func(grpc_channel_element *elem, grpc_channel *master,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 const grpc_channel_args *args,
44 grpc_mdctx *metadata_context, int is_first,
45 int is_last) {
46 GPR_ASSERT(args->num_args == 1);
47 GPR_ASSERT(args->args[0].type == GRPC_ARG_INTEGER);
48 GPR_ASSERT(0 == strcmp(args->args[0].key, "test_key"));
49 GPR_ASSERT(args->args[0].value.integer == 42);
50 GPR_ASSERT(is_first);
51 GPR_ASSERT(is_last);
52 *(int *)(elem->channel_data) = 0;
53}
54
55static void call_init_func(grpc_call_element *elem,
Craig Tiller1a727fd2015-04-24 13:21:22 -070056 const void *server_transport_data,
Craig Tillerb7959a02015-06-25 08:50:54 -070057 grpc_transport_stream_op *initial_op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 ++*(int *)(elem->channel_data);
59 *(int *)(elem->call_data) = 0;
60}
61
62static void channel_destroy_func(grpc_channel_element *elem) {}
63
64static void call_destroy_func(grpc_call_element *elem) {
65 ++*(int *)(elem->channel_data);
66}
67
Craig Tillerb7959a02015-06-25 08:50:54 -070068static void call_func(grpc_call_element *elem, grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 ++*(int *)(elem->call_data);
70}
71
Craig Tilleracf0f072015-06-29 08:24:16 -070072static void channel_func(grpc_channel_element *elem, grpc_transport_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 ++*(int *)(elem->channel_data);
74}
75
Craig Tiller32946d32015-01-15 11:37:30 -080076static void test_create_channel_stack(void) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080077 const grpc_channel_filter filter = {
Craig Tiller079a11b2015-06-30 10:07:15 -070078 call_func, channel_func, sizeof(int),
79 call_init_func, call_destroy_func, sizeof(int),
80 channel_init_func, channel_destroy_func, "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 const grpc_channel_filter *filters = &filter;
82 grpc_channel_stack *channel_stack;
83 grpc_call_stack *call_stack;
84 grpc_channel_element *channel_elem;
85 grpc_call_element *call_elem;
86 grpc_arg arg;
87 grpc_channel_args chan_args;
88 grpc_mdctx *metadata_context;
89 int *channel_data;
90 int *call_data;
91
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 metadata_context = grpc_mdctx_create();
93
94 arg.type = GRPC_ARG_INTEGER;
95 arg.key = "test_key";
96 arg.value.integer = 42;
97
98 chan_args.num_args = 1;
99 chan_args.args = &arg;
100
101 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
Craig Tiller98465032015-06-29 14:36:42 -0700102 grpc_channel_stack_init(&filters, 1, NULL, &chan_args, metadata_context,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103 channel_stack);
104 GPR_ASSERT(channel_stack->count == 1);
105 channel_elem = grpc_channel_stack_element(channel_stack, 0);
106 channel_data = (int *)channel_elem->channel_data;
107 GPR_ASSERT(*channel_data == 0);
108
109 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller6e84aba2015-04-23 15:08:17 -0700110 grpc_call_stack_init(channel_stack, NULL, NULL, call_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111 GPR_ASSERT(call_stack->count == 1);
112 call_elem = grpc_call_stack_element(call_stack, 0);
113 GPR_ASSERT(call_elem->filter == channel_elem->filter);
114 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
115 call_data = (int *)call_elem->call_data;
116 GPR_ASSERT(*call_data == 0);
117 GPR_ASSERT(*channel_data == 1);
118
119 grpc_call_stack_destroy(call_stack);
120 gpr_free(call_stack);
121 GPR_ASSERT(*channel_data == 2);
122
123 grpc_channel_stack_destroy(channel_stack);
124 gpr_free(channel_stack);
125
Craig Tiller9be83ee2015-02-18 14:16:15 -0800126 grpc_mdctx_unref(metadata_context);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127}
128
129int main(int argc, char **argv) {
130 grpc_test_init(argc, argv);
131 test_create_channel_stack();
132 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800133}