Debian Codegraph @

test/core/channel/minimal_stack_is_minimal_test.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 /*******************************************************************************
20 * This test verifies that various stack configurations result in the set of
21 * filters that we expect.
22 *
23 * This is akin to a golden-file test, and suffers the same disadvantages and
24 * advantages: it reflects that the code as written has not been modified - and
25 * valid code modifications WILL break this test and it will need updating.
26 *
27 * The intent therefore is to allow code reviewers to more easily catch changes
28 * that perturb the generated list of channel filters in different
29 * configurations and assess whether such a change is correct and desirable.
30 */
31
32 #include <stdarg.h>
33 #include <string.h>
34
35 #include <algorithm>
36 #include <string>
37 #include <vector>
38
39 #include "absl/strings/str_join.h"
40
41 #include <grpc/grpc.h>
42 #include <grpc/support/log.h>
43
44 #include "src/core/lib/channel/channel_args.h"
45 #include "src/core/lib/channel/channel_stack.h"
46 #include "src/core/lib/channel/channel_stack_builder_impl.h"
47 #include "src/core/lib/config/core_configuration.h"
48 #include "src/core/lib/iomgr/exec_ctx.h"
49 #include "src/core/lib/surface/channel_init.h"
50 #include "src/core/lib/surface/channel_stack_type.h"
51 #include "src/core/lib/transport/transport_fwd.h"
52 #include "src/core/lib/transport/transport_impl.h"
53 #include "test/core/util/test_config.h"
54
55 // use CHECK_STACK instead
56 static int check_stack(const char* file, int line, const char* transport_name,
57 grpc_channel_args* init_args,
58 unsigned channel_stack_type, ...);
59
60 // arguments: const char *transport_name - the name of the transport type to
61 // simulate
62 // grpc_channel_args *init_args - channel args to pass down
63 // grpc_channel_stack_type channel_stack_type - the archetype of
64 // channel stack to create
65 // variadic arguments - the (in-order) expected list of channel
66 // filters to instantiate, terminated with NULL
67 #define CHECK_STACK(...) check_stack(__FILE__, __LINE__, __VA_ARGS__)
68
69 int main(int argc, char** argv) {
70 grpc::testing::TestEnvironment env(&argc, argv);
71 grpc_init();
72 int errors = 0;
73
74 // tests with a minimal stack
75 grpc_arg minimal_stack_arg;
76 minimal_stack_arg.type = GRPC_ARG_INTEGER;
77 minimal_stack_arg.key = const_cast<char*>(GRPC_ARG_MINIMAL_STACK);
78 minimal_stack_arg.value.integer = 1;
79 grpc_channel_args minimal_stack_args = {1, &minimal_stack_arg};
80 errors +=
81 CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL,
82 "authority", "connected", NULL);
83 errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
84 "authority", "connected", NULL);
85 errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_SERVER_CHANNEL,
86 "server", "connected", NULL);
87 errors +=
88 CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL,
89 "authority", "http-client", "connected", NULL);
90 errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
91 "authority", "http-client", "connected", NULL);
92 errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_SERVER_CHANNEL,
93 "server", "http-server", "connected", NULL);
94 errors += CHECK_STACK(nullptr, &minimal_stack_args, GRPC_CLIENT_CHANNEL,
95 "client-channel", NULL);
96
97 // tests with a default stack
98 errors +=
99 CHECK_STACK("unknown", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
100 "message_size", "deadline", "connected", NULL);
101 errors += CHECK_STACK("unknown", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
102 "message_size", "connected", NULL);
103 errors += CHECK_STACK("unknown", nullptr, GRPC_SERVER_CHANNEL, "server",
104 "message_size", "deadline", "connected", NULL);
105 errors +=
106 CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
107 "message_size", "deadline", "http-client",
108 "message_decompress", "message_compress", "connected", NULL);
109 errors += CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
110 "message_size", "http-client", "message_decompress",
111 "message_compress", "connected", NULL);
112 errors +=
113 CHECK_STACK("chttp2", nullptr, GRPC_SERVER_CHANNEL, "server",
114 "message_size", "deadline", "http-server",
115 "message_decompress", "message_compress", "connected", NULL);
116 errors += CHECK_STACK(nullptr, nullptr, GRPC_CLIENT_CHANNEL, "client-channel",
117 NULL);
118
119 GPR_ASSERT(errors == 0);
120 grpc_shutdown();
121 return 0;
122 }
123
124 /*******************************************************************************
125 * End of tests definitions, start of test infrastructure
126 */
127
128 static int check_stack(const char* file, int line, const char* transport_name,
129 grpc_channel_args* init_args,
130 unsigned channel_stack_type, ...) {
131 grpc_core::ChannelArgs channel_args =
132 grpc_core::ChannelArgs::FromC(init_args);
133 // create phony channel stack
134 grpc_core::ChannelStackBuilderImpl builder(
135 "test", static_cast<grpc_channel_stack_type>(channel_stack_type),
136 channel_args);
137 grpc_transport_vtable fake_transport_vtable;
138 memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable));
139 fake_transport_vtable.name = transport_name;
140 grpc_transport fake_transport = {&fake_transport_vtable};
141 builder.SetTarget("foo.test.google.fr");
142 if (transport_name != nullptr) {
143 builder.SetTransport(&fake_transport);
144 }
145 {
146 grpc_core::ExecCtx exec_ctx;
147 GPR_ASSERT(grpc_core::CoreConfiguration::Get().channel_init().CreateStack(
148 &builder));
149 }
150
151 // build up our expectation list
152 std::vector<std::string> parts;
153 va_list args;
154 va_start(args, channel_stack_type);
155 for (;;) {
156 char* a = va_arg(args, char*);
157 if (a == nullptr) break;
158 parts.push_back(a);
159 }
160 va_end(args);
161 std::string expect = absl::StrJoin(parts, ", ");
162
163 // build up our "got" list
164 parts.clear();
165 for (const auto& entry : *builder.mutable_stack()) {
166 const char* name = entry->name;
167 if (name == nullptr) continue;
168 parts.push_back(name);
169 }
170 std::string got = absl::StrJoin(parts, ", ");
171
172 // figure out result, log if there's an error
173 int result = 0;
174 if (got != expect) {
175 std::string args_str = channel_args.ToString();
176
177 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR,
178 "**************************************************");
179 gpr_log(
180 file, line, GPR_LOG_SEVERITY_ERROR,
181 "FAILED transport=%s; stack_type=%s; channel_args=%s:", transport_name,
182 grpc_channel_stack_type_string(
183 static_cast<grpc_channel_stack_type>(channel_stack_type)),
184 args_str.c_str());
185 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "EXPECTED: %s", expect.c_str());
186 gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "GOT: %s", got.c_str());
187 result = 1;
188 }
189
190 return result;
191 }