blob: 44ede2f1d9825e2c414179bea15be5a44beb1095 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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
42#define LOG_TEST_NAME() gpr_log(GPR_INFO, "%s", __FUNCTION__)
43
44static void channel_init_func(grpc_channel_element *elem,
45 const grpc_channel_args *args,
46 grpc_mdctx *metadata_context, int is_first,
47 int is_last) {
48 GPR_ASSERT(args->num_args == 1);
49 GPR_ASSERT(args->args[0].type == GRPC_ARG_INTEGER);
50 GPR_ASSERT(0 == strcmp(args->args[0].key, "test_key"));
51 GPR_ASSERT(args->args[0].value.integer == 42);
52 GPR_ASSERT(is_first);
53 GPR_ASSERT(is_last);
54 *(int *)(elem->channel_data) = 0;
55}
56
57static void call_init_func(grpc_call_element *elem,
58 const void *server_transport_data) {
59 ++*(int *)(elem->channel_data);
60 *(int *)(elem->call_data) = 0;
61}
62
63static void channel_destroy_func(grpc_channel_element *elem) {}
64
65static void call_destroy_func(grpc_call_element *elem) {
66 ++*(int *)(elem->channel_data);
67}
68
69static void call_func(grpc_call_element *elem, grpc_call_op *op) {
70 ++*(int *)(elem->call_data);
71}
72
73static void channel_func(grpc_channel_element *elem, grpc_channel_op *op) {
74 ++*(int *)(elem->channel_data);
75}
76
77static void test_create_channel_stack() {
78 const grpc_channel_filter filter = {
79 call_func, channel_func,
80
81 sizeof(int), call_init_func, call_destroy_func,
82
83 sizeof(int), channel_init_func, channel_destroy_func,
84 };
85 const grpc_channel_filter *filters = &filter;
86 grpc_channel_stack *channel_stack;
87 grpc_call_stack *call_stack;
88 grpc_channel_element *channel_elem;
89 grpc_call_element *call_elem;
90 grpc_arg arg;
91 grpc_channel_args chan_args;
92 grpc_mdctx *metadata_context;
93 int *channel_data;
94 int *call_data;
95
96 LOG_TEST_NAME();
97
98 metadata_context = grpc_mdctx_create();
99
100 arg.type = GRPC_ARG_INTEGER;
101 arg.key = "test_key";
102 arg.value.integer = 42;
103
104 chan_args.num_args = 1;
105 chan_args.args = &arg;
106
107 channel_stack = gpr_malloc(grpc_channel_stack_size(&filters, 1));
108 grpc_channel_stack_init(&filters, 1, &chan_args, metadata_context,
109 channel_stack);
110 GPR_ASSERT(channel_stack->count == 1);
111 channel_elem = grpc_channel_stack_element(channel_stack, 0);
112 channel_data = (int *)channel_elem->channel_data;
113 GPR_ASSERT(*channel_data == 0);
114
115 call_stack = gpr_malloc(channel_stack->call_stack_size);
116 grpc_call_stack_init(channel_stack, NULL, call_stack);
117 GPR_ASSERT(call_stack->count == 1);
118 call_elem = grpc_call_stack_element(call_stack, 0);
119 GPR_ASSERT(call_elem->filter == channel_elem->filter);
120 GPR_ASSERT(call_elem->channel_data == channel_elem->channel_data);
121 call_data = (int *)call_elem->call_data;
122 GPR_ASSERT(*call_data == 0);
123 GPR_ASSERT(*channel_data == 1);
124
125 grpc_call_stack_destroy(call_stack);
126 gpr_free(call_stack);
127 GPR_ASSERT(*channel_data == 2);
128
129 grpc_channel_stack_destroy(channel_stack);
130 gpr_free(channel_stack);
131
132 grpc_mdctx_orphan(metadata_context);
133}
134
135int main(int argc, char **argv) {
136 grpc_test_init(argc, argv);
137 test_create_channel_stack();
138 return 0;
139}