blob: 2939027891b8b4a1e6589523f9b207d81ee97fda [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,
45 grpc_channel_element *elem, grpc_channel *master,
46 const grpc_channel_args *args,
47 grpc_mdctx *metadata_context, int is_first,
48 int is_last) {
49 GPR_ASSERT(args->num_args == 1);
50 GPR_ASSERT(args->args[0].type == GRPC_ARG_INTEGER);
51 GPR_ASSERT(0 == strcmp(args->args[0].key, "test_key"));
52 GPR_ASSERT(args->args[0].value.integer == 42);
53 GPR_ASSERT(is_first);
54 GPR_ASSERT(is_last);
55 *(int *)(elem->channel_data) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056}
57
Craig Tillera82950e2015-09-22 12:33:20 -070058static void call_init_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
59 const void *server_transport_data,
60 grpc_transport_stream_op *initial_op) {
61 ++*(int *)(elem->channel_data);
62 *(int *)(elem->call_data) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063}
64
Craig Tillera82950e2015-09-22 12:33:20 -070065static void channel_destroy_func(grpc_exec_ctx *exec_ctx,
66 grpc_channel_element *elem) {}
67
68static void call_destroy_func(grpc_exec_ctx *exec_ctx,
69 grpc_call_element *elem) {
70 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071}
72
Craig Tillera82950e2015-09-22 12:33:20 -070073static void call_func(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
74 grpc_transport_stream_op *op) {
75 ++*(int *)(elem->call_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076}
77
Craig Tillera82950e2015-09-22 12:33:20 -070078static void channel_func(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
79 grpc_transport_op *op) {
80 ++*(int *)(elem->channel_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081}
82
Craig Tillera82950e2015-09-22 12:33:20 -070083static char *get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
84 return gpr_strdup("peer");
Craig Tillerdfff1b82015-09-21 14:39:57 -070085}
Craig Tiller1b22b9d2015-07-20 13:42:22 -070086
Craig Tillera82950e2015-09-22 12:33:20 -070087static void test_create_channel_stack(void) {
88 const grpc_channel_filter filter = {call_func, channel_func,
89 sizeof(int), call_init_func,
90 call_destroy_func, sizeof(int),
91 channel_init_func, channel_destroy_func,
92 get_peer, "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 const grpc_channel_filter *filters = &filter;
94 grpc_channel_stack *channel_stack;
95 grpc_call_stack *call_stack;
96 grpc_channel_element *channel_elem;
97 grpc_call_element *call_elem;
98 grpc_arg arg;
99 grpc_channel_args chan_args;
100 grpc_mdctx *metadata_context;
101 int *channel_data;
102 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -0700103 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104
Craig Tillera82950e2015-09-22 12:33:20 -0700105 metadata_context = grpc_mdctx_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
107 arg.type = GRPC_ARG_INTEGER;
108 arg.key = "test_key";
109 arg.value.integer = 42;
110
111 chan_args.num_args = 1;
112 chan_args.args = &arg;
113
Craig Tillera82950e2015-09-22 12:33:20 -0700114 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
115 grpc_channel_stack_init(&exec_ctx, &filters, 1, NULL, &chan_args,
116 metadata_context, channel_stack);
117 GPR_ASSERT(channel_stack->count == 1);
118 channel_elem = grpc_channel_stack_element(channel_stack, 0);
119 channel_data = (int *)channel_elem->channel_data;
120 GPR_ASSERT(*channel_data == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tillera82950e2015-09-22 12:33:20 -0700122 call_stack = gpr_malloc(channel_stack->call_stack_size);
123 grpc_call_stack_init(&exec_ctx, channel_stack, NULL, NULL, call_stack);
124 GPR_ASSERT(call_stack->count == 1);
125 call_elem = grpc_call_stack_element(call_stack, 0);
126 GPR_ASSERT(call_elem->filter == channel_elem->filter);
127 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
128 call_data = (int *)call_elem->call_data;
129 GPR_ASSERT(*call_data == 0);
130 GPR_ASSERT(*channel_data == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
Craig Tillera82950e2015-09-22 12:33:20 -0700132 grpc_call_stack_destroy(&exec_ctx, call_stack);
133 gpr_free(call_stack);
134 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tillera82950e2015-09-22 12:33:20 -0700136 grpc_channel_stack_destroy(&exec_ctx, channel_stack);
137 gpr_free(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138
Craig Tillera82950e2015-09-22 12:33:20 -0700139 grpc_mdctx_unref(metadata_context);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700140
Craig Tillerb8b1a462015-09-22 12:50:03 -0700141 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142}
143
Craig Tillera82950e2015-09-22 12:33:20 -0700144int main(int argc, char **argv) {
145 grpc_test_init(argc, argv);
146 test_create_channel_stack();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800148}