Debian Codegraph @

test/core/end2end/fixtures/h2_sockpair_1byte.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/gpr/useful.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
36 #include "src/core/lib/iomgr/endpoint.h"
37 #include "src/core/lib/iomgr/endpoint_pair.h"
38 #include "src/core/lib/iomgr/error.h"
39 #include "src/core/lib/iomgr/exec_ctx.h"
40 #include "src/core/lib/surface/channel.h"
41 #include "src/core/lib/surface/channel_stack_type.h"
42 #include "src/core/lib/surface/completion_queue.h"
43 #include "src/core/lib/surface/server.h"
44 #include "src/core/lib/transport/transport.h"
45 #include "src/core/lib/transport/transport_fwd.h"
46 #include "test/core/end2end/end2end_tests.h"
47 #include "test/core/util/test_config.h"
48
49 /* chttp2 transport that is immediately available (used for testing
50 connected_channel without a client_channel */
51
52 struct custom_fixture_data {
53 grpc_endpoint_pair ep;
54 };
55
56 static void server_setup_transport(void* ts, grpc_transport* transport) {
57 grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts);
58 grpc_core::ExecCtx exec_ctx;
59 custom_fixture_data* fixture_data =
60 static_cast<custom_fixture_data*>(f->fixture_data);
61 grpc_endpoint_add_to_pollset(fixture_data->ep.server, grpc_cq_pollset(f->cq));
62 grpc_core::Server* core_server = grpc_core::Server::FromC(f->server);
63 grpc_error_handle error = core_server->SetupTransport(
64 transport, nullptr, core_server->channel_args(), nullptr);
65 if (error.ok()) {
66 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
67 } else {
68 grpc_transport_destroy(transport);
69 }
70 }
71
72 typedef struct {
73 grpc_end2end_test_fixture* f;
74 const grpc_channel_args* client_args;
75 } sp_client_setup;
76
77 static void client_setup_transport(void* ts, grpc_transport* transport) {
78 sp_client_setup* cs = static_cast<sp_client_setup*>(ts);
79
80 auto args = grpc_core::ChannelArgs::FromC(cs->client_args)
81 .Set(GRPC_ARG_DEFAULT_AUTHORITY, "test-authority");
82 auto channel = grpc_core::Channel::Create(
83 "socketpair-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport);
84 if (channel.ok()) {
85 cs->f->client = channel->release()->c_ptr();
86 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
87 } else {
88 cs->f->client = grpc_lame_client_channel_create(
89 nullptr, static_cast<grpc_status_code>(channel.status().code()),
90 "lame channel");
91 grpc_transport_destroy(transport);
92 }
93 }
94
95 static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
96 const grpc_channel_args* /*client_args*/,
97 const grpc_channel_args* /*server_args*/) {
98 custom_fixture_data* fixture_data = static_cast<custom_fixture_data*>(
99 gpr_malloc(sizeof(custom_fixture_data)));
100 grpc_end2end_test_fixture f;
101 memset(&f, 0, sizeof(f));
102 f.fixture_data = fixture_data;
103 f.cq = grpc_completion_queue_create_for_next(nullptr);
104 grpc_arg a[3];
105 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
106 a[0].type = GRPC_ARG_INTEGER;
107 a[0].value.integer = 1;
108 a[1].key = const_cast<char*>(GRPC_ARG_TCP_MIN_READ_CHUNK_SIZE);
109 a[1].type = GRPC_ARG_INTEGER;
110 a[1].value.integer = 1;
111 a[2].key = const_cast<char*>(GRPC_ARG_TCP_MAX_READ_CHUNK_SIZE);
112 a[2].type = GRPC_ARG_INTEGER;
113 a[2].value.integer = 1;
114 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
115 fixture_data->ep = grpc_iomgr_create_endpoint_pair("fixture", &args);
116 return f;
117 }
118
119 static void chttp2_init_client_socketpair(
120 grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
121 grpc_core::ExecCtx exec_ctx;
122 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
123 grpc_transport* transport;
124 sp_client_setup cs;
125 auto client_channel_args = grpc_core::CoreConfiguration::Get()
126 .channel_args_preconditioning()
127 .PreconditionChannelArgs(client_args);
128 cs.client_args = client_channel_args.ToC().release();
129 cs.f = f;
130 transport = grpc_create_chttp2_transport(client_channel_args,
131 fixture_data->ep.client, true);
132 client_setup_transport(&cs, transport);
133 grpc_channel_args_destroy(cs.client_args);
134 GPR_ASSERT(f->client);
135 }
136
137 static void chttp2_init_server_socketpair(
138 grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
139 grpc_core::ExecCtx exec_ctx;
140 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
141 grpc_transport* transport;
142 GPR_ASSERT(!f->server);
143 f->server = grpc_server_create(server_args, nullptr);
144 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
145 grpc_server_start(f->server);
146 auto server_channel_args = grpc_core::CoreConfiguration::Get()
147 .channel_args_preconditioning()
148 .PreconditionChannelArgs(server_args);
149 transport = grpc_create_chttp2_transport(server_channel_args,
150 fixture_data->ep.server, false);
151 server_setup_transport(f, transport);
152 }
153
154 static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
155 grpc_core::ExecCtx exec_ctx;
156 gpr_free(f->fixture_data);
157 }
158
159 /* All test configurations */
160 static grpc_end2end_test_config configs[] = {
161 {"chttp2/socketpair_one_byte_at_a_time",
162 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
163 chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
164 chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
165 };
166
167 int main(int argc, char** argv) {
168 size_t i;
169
170 g_fixture_slowdown_factor = 2;
171
172 grpc::testing::TestEnvironment env(&argc, argv);
173 grpc_end2end_tests_pre_init();
174 grpc_init();
175
176 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
177 grpc_end2end_tests(argc, argv, configs[i]);
178 }
179
180 grpc_shutdown();
181
182 return 0;
183 }