Debian Codegraph @

test/core/end2end/tests/high_initial_seqno.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 <stdint.h>
20 #include <string.h>
21
22 #include <string>
23
24 #include "absl/strings/str_cat.h"
25
26 #include <grpc/grpc.h>
27 #include <grpc/impl/codegen/propagation_bits.h>
28 #include <grpc/slice.h>
29 #include <grpc/status.h>
30 #include <grpc/support/log.h>
31
32 #include "test/core/end2end/cq_verifier.h"
33 #include "test/core/end2end/end2end_tests.h"
34 #include "test/core/util/test_config.h"
35
36 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
37
38 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
39 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
42 grpc_end2end_test_fixture f;
43 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
44 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
46 config.init_client(&f, client_args);
47 return f;
48 }
49
50 static gpr_timespec n_seconds_from_now(int n) {
51 return grpc_timeout_seconds_to_deadline(n);
52 }
53
54 static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56 }
57
58 static void drain_cq(grpc_completion_queue* cq) {
59 grpc_event ev;
60 do {
61 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64
65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
67 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
68 grpc_event ev;
69 do {
70 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
71 nullptr);
72 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
73 grpc_server_destroy(f->server);
74 f->server = nullptr;
75 }
76
77 static void shutdown_client(grpc_end2end_test_fixture* f) {
78 if (!f->client) return;
79 grpc_channel_destroy(f->client);
80 f->client = nullptr;
81 }
82
83 static void end_test(grpc_end2end_test_fixture* f) {
84 shutdown_server(f);
85 shutdown_client(f);
86
87 grpc_completion_queue_shutdown(f->cq);
88 drain_cq(f->cq);
89 grpc_completion_queue_destroy(f->cq);
90 }
91
92 static void simple_request_body(grpc_end2end_test_config /*config*/,
93 grpc_end2end_test_fixture f) {
94 grpc_call* c;
95 grpc_call* s;
96 grpc_core::CqVerifier cqv(f.cq);
97 grpc_op ops[6];
98 grpc_op* op;
99 grpc_metadata_array initial_metadata_recv;
100 grpc_metadata_array trailing_metadata_recv;
101 grpc_metadata_array request_metadata_recv;
102 grpc_call_details call_details;
103 grpc_status_code status;
104 grpc_call_error error;
105 grpc_slice details;
106 int was_cancelled = 2;
107
108 gpr_timespec deadline = five_seconds_from_now();
109 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
110 grpc_slice_from_static_string("/foo"), nullptr,
111 deadline, nullptr);
112 GPR_ASSERT(c);
113
114 grpc_metadata_array_init(&initial_metadata_recv);
115 grpc_metadata_array_init(&trailing_metadata_recv);
116 grpc_metadata_array_init(&request_metadata_recv);
117 grpc_call_details_init(&call_details);
118
119 memset(ops, 0, sizeof(ops));
120 op = ops;
121 op->op = GRPC_OP_SEND_INITIAL_METADATA;
122 op->data.send_initial_metadata.count = 0;
123 op->flags = 0;
124 op->reserved = nullptr;
125 op++;
126 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
127 op->flags = 0;
128 op->reserved = nullptr;
129 op++;
130 op->op = GRPC_OP_RECV_INITIAL_METADATA;
131 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
132 op->flags = 0;
133 op->reserved = nullptr;
134 op++;
135 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
136 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
137 op->data.recv_status_on_client.status = &status;
138 op->data.recv_status_on_client.status_details = &details;
139 op->flags = 0;
140 op->reserved = nullptr;
141 op++;
142 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
143 nullptr);
144 GPR_ASSERT(GRPC_CALL_OK == error);
145
146 error =
147 grpc_server_request_call(f.server, &s, &call_details,
148 &request_metadata_recv, f.cq, f.cq, tag(101));
149 GPR_ASSERT(GRPC_CALL_OK == error);
150 cqv.Expect(tag(101), true);
151 cqv.Verify();
152
153 memset(ops, 0, sizeof(ops));
154 op = ops;
155 op->op = GRPC_OP_SEND_INITIAL_METADATA;
156 op->data.send_initial_metadata.count = 0;
157 op->flags = 0;
158 op->reserved = nullptr;
159 op++;
160 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
161 op->data.send_status_from_server.trailing_metadata_count = 0;
162 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
163 grpc_slice status_details = grpc_slice_from_static_string("xyz");
164 op->data.send_status_from_server.status_details = &status_details;
165 op->flags = 0;
166 op->reserved = nullptr;
167 op++;
168 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
169 op->data.recv_close_on_server.cancelled = &was_cancelled;
170 op->flags = 0;
171 op->reserved = nullptr;
172 op++;
173 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
174 nullptr);
175 GPR_ASSERT(GRPC_CALL_OK == error);
176
177 cqv.Expect(tag(102), true);
178 cqv.Expect(tag(1), true);
179 cqv.Verify();
180
181 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
182 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
183 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
184 GPR_ASSERT(was_cancelled == 0);
185
186 grpc_slice_unref(details);
187 grpc_metadata_array_destroy(&initial_metadata_recv);
188 grpc_metadata_array_destroy(&trailing_metadata_recv);
189 grpc_metadata_array_destroy(&request_metadata_recv);
190 grpc_call_details_destroy(&call_details);
191
192 grpc_call_unref(c);
193 grpc_call_unref(s);
194
195 /* TODO(ctiller): this rate limits the test, and it should be removed when
196 retry has been implemented; until then cross-thread chatter
197 may result in some requests needing to be cancelled due to
198 seqno exhaustion. */
199 cqv.VerifyEmpty();
200 }
201
202 static void test_invoke_10_simple_requests(grpc_end2end_test_config config,
203 int initial_sequence_number) {
204 int i;
205 grpc_end2end_test_fixture f;
206 grpc_arg client_arg;
207 grpc_channel_args client_args;
208
209 client_arg.type = GRPC_ARG_INTEGER;
210 client_arg.key = const_cast<char*>(GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER);
211 client_arg.value.integer = initial_sequence_number;
212
213 client_args.num_args = 1;
214 client_args.args = &client_arg;
215
216 std::string name = absl::StrCat("test_invoke_requests first_seqno=",
217 initial_sequence_number);
218 f = begin_test(config, name.c_str(), &client_args, nullptr);
219 for (i = 0; i < 10; i++) {
220 simple_request_body(config, f);
221 gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
222 }
223 end_test(&f);
224 config.tear_down_data(&f);
225 }
226
227 void high_initial_seqno(grpc_end2end_test_config config) {
228 test_invoke_10_simple_requests(config, 16777213);
229 if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) {
230 test_invoke_10_simple_requests(config, 2147483645);
231 }
232 }
233
234 void high_initial_seqno_pre_init(void) {}