Debian Codegraph @

test/core/end2end/fixtures/h2_sockpair+trace.cc

raw
1 /*
2 *
3 * Copyright 2015 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 #include <string.h>
20
21 #include "absl/status/status.h"
22 #include "absl/status/statusor.h"
23
24 #include <grpc/grpc.h>
25 #include <grpc/status.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/channel/channel_args_preconditioning.h"
32 #include "src/core/lib/channel/channelz.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/gprpp/global_config_generic.h"
36 #include "src/core/lib/gprpp/ref_counted_ptr.h"
37 #include "src/core/lib/iomgr/endpoint.h"
38 #include "src/core/lib/iomgr/endpoint_pair.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/exec_ctx.h"
41 #include "src/core/lib/iomgr/port.h"
42 #include "src/core/lib/surface/channel.h"
43 #include "src/core/lib/surface/channel_stack_type.h"
44 #include "src/core/lib/surface/completion_queue.h"
45 #include "src/core/lib/surface/server.h"
46 #include "src/core/lib/transport/transport.h"
47 #include "src/core/lib/transport/transport_fwd.h"
48 #include "test/core/end2end/end2end_tests.h"
49 #include "test/core/util/test_config.h"
50
51 #ifdef GRPC_POSIX_SOCKET
52 #include <unistd.h>
53 #endif
54
55 /* chttp2 transport that is immediately available (used for testing
56 connected_channel without a client_channel */
57
58 struct custom_fixture_data {
59 grpc_endpoint_pair ep;
60 };
61
62 static void server_setup_transport(void* ts, grpc_transport* transport) {
63 grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts);
64 grpc_core::ExecCtx exec_ctx;
65 custom_fixture_data* fixture_data =
66 static_cast<custom_fixture_data*>(f->fixture_data);
67 grpc_endpoint_add_to_pollset(fixture_data->ep.server, grpc_cq_pollset(f->cq));
68 grpc_core::Server* core_server = grpc_core::Server::FromC(f->server);
69 grpc_error_handle error = core_server->SetupTransport(
70 transport, nullptr, core_server->channel_args(), nullptr);
71 if (error.ok()) {
72 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
73 } else {
74 grpc_transport_destroy(transport);
75 }
76 }
77
78 typedef struct {
79 grpc_end2end_test_fixture* f;
80 const grpc_channel_args* client_args;
81 } sp_client_setup;
82
83 static void client_setup_transport(void* ts, grpc_transport* transport) {
84 sp_client_setup* cs = static_cast<sp_client_setup*>(ts);
85
86 auto args = grpc_core::ChannelArgs::FromC(cs->client_args)
87 .Set(GRPC_ARG_DEFAULT_AUTHORITY, "test-authority");
88 auto channel = grpc_core::Channel::Create(
89 "socketpair-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport);
90 if (channel.ok()) {
91 cs->f->client = channel->release()->c_ptr();
92 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
93 } else {
94 cs->f->client = grpc_lame_client_channel_create(
95 nullptr, static_cast<grpc_status_code>(channel.status().code()),
96 "lame channel");
97 grpc_transport_destroy(transport);
98 }
99 }
100
101 static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
102 const grpc_channel_args* /*client_args*/,
103 const grpc_channel_args* /*server_args*/) {
104 custom_fixture_data* fixture_data = static_cast<custom_fixture_data*>(
105 gpr_malloc(sizeof(custom_fixture_data)));
106 grpc_end2end_test_fixture f;
107 memset(&f, 0, sizeof(f));
108 f.fixture_data = fixture_data;
109 f.cq = grpc_completion_queue_create_for_next(nullptr);
110 fixture_data->ep = grpc_iomgr_create_endpoint_pair("fixture", nullptr);
111 return f;
112 }
113
114 static void chttp2_init_client_socketpair(
115 grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
116 grpc_core::ExecCtx exec_ctx;
117 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
118 grpc_transport* transport;
119 sp_client_setup cs;
120 cs.client_args = client_args;
121 cs.f = f;
122 auto client_channel_args = grpc_core::CoreConfiguration::Get()
123 .channel_args_preconditioning()
124 .PreconditionChannelArgs(client_args);
125 transport = grpc_create_chttp2_transport(client_channel_args,
126 fixture_data->ep.client, true);
127 client_setup_transport(&cs, transport);
128 GPR_ASSERT(f->client);
129 }
130
131 static void chttp2_init_server_socketpair(
132 grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
133 grpc_core::ExecCtx exec_ctx;
134 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
135 grpc_transport* transport;
136 GPR_ASSERT(!f->server);
137 f->server = grpc_server_create(server_args, nullptr);
138 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
139 grpc_server_start(f->server);
140 auto server_channel_args = grpc_core::CoreConfiguration::Get()
141 .channel_args_preconditioning()
142 .PreconditionChannelArgs(server_args);
143 transport = grpc_create_chttp2_transport(server_channel_args,
144 fixture_data->ep.server, false);
145 server_setup_transport(f, transport);
146 }
147
148 static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
149 grpc_core::ExecCtx exec_ctx;
150 gpr_free(f->fixture_data);
151 }
152
153 /* All test configurations */
154 static grpc_end2end_test_config configs[] = {
155 {"chttp2/socketpair", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
156 chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
157 chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
158 };
159
160 int main(int argc, char** argv) {
161 size_t i;
162
163 /* force tracing on, with a value to force many
164 code paths in trace.c to be taken */
165 GPR_GLOBAL_CONFIG_SET(grpc_trace, "doesnt-exist,http,all");
166
167 #ifdef GRPC_POSIX_SOCKET
168 g_fixture_slowdown_factor = isatty(STDOUT_FILENO) ? 10 : 1;
169 #else
170 g_fixture_slowdown_factor = 10;
171 #endif
172
173 #ifdef GPR_WINDOWS
174 /* on Windows, writing logs to stderr is very slow
175 when stderr is redirected to a disk file.
176 The "trace" tests fixtures generates large amount
177 of logs, so setting a buffer for stderr prevents certain
178 test cases from timing out. */
179 setvbuf(stderr, NULL, _IOLBF, 1024);
180 #endif
181
182 grpc::testing::TestEnvironment env(&argc, argv);
183 grpc_end2end_tests_pre_init();
184 grpc_init();
185
186 GPR_ASSERT(0 == grpc_tracer_set_enabled("also-doesnt-exist", 0));
187 GPR_ASSERT(1 == grpc_tracer_set_enabled("http", 1));
188 GPR_ASSERT(1 == grpc_tracer_set_enabled("all", 1));
189
190 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
191 grpc_end2end_tests(argc, argv, configs[i]);
192 }
193
194 grpc_shutdown();
195
196 return 0;
197 }