blob: 08550b4934ec2307222c680c3b64c9a3b9222f93 [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 Tillera82950e2015-09-22 12:33:20 -070084static void test_create_channel_stack(void) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -070085 const grpc_channel_filter filter = {
Craig Tiller93b94472015-11-02 14:18:30 -080086 call_func, channel_func, sizeof(int), call_init_func,
87 grpc_call_stack_ignore_set_pollset, call_destroy_func, sizeof(int),
88 channel_init_func, channel_destroy_func, get_peer, "some_test_filter"};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089 const grpc_channel_filter *filters = &filter;
90 grpc_channel_stack *channel_stack;
91 grpc_call_stack *call_stack;
92 grpc_channel_element *channel_elem;
93 grpc_call_element *call_elem;
94 grpc_arg arg;
95 grpc_channel_args chan_args;
96 grpc_mdctx *metadata_context;
97 int *channel_data;
98 int *call_data;
Craig Tillerf5768a62015-09-22 10:54:34 -070099 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100
Craig Tillera82950e2015-09-22 12:33:20 -0700101 metadata_context = grpc_mdctx_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102
103 arg.type = GRPC_ARG_INTEGER;
104 arg.key = "test_key";
105 arg.value.integer = 42;
106
107 chan_args.num_args = 1;
108 chan_args.args = &arg;
109
Craig Tillera82950e2015-09-22 12:33:20 -0700110 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
111 grpc_channel_stack_init(&exec_ctx, &filters, 1, NULL, &chan_args,
112 metadata_context, channel_stack);
113 GPR_ASSERT(channel_stack->count == 1);
114 channel_elem = grpc_channel_stack_element(channel_stack, 0);
115 channel_data = (int *)channel_elem->channel_data;
116 GPR_ASSERT(*channel_data == 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Craig Tillera82950e2015-09-22 12:33:20 -0700118 call_stack = gpr_malloc(channel_stack->call_stack_size);
Craig Tiller93b94472015-11-02 14:18:30 -0800119 grpc_call_stack_init(&exec_ctx, channel_stack, 0, NULL, NULL, NULL, NULL,
120 call_stack);
Craig Tillera82950e2015-09-22 12:33:20 -0700121 GPR_ASSERT(call_stack->count == 1);
122 call_elem = grpc_call_stack_element(call_stack, 0);
123 GPR_ASSERT(call_elem->filter == channel_elem->filter);
124 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
125 call_data = (int *)call_elem->call_data;
126 GPR_ASSERT(*call_data == 0);
127 GPR_ASSERT(*channel_data == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128
Craig Tillera82950e2015-09-22 12:33:20 -0700129 grpc_call_stack_destroy(&exec_ctx, call_stack);
130 gpr_free(call_stack);
131 GPR_ASSERT(*channel_data == 2);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Craig Tillera82950e2015-09-22 12:33:20 -0700133 grpc_channel_stack_destroy(&exec_ctx, channel_stack);
134 gpr_free(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tillera82950e2015-09-22 12:33:20 -0700136 grpc_mdctx_unref(metadata_context);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700137
Craig Tillerb8b1a462015-09-22 12:50:03 -0700138 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139}
140
Craig Tillera82950e2015-09-22 12:33:20 -0700141int main(int argc, char **argv) {
142 grpc_test_init(argc, argv);
Craig Tiller0e72ede2015-11-19 07:48:53 -0800143 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -0700144 test_create_channel_stack();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800145 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800147}