minimal_stack_is_minimal_test.cc
Go to the documentation of this file.
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 <string.h>
33 
34 #include "absl/strings/str_cat.h"
35 #include "absl/strings/str_format.h"
36 #include "absl/strings/str_join.h"
37 
38 #include <grpc/grpc.h>
39 #include <grpc/support/alloc.h>
41 
50 
51 // use CHECK_STACK instead
52 static int check_stack(const char* file, int line, const char* transport_name,
53  grpc_channel_args* init_args,
54  unsigned channel_stack_type, ...);
55 
56 // arguments: const char *transport_name - the name of the transport type to
57 // simulate
58 // grpc_channel_args *init_args - channel args to pass down
59 // grpc_channel_stack_type channel_stack_type - the archetype of
60 // channel stack to create
61 // variadic arguments - the (in-order) expected list of channel
62 // filters to instantiate, terminated with NULL
63 #define CHECK_STACK(...) check_stack(__FILE__, __LINE__, __VA_ARGS__)
64 
65 int main(int argc, char** argv) {
67  grpc_init();
68  int errors = 0;
69 
70  // tests with a minimal stack
71  grpc_arg minimal_stack_arg;
72  minimal_stack_arg.type = GRPC_ARG_INTEGER;
73  minimal_stack_arg.key = const_cast<char*>(GRPC_ARG_MINIMAL_STACK);
74  minimal_stack_arg.value.integer = 1;
75  grpc_channel_args minimal_stack_args = {1, &minimal_stack_arg};
76  errors +=
77  CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL,
78  "authority", "connected", NULL);
79  errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
80  "authority", "connected", NULL);
81  errors += CHECK_STACK("unknown", &minimal_stack_args, GRPC_SERVER_CHANNEL,
82  "server", "connected", NULL);
83  errors +=
84  CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_DIRECT_CHANNEL,
85  "authority", "http-client", "connected", NULL);
86  errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_CLIENT_SUBCHANNEL,
87  "authority", "http-client", "connected", NULL);
88  errors += CHECK_STACK("chttp2", &minimal_stack_args, GRPC_SERVER_CHANNEL,
89  "server", "http-server", "connected", NULL);
90  errors += CHECK_STACK(nullptr, &minimal_stack_args, GRPC_CLIENT_CHANNEL,
91  "client-channel", NULL);
92 
93  // tests with a default stack
94  errors +=
95  CHECK_STACK("unknown", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
96  "message_size", "deadline", "connected", NULL);
97  errors += CHECK_STACK("unknown", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
98  "message_size", "connected", NULL);
99  errors += CHECK_STACK("unknown", nullptr, GRPC_SERVER_CHANNEL, "server",
100  "message_size", "deadline", "connected", NULL);
101  errors +=
102  CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_DIRECT_CHANNEL, "authority",
103  "message_size", "deadline", "http-client",
104  "message_decompress", "message_compress", "connected", NULL);
105  errors += CHECK_STACK("chttp2", nullptr, GRPC_CLIENT_SUBCHANNEL, "authority",
106  "message_size", "http-client", "message_decompress",
107  "message_compress", "connected", NULL);
108  errors +=
109  CHECK_STACK("chttp2", nullptr, GRPC_SERVER_CHANNEL, "server",
110  "message_size", "deadline", "http-server",
111  "message_decompress", "message_compress", "connected", NULL);
112  errors += CHECK_STACK(nullptr, nullptr, GRPC_CLIENT_CHANNEL, "client-channel",
113  NULL);
114 
115  GPR_ASSERT(errors == 0);
116  grpc_shutdown();
117  return 0;
118 }
119 
120 /*******************************************************************************
121  * End of tests definitions, start of test infrastructure
122  */
123 
124 static int check_stack(const char* file, int line, const char* transport_name,
125  grpc_channel_args* init_args,
126  unsigned channel_stack_type, ...) {
127  // create phony channel stack
129  "test", static_cast<grpc_channel_stack_type>(channel_stack_type));
130  grpc_transport_vtable fake_transport_vtable;
131  memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable));
132  fake_transport_vtable.name = transport_name;
133  grpc_transport fake_transport = {&fake_transport_vtable};
134  grpc_core::ChannelArgs channel_args =
136  builder.SetTarget("foo.test.google.fr").SetChannelArgs(channel_args);
137  if (transport_name != nullptr) {
138  builder.SetTransport(&fake_transport);
139  }
140  {
142  GPR_ASSERT(grpc_core::CoreConfiguration::Get().channel_init().CreateStack(
143  &builder));
144  }
145 
146  // build up our expectation list
147  std::vector<std::string> parts;
148  va_list args;
149  va_start(args, channel_stack_type);
150  for (;;) {
151  char* a = va_arg(args, char*);
152  if (a == nullptr) break;
153  parts.push_back(a);
154  }
155  va_end(args);
156  std::string expect = absl::StrJoin(parts, ", ");
157 
158  // build up our "got" list
159  parts.clear();
160  for (const auto& entry : *builder.mutable_stack()) {
161  const char* name = entry->name;
162  if (name == nullptr) continue;
163  parts.push_back(name);
164  }
165  std::string got = absl::StrJoin(parts, ", ");
166 
167  // figure out result, log if there's an error
168  int result = 0;
169  if (got != expect) {
170  std::string args_str = channel_args.ToString();
171 
173  "**************************************************");
174  gpr_log(
176  "FAILED transport=%s; stack_type=%s; channel_args=%s:", transport_name,
178  static_cast<grpc_channel_stack_type>(channel_stack_type)),
179  args_str.c_str());
180  gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "EXPECTED: %s", expect.c_str());
181  gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "GOT: %s", got.c_str());
182  result = 1;
183  }
184 
185  return result;
186 }
grpc_arg
Definition: grpc_types.h:103
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
main
int main(int argc, char **argv)
Definition: minimal_stack_is_minimal_test.cc:65
core_configuration.h
generate.env
env
Definition: generate.py:37
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_arg::value
union grpc_arg::grpc_arg_value value
memset
return memset(p, 0, total)
grpc_transport_vtable
Definition: transport_impl.h:37
GRPC_CLIENT_DIRECT_CHANNEL
@ GRPC_CLIENT_DIRECT_CHANNEL
Definition: channel_stack_type.h:34
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::ChannelArgs::FromC
static ChannelArgs FromC(const grpc_channel_args *args)
Definition: channel_args.cc:84
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
setup.name
name
Definition: setup.py:542
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
GRPC_CLIENT_SUBCHANNEL
@ GRPC_CLIENT_SUBCHANNEL
Definition: channel_stack_type.h:29
check_stack
static int check_stack(const char *file, int line, const char *transport_name, grpc_channel_args *init_args, unsigned channel_stack_type,...)
Definition: minimal_stack_is_minimal_test.cc:124
grpc_channel_args
Definition: grpc_types.h:132
GRPC_SERVER_CHANNEL
@ GRPC_SERVER_CHANNEL
Definition: channel_stack_type.h:36
string_util.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel_init.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
errors
const char * errors
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:841
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
grpc_core::CoreConfiguration::Get
static const CoreConfiguration & Get()
Definition: core_configuration.h:82
channel_stack.h
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc_core::ChannelStackBuilderImpl
Definition: channel_stack_builder_impl.h:33
GPR_LOG_SEVERITY_ERROR
@ GPR_LOG_SEVERITY_ERROR
Definition: include/grpc/impl/codegen/log.h:48
channel_stack_type.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
test_config.h
grpc_core::ChannelArgs::ToString
std::string ToString() const
Definition: channel_args.cc:186
CHECK_STACK
#define CHECK_STACK(...)
Definition: minimal_stack_is_minimal_test.cc:63
GRPC_CLIENT_CHANNEL
@ GRPC_CLIENT_CHANNEL
Definition: channel_stack_type.h:26
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_arg::key
char * key
Definition: grpc_types.h:105
GRPC_ARG_MINIMAL_STACK
#define GRPC_ARG_MINIMAL_STACK
Definition: grpc_types.h:147
grpc_channel_stack_type
grpc_channel_stack_type
Definition: channel_stack_type.h:24
regen-readme.line
line
Definition: regen-readme.py:30
grpc_core::ChannelArgs
Definition: channel_args.h:111
grpc_transport
Definition: transport_impl.h:89
channel_stack_builder_impl.h
transport_impl.h
grpc_arg::grpc_arg_value::integer
int integer
Definition: grpc_types.h:108
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_arg::type
grpc_arg_type type
Definition: grpc_types.h:104
grpc_transport_vtable::name
const char * name
Definition: transport_impl.h:43
grpc_channel_stack_type_string
const char * grpc_channel_stack_type_string(grpc_channel_stack_type type)
Definition: channel_stack_type.cc:41
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:29