Debian Codegraph @

test/core/transport/chttp2/streams_not_seen_test.cc

raw
1 //
2 //
3 // Copyright 2022 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 <grpc/support/port_platform.h>
20
21 #include <limits.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <algorithm>
27 #include <atomic>
28 #include <memory>
29 #include <new>
30 #include <string>
31 #include <thread>
32 #include <vector>
33
34 #include "absl/base/thread_annotations.h"
35 #include "absl/status/status.h"
36 #include "absl/strings/match.h"
37 #include "absl/strings/str_cat.h"
38 #include "absl/strings/string_view.h"
39 #include "absl/time/time.h"
40 #include "absl/types/optional.h"
41 #include "gtest/gtest.h"
42
43 #include <grpc/grpc.h>
44 #include <grpc/grpc_security.h>
45 #include <grpc/impl/codegen/propagation_bits.h>
46 #include <grpc/slice.h>
47 #include <grpc/slice_buffer.h>
48 #include <grpc/status.h>
49 #include <grpc/support/alloc.h>
50 #include <grpc/support/log.h>
51
52 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
53 #include "src/core/ext/transport/chttp2/transport/frame_goaway.h"
54 #include "src/core/lib/channel/channel_args.h"
55 #include "src/core/lib/channel/channel_fwd.h"
56 #include "src/core/lib/channel/channel_stack.h"
57 #include "src/core/lib/channel/channel_stack_builder.h"
58 #include "src/core/lib/config/core_configuration.h"
59 #include "src/core/lib/gpr/useful.h"
60 #include "src/core/lib/gprpp/debug_location.h"
61 #include "src/core/lib/gprpp/host_port.h"
62 #include "src/core/lib/gprpp/notification.h"
63 #include "src/core/lib/gprpp/sync.h"
64 #include "src/core/lib/gprpp/time.h"
65 #include "src/core/lib/iomgr/closure.h"
66 #include "src/core/lib/iomgr/endpoint.h"
67 #include "src/core/lib/iomgr/error.h"
68 #include "src/core/lib/iomgr/exec_ctx.h"
69 #include "src/core/lib/iomgr/iomgr_fwd.h"
70 #include "src/core/lib/iomgr/tcp_server.h"
71 #include "src/core/lib/slice/slice.h"
72 #include "src/core/lib/slice/slice_internal.h"
73 #include "src/core/lib/surface/channel_stack_type.h"
74 #include "src/core/lib/transport/metadata_batch.h"
75 #include "src/core/lib/transport/transport.h"
76 #include "test/core/end2end/cq_verifier.h"
77 #include "test/core/util/port.h"
78 #include "test/core/util/test_config.h"
79 #include "test/core/util/test_tcp_server.h"
80
81 namespace grpc_core {
82 namespace {
83
84 void* Tag(intptr_t t) { return reinterpret_cast<void*>(t); }
85
86 // A filter that records state about trailing metadata.
87 class TrailingMetadataRecordingFilter {
88 public:
89 static grpc_channel_filter kFilterVtable;
90
91 static bool trailing_metadata_available() {
92 return trailing_metadata_available_;
93 }
94
95 static void reset_trailing_metadata_available() {
96 trailing_metadata_available_ = false;
97 }
98
99 static absl::optional<GrpcStreamNetworkState::ValueType>
100 stream_network_state() {
101 return stream_network_state_;
102 }
103
104 static void reset_stream_network_state() {
105 stream_network_state_ = absl::nullopt;
106 }
107
108 static void reset_state() {
109 reset_trailing_metadata_available();
110 reset_stream_network_state();
111 }
112
113 private:
114 class CallData {
115 public:
116 static grpc_error_handle Init(grpc_call_element* elem,
117 const grpc_call_element_args* args) {
118 new (elem->call_data) CallData(args);
119 return absl::OkStatus();
120 }
121
122 static void Destroy(grpc_call_element* elem,
123 const grpc_call_final_info* /*final_info*/,
124 grpc_closure* /*ignored*/) {
125 auto* calld = static_cast<CallData*>(elem->call_data);
126 calld->~CallData();
127 }
128
129 static void StartTransportStreamOpBatch(
130 grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
131 auto* calld = static_cast<CallData*>(elem->call_data);
132 if (batch->recv_initial_metadata) {
133 calld->trailing_metadata_available_ =
134 batch->payload->recv_initial_metadata.trailing_metadata_available;
135 calld->original_recv_initial_metadata_ready_ =
136 batch->payload->recv_initial_metadata.recv_initial_metadata_ready;
137 batch->payload->recv_initial_metadata.recv_initial_metadata_ready =
138 &calld->recv_initial_metadata_ready_;
139 }
140 if (batch->recv_trailing_metadata) {
141 calld->recv_trailing_metadata_ =
142 batch->payload->recv_trailing_metadata.recv_trailing_metadata;
143 calld->original_recv_trailing_metadata_ready_ =
144 batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready;
145 batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready =
146 &calld->recv_trailing_metadata_ready_;
147 }
148 grpc_call_next_op(elem, batch);
149 }
150
151 private:
152 explicit CallData(const grpc_call_element_args* /*args*/) {
153 GRPC_CLOSURE_INIT(&recv_initial_metadata_ready_, RecvInitialMetadataReady,
154 this, nullptr);
155 GRPC_CLOSURE_INIT(&recv_trailing_metadata_ready_,
156 RecvTrailingMetadataReady, this, nullptr);
157 }
158
159 static void RecvInitialMetadataReady(void* arg, grpc_error_handle error) {
160 auto* calld = static_cast<CallData*>(arg);
161 TrailingMetadataRecordingFilter::trailing_metadata_available_ =
162 *calld->trailing_metadata_available_;
163 Closure::Run(DEBUG_LOCATION, calld->original_recv_initial_metadata_ready_,
164 error);
165 }
166
167 static void RecvTrailingMetadataReady(void* arg, grpc_error_handle error) {
168 auto* calld = static_cast<CallData*>(arg);
169 stream_network_state_ =
170 calld->recv_trailing_metadata_->get(GrpcStreamNetworkState());
171 Closure::Run(DEBUG_LOCATION,
172 calld->original_recv_trailing_metadata_ready_, error);
173 }
174
175 bool* trailing_metadata_available_ = nullptr;
176 grpc_closure recv_initial_metadata_ready_;
177 grpc_closure* original_recv_initial_metadata_ready_ = nullptr;
178 grpc_metadata_batch* recv_trailing_metadata_ = nullptr;
179 grpc_closure recv_trailing_metadata_ready_;
180 grpc_closure* original_recv_trailing_metadata_ready_ = nullptr;
181 };
182
183 static grpc_error_handle Init(grpc_channel_element* elem,
184 grpc_channel_element_args* /*args*/) {
185 new (elem->channel_data) TrailingMetadataRecordingFilter();
186 return absl::OkStatus();
187 }
188
189 static void Destroy(grpc_channel_element* elem) {
190 auto* chand =
191 static_cast<TrailingMetadataRecordingFilter*>(elem->channel_data);
192 chand->~TrailingMetadataRecordingFilter();
193 }
194
195 static bool trailing_metadata_available_;
196 static absl::optional<GrpcStreamNetworkState::ValueType>
197 stream_network_state_;
198 };
199
200 grpc_channel_filter TrailingMetadataRecordingFilter::kFilterVtable = {
201 CallData::StartTransportStreamOpBatch,
202 nullptr,
203 grpc_channel_next_op,
204 sizeof(CallData),
205 CallData::Init,
206 grpc_call_stack_ignore_set_pollset_or_pollset_set,
207 CallData::Destroy,
208 sizeof(TrailingMetadataRecordingFilter),
209 Init,
210 grpc_channel_stack_no_post_init,
211 Destroy,
212 grpc_channel_next_get_info,
213 "trailing-metadata-recording-filter",
214 };
215 bool TrailingMetadataRecordingFilter::trailing_metadata_available_;
216 absl::optional<GrpcStreamNetworkState::ValueType>
217 TrailingMetadataRecordingFilter::stream_network_state_;
218
219 class StreamsNotSeenTest : public ::testing::Test {
220 protected:
221 explicit StreamsNotSeenTest(bool server_allows_streams = true)
222 : server_allows_streams_(server_allows_streams) {
223 // Reset the filter state
224 TrailingMetadataRecordingFilter::reset_state();
225 grpc_slice_buffer_init(&read_buffer_);
226 GRPC_CLOSURE_INIT(&on_read_done_, OnReadDone, this, nullptr);
227 // Start the test tcp server
228 port_ = grpc_pick_unused_port_or_die();
229 test_tcp_server_init(&server_, OnConnect, this);
230 test_tcp_server_start(&server_, port_);
231 // Start polling on the test tcp server
232 server_poll_thread_ = std::make_unique<std::thread>([this]() {
233 while (!shutdown_) {
234 test_tcp_server_poll(&server_, 10);
235 }
236 });
237 // Create the channel
238 cq_ = grpc_completion_queue_create_for_next(nullptr);
239 cqv_ = std::make_unique<CqVerifier>(cq_);
240 grpc_arg client_args[] = {
241 grpc_channel_arg_integer_create(
242 const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
243 grpc_channel_arg_integer_create(
244 const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE), 0),
245 grpc_channel_arg_integer_create(
246 const_cast<char*>(GRPC_ARG_ENABLE_RETRIES), 0)};
247 grpc_channel_args client_channel_args = {GPR_ARRAY_SIZE(client_args),
248 client_args};
249 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
250 channel_ = grpc_channel_create(JoinHostPort("127.0.0.1", port_).c_str(),
251 creds, &client_channel_args);
252 grpc_channel_credentials_release(creds);
253 // Wait for the channel to connect
254 grpc_connectivity_state state = grpc_channel_check_connectivity_state(
255 channel_, /*try_to_connect=*/true);
256 while (state != GRPC_CHANNEL_READY) {
257 grpc_channel_watch_connectivity_state(
258 channel_, state, grpc_timeout_seconds_to_deadline(1), cq_, Tag(1));
259 cqv_->Expect(Tag(1), true);
260 cqv_->Verify(Duration::Seconds(5));
261 state = grpc_channel_check_connectivity_state(channel_, false);
262 }
263 ExecCtx::Get()->Flush();
264 GPR_ASSERT(
265 connect_notification_.WaitForNotificationWithTimeout(absl::Seconds(1)));
266 }
267
268 ~StreamsNotSeenTest() override {
269 cqv_.reset();
270 grpc_completion_queue_shutdown(cq_);
271 grpc_event ev;
272 do {
273 ev = grpc_completion_queue_next(cq_, grpc_timeout_seconds_to_deadline(1),
274 nullptr);
275 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
276 grpc_completion_queue_destroy(cq_);
277 grpc_channel_destroy(channel_);
278 grpc_endpoint_shutdown(tcp_, GRPC_ERROR_CREATE("Test Shutdown"));
279 ExecCtx::Get()->Flush();
280 GPR_ASSERT(read_end_notification_.WaitForNotificationWithTimeout(
281 absl::Seconds(5)));
282 grpc_endpoint_destroy(tcp_);
283 shutdown_ = true;
284 server_poll_thread_->join();
285 test_tcp_server_destroy(&server_);
286 ExecCtx::Get()->Flush();
287 }
288
289 static void OnConnect(void* arg, grpc_endpoint* tcp,
290 grpc_pollset* /* accepting_pollset */,
291 grpc_tcp_server_acceptor* acceptor) {
292 gpr_free(acceptor);
293 StreamsNotSeenTest* self = static_cast<StreamsNotSeenTest*>(arg);
294 self->tcp_ = tcp;
295 grpc_endpoint_add_to_pollset(tcp, self->server_.pollset[0]);
296 grpc_endpoint_read(tcp, &self->read_buffer_, &self->on_read_done_, false,
297 /*min_progress_size=*/1);
298 std::thread([self]() {
299 ExecCtx exec_ctx;
300 // Send settings frame from server
301 if (self->server_allows_streams_) {
302 constexpr char kHttp2SettingsFrame[] =
303 "\x00\x00\x00\x04\x00\x00\x00\x00\x00";
304 self->Write(absl::string_view(kHttp2SettingsFrame,
305 sizeof(kHttp2SettingsFrame) - 1));
306 } else {
307 // Create a settings frame with a max concurrent stream setting of 0
308 constexpr char kHttp2SettingsFrame[] =
309 "\x00\x00\x06\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00";
310 self->Write(absl::string_view(kHttp2SettingsFrame,
311 sizeof(kHttp2SettingsFrame) - 1));
312 }
313 self->connect_notification_.Notify();
314 }).detach();
315 }
316
317 // This is a blocking call. It waits for the write callback to be invoked
318 // before returning. (In other words, do not call this from a thread that
319 // should not be blocked, for example, a polling thread.)
320 void Write(absl::string_view bytes) {
321 grpc_slice slice =
322 StaticSlice::FromStaticBuffer(bytes.data(), bytes.size()).TakeCSlice();
323 grpc_slice_buffer buffer;
324 grpc_slice_buffer_init(&buffer);
325 grpc_slice_buffer_add(&buffer, slice);
326 WriteBuffer(&buffer);
327 grpc_slice_buffer_destroy(&buffer);
328 }
329
330 void SendPing() {
331 // Send and recv ping ack
332 const char ping_bytes[] =
333 "\x00\x00\x08\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
334 const char ping_ack_bytes[] =
335 "\x00\x00\x08\x06\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
336 Write(absl::string_view(ping_bytes, sizeof(ping_bytes) - 1));
337 WaitForReadBytes(
338 absl::string_view(ping_ack_bytes, sizeof(ping_ack_bytes) - 1));
339 }
340
341 void SendGoaway(uint32_t last_stream_id) {
342 grpc_slice_buffer buffer;
343 grpc_slice_buffer_init(&buffer);
344 grpc_chttp2_goaway_append(last_stream_id, 0, grpc_empty_slice(), &buffer);
345 WriteBuffer(&buffer);
346 grpc_slice_buffer_destroy(&buffer);
347 }
348
349 void WriteBuffer(grpc_slice_buffer* buffer) {
350 Notification on_write_done_notification_;
351 GRPC_CLOSURE_INIT(&on_write_done_, OnWriteDone,
352 &on_write_done_notification_, nullptr);
353 grpc_endpoint_write(tcp_, buffer, &on_write_done_, nullptr,
354 /*max_frame_size=*/INT_MAX);
355 ExecCtx::Get()->Flush();
356 GPR_ASSERT(on_write_done_notification_.WaitForNotificationWithTimeout(
357 absl::Seconds(5)));
358 }
359
360 static void OnWriteDone(void* arg, grpc_error_handle error) {
361 GPR_ASSERT(error.ok());
362 Notification* on_write_done_notification_ = static_cast<Notification*>(arg);
363 on_write_done_notification_->Notify();
364 }
365
366 static void OnReadDone(void* arg, grpc_error_handle error) {
367 StreamsNotSeenTest* self = static_cast<StreamsNotSeenTest*>(arg);
368 if (error.ok()) {
369 {
370 MutexLock lock(&self->mu_);
371 for (size_t i = 0; i < self->read_buffer_.count; ++i) {
372 absl::StrAppend(&self->read_bytes_,
373 StringViewFromSlice(self->read_buffer_.slices[i]));
374 }
375 self->read_cv_.SignalAll();
376 }
377 grpc_slice_buffer_reset_and_unref(&self->read_buffer_);
378 grpc_endpoint_read(self->tcp_, &self->read_buffer_, &self->on_read_done_,
379 false, /*min_progress_size=*/1);
380 } else {
381 grpc_slice_buffer_destroy(&self->read_buffer_);
382 self->read_end_notification_.Notify();
383 }
384 }
385
386 // Waits for \a bytes to show up in read_bytes_
387 void WaitForReadBytes(absl::string_view bytes) {
388 std::atomic<bool> done{false};
389 std::thread cq_driver([&]() {
390 while (!done) {
391 grpc_event ev = grpc_completion_queue_next(
392 cq_, grpc_timeout_milliseconds_to_deadline(10), nullptr);
393 GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
394 }
395 });
396 {
397 MutexLock lock(&mu_);
398 while (!absl::StrContains(read_bytes_, bytes)) {
399 read_cv_.WaitWithTimeout(&mu_, absl::Seconds(5));
400 }
401 }
402 done = true;
403 cq_driver.join();
404 }
405
406 // Flag to check whether the server's MAX_CONCURRENT_STREAM setting is
407 // non-zero or not.
408 bool server_allows_streams_;
409 int port_;
410 test_tcp_server server_;
411 std::unique_ptr<std::thread> server_poll_thread_;
412 grpc_endpoint* tcp_ = nullptr;
413 Notification connect_notification_;
414 grpc_slice_buffer read_buffer_;
415 grpc_closure on_write_done_;
416 grpc_closure on_read_done_;
417 Notification read_end_notification_;
418 std::string read_bytes_ ABSL_GUARDED_BY(mu_);
419 grpc_channel* channel_ = nullptr;
420 grpc_completion_queue* cq_ = nullptr;
421 std::unique_ptr<CqVerifier> cqv_;
422 Mutex mu_;
423 CondVar read_cv_;
424 std::atomic<bool> shutdown_{false};
425 };
426
427 // Client's HTTP2 transport starts a new stream, sends the request on the wire,
428 // but receives a GOAWAY with a stream ID of 0, meaning that the request was
429 // unseen by the server.The test verifies that the HTTP2 transport adds
430 // GrpcNetworkStreamState with a value of kNotSeenByServer to the trailing
431 // metadata.
432 TEST_F(StreamsNotSeenTest, StartStreamBeforeGoaway) {
433 grpc_call* c =
434 grpc_channel_create_call(channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, cq_,
435 grpc_slice_from_static_string("/foo"), nullptr,
436 grpc_timeout_seconds_to_deadline(1), nullptr);
437 GPR_ASSERT(c);
438 grpc_metadata_array initial_metadata_recv;
439 grpc_metadata_array trailing_metadata_recv;
440 grpc_metadata_array_init(&initial_metadata_recv);
441 grpc_metadata_array_init(&trailing_metadata_recv);
442 grpc_op* op;
443 grpc_op ops[6];
444 grpc_status_code status;
445 const char* error_string;
446 grpc_call_error error;
447 grpc_slice details;
448 // Send the request
449 memset(ops, 0, sizeof(ops));
450 op = ops;
451 op->op = GRPC_OP_SEND_INITIAL_METADATA;
452 op->data.send_initial_metadata.count = 0;
453 op->flags = 0;
454 op->reserved = nullptr;
455 op++;
456 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
457 op->flags = 0;
458 op->reserved = nullptr;
459 op++;
460 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(101),
461 nullptr);
462 cqv_->Expect(Tag(101), true);
463 cqv_->Verify();
464 // Send a goaway from server signalling that the request was unseen by the
465 // server.
466 SendGoaway(0);
467 memset(ops, 0, sizeof(ops));
468 op = ops;
469 op->op = GRPC_OP_RECV_INITIAL_METADATA;
470 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
471 op->flags = 0;
472 op->reserved = nullptr;
473 op++;
474 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
475 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
476 op->data.recv_status_on_client.status = &status;
477 op->data.recv_status_on_client.status_details = &details;
478 op->data.recv_status_on_client.error_string = &error_string;
479 op->flags = 0;
480 op->reserved = nullptr;
481 op++;
482 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(102),
483 nullptr);
484 GPR_ASSERT(GRPC_CALL_OK == error);
485 cqv_->Expect(Tag(102), true);
486 cqv_->Verify();
487 // Verify status and metadata
488 EXPECT_EQ(status, GRPC_STATUS_UNAVAILABLE);
489 ASSERT_TRUE(TrailingMetadataRecordingFilter::trailing_metadata_available());
490 ASSERT_TRUE(
491 TrailingMetadataRecordingFilter::stream_network_state().has_value());
492 EXPECT_EQ(TrailingMetadataRecordingFilter::stream_network_state().value(),
493 GrpcStreamNetworkState::kNotSeenByServer);
494 grpc_slice_unref(details);
495 gpr_free(const_cast<char*>(error_string));
496 grpc_metadata_array_destroy(&initial_metadata_recv);
497 grpc_metadata_array_destroy(&trailing_metadata_recv);
498 grpc_call_unref(c);
499 ExecCtx::Get()->Flush();
500 }
501
502 // Client's HTTP2 transport starts a new stream, sends the request on the wire,
503 // notices that the transport is destroyed. The test verifies that the HTTP2
504 // transport does not add GrpcNetworkStreamState metadata since we don't know
505 // whether the server saw the request or not.
506 TEST_F(StreamsNotSeenTest, TransportDestroyed) {
507 grpc_call* c =
508 grpc_channel_create_call(channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, cq_,
509 grpc_slice_from_static_string("/foo"), nullptr,
510 grpc_timeout_seconds_to_deadline(1), nullptr);
511 GPR_ASSERT(c);
512 grpc_metadata_array initial_metadata_recv;
513 grpc_metadata_array trailing_metadata_recv;
514 grpc_metadata_array_init(&initial_metadata_recv);
515 grpc_metadata_array_init(&trailing_metadata_recv);
516 grpc_op* op;
517 grpc_op ops[6];
518 grpc_status_code status;
519 const char* error_string;
520 grpc_call_error error;
521 grpc_slice details;
522 // Send the request
523 memset(ops, 0, sizeof(ops));
524 op = ops;
525 op->op = GRPC_OP_SEND_INITIAL_METADATA;
526 op->data.send_initial_metadata.count = 0;
527 op->flags = 0;
528 op->reserved = nullptr;
529 op++;
530 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
531 op->flags = 0;
532 op->reserved = nullptr;
533 op++;
534 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(101),
535 nullptr);
536 cqv_->Expect(Tag(101), true);
537 cqv_->Verify();
538 // Shutdown the server endpoint
539 grpc_endpoint_shutdown(tcp_, GRPC_ERROR_CREATE("Server shutdown"));
540 memset(ops, 0, sizeof(ops));
541 op = ops;
542 op->op = GRPC_OP_RECV_INITIAL_METADATA;
543 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
544 op->flags = 0;
545 op->reserved = nullptr;
546 op++;
547 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
548 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
549 op->data.recv_status_on_client.status = &status;
550 op->data.recv_status_on_client.status_details = &details;
551 op->data.recv_status_on_client.error_string = &error_string;
552 op->flags = 0;
553 op->reserved = nullptr;
554 op++;
555 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(102),
556 nullptr);
557 GPR_ASSERT(GRPC_CALL_OK == error);
558 cqv_->Expect(Tag(102), true);
559 cqv_->Verify();
560 // Verify status and metadata
561 EXPECT_EQ(status, GRPC_STATUS_UNAVAILABLE);
562 EXPECT_FALSE(
563 TrailingMetadataRecordingFilter::stream_network_state().has_value());
564 grpc_slice_unref(details);
565 gpr_free(const_cast<char*>(error_string));
566 grpc_metadata_array_destroy(&initial_metadata_recv);
567 grpc_metadata_array_destroy(&trailing_metadata_recv);
568 grpc_call_unref(c);
569 ExecCtx::Get()->Flush();
570 }
571
572 // Client's HTTP2 transport tries to send an RPC after having received a GOAWAY
573 // frame. The test verifies that the HTTP2 transport adds GrpcNetworkStreamState
574 // with a value of kNotSentOnWire to the trailing metadata.
575 TEST_F(StreamsNotSeenTest, StartStreamAfterGoaway) {
576 // Send Goaway from the server
577 SendGoaway(0);
578 // Send a ping to make sure that the goaway was received.
579 SendPing();
580 // Try sending an RPC
581 grpc_call* c =
582 grpc_channel_create_call(channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, cq_,
583 grpc_slice_from_static_string("/foo"), nullptr,
584 grpc_timeout_seconds_to_deadline(1), nullptr);
585 GPR_ASSERT(c);
586 grpc_metadata_array initial_metadata_recv;
587 grpc_metadata_array trailing_metadata_recv;
588 grpc_metadata_array_init(&initial_metadata_recv);
589 grpc_metadata_array_init(&trailing_metadata_recv);
590 grpc_op* op;
591 grpc_op ops[6];
592 grpc_status_code status;
593 const char* error_string;
594 grpc_call_error error;
595 grpc_slice details;
596 memset(ops, 0, sizeof(ops));
597 op = ops;
598 op->op = GRPC_OP_SEND_INITIAL_METADATA;
599 op->data.send_initial_metadata.count = 0;
600 op->flags = 0;
601 op->reserved = nullptr;
602 op++;
603 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
604 op->flags = 0;
605 op->reserved = nullptr;
606 op++;
607 op->op = GRPC_OP_RECV_INITIAL_METADATA;
608 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
609 op->flags = 0;
610 op->reserved = nullptr;
611 op++;
612 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
613 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
614 op->data.recv_status_on_client.status = &status;
615 op->data.recv_status_on_client.status_details = &details;
616 op->data.recv_status_on_client.error_string = &error_string;
617 op->flags = 0;
618 op->reserved = nullptr;
619 op++;
620 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(101),
621 nullptr);
622 GPR_ASSERT(GRPC_CALL_OK == error);
623 cqv_->Expect(Tag(101), true);
624 cqv_->Verify();
625 // Verify status and metadata
626 EXPECT_EQ(status, GRPC_STATUS_UNAVAILABLE);
627 ASSERT_TRUE(TrailingMetadataRecordingFilter::trailing_metadata_available());
628 ASSERT_TRUE(
629 TrailingMetadataRecordingFilter::stream_network_state().has_value());
630 EXPECT_EQ(TrailingMetadataRecordingFilter::stream_network_state().value(),
631 GrpcStreamNetworkState::kNotSentOnWire);
632 grpc_slice_unref(details);
633 gpr_free(const_cast<char*>(error_string));
634 grpc_metadata_array_destroy(&initial_metadata_recv);
635 grpc_metadata_array_destroy(&trailing_metadata_recv);
636 grpc_call_unref(c);
637 ExecCtx::Get()->Flush();
638 }
639
640 // These tests have the server sending a SETTINGS_FRAME with a max concurrent
641 // streams settings of 0 which denies the client the chance to start a stream.
642 // Note that in the future, these tests might become outdated if the
643 // client_channel learns about the max concurrent streams setting.
644 class ZeroConcurrencyTest : public StreamsNotSeenTest {
645 protected:
646 ZeroConcurrencyTest() : StreamsNotSeenTest(/*server_allows_streams=*/false) {}
647 };
648
649 // Client's HTTP2 transport receives a RPC request, but it cannot start the RPC
650 // because of the max concurrent streams setting. A goaway frame is then
651 // received which should result in the RPC getting cancelled with
652 // kNotSentOnWire.
653 TEST_F(ZeroConcurrencyTest, StartStreamBeforeGoaway) {
654 grpc_call* c =
655 grpc_channel_create_call(channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, cq_,
656 grpc_slice_from_static_string("/foo"), nullptr,
657 grpc_timeout_seconds_to_deadline(5), nullptr);
658 GPR_ASSERT(c);
659 grpc_metadata_array initial_metadata_recv;
660 grpc_metadata_array trailing_metadata_recv;
661 grpc_metadata_array_init(&initial_metadata_recv);
662 grpc_metadata_array_init(&trailing_metadata_recv);
663 grpc_op* op;
664 grpc_op ops[6];
665 grpc_status_code status;
666 const char* error_string;
667 grpc_call_error error;
668 grpc_slice details;
669 // Send the request
670 memset(ops, 0, sizeof(ops));
671 op = ops;
672 op->op = GRPC_OP_SEND_INITIAL_METADATA;
673 op->data.send_initial_metadata.count = 0;
674 op->flags = 0;
675 op->reserved = nullptr;
676 op++;
677 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
678 op->flags = 0;
679 op->reserved = nullptr;
680 op++;
681 op->op = GRPC_OP_RECV_INITIAL_METADATA;
682 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
683 op->flags = 0;
684 op->reserved = nullptr;
685 op++;
686 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
687 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
688 op->data.recv_status_on_client.status = &status;
689 op->data.recv_status_on_client.status_details = &details;
690 op->data.recv_status_on_client.error_string = &error_string;
691 op->flags = 0;
692 op->reserved = nullptr;
693 op++;
694 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(101),
695 nullptr);
696 // This test assumes that nothing would pause the RPC before its received by
697 // the transport. If that no longer holds true, we might need to drive the cq
698 // for some time to make sure that the RPC reaches the HTTP2 layer.
699 SendGoaway(0);
700 GPR_ASSERT(GRPC_CALL_OK == error);
701 cqv_->Expect(Tag(101), true);
702 cqv_->Verify();
703 // Verify status and metadata
704 EXPECT_EQ(status, GRPC_STATUS_UNAVAILABLE);
705 ASSERT_TRUE(TrailingMetadataRecordingFilter::trailing_metadata_available());
706 ASSERT_TRUE(
707 TrailingMetadataRecordingFilter::stream_network_state().has_value());
708 EXPECT_EQ(TrailingMetadataRecordingFilter::stream_network_state().value(),
709 GrpcStreamNetworkState::kNotSentOnWire);
710 grpc_slice_unref(details);
711 gpr_free(const_cast<char*>(error_string));
712 grpc_metadata_array_destroy(&initial_metadata_recv);
713 grpc_metadata_array_destroy(&trailing_metadata_recv);
714 grpc_call_unref(c);
715 ExecCtx::Get()->Flush();
716 }
717
718 // Client's HTTP2 transport receives a RPC request, but it cannot start the RPC
719 // because of the max concurrent streams setting. Server then shuts its endpoint
720 // which should result in the RPC getting cancelled with kNotSentOnWire.
721 TEST_F(ZeroConcurrencyTest, TransportDestroyed) {
722 grpc_call* c =
723 grpc_channel_create_call(channel_, nullptr, GRPC_PROPAGATE_DEFAULTS, cq_,
724 grpc_slice_from_static_string("/foo"), nullptr,
725 grpc_timeout_seconds_to_deadline(5), nullptr);
726 GPR_ASSERT(c);
727 grpc_metadata_array initial_metadata_recv;
728 grpc_metadata_array trailing_metadata_recv;
729 grpc_metadata_array_init(&initial_metadata_recv);
730 grpc_metadata_array_init(&trailing_metadata_recv);
731 grpc_op* op;
732 grpc_op ops[6];
733 grpc_status_code status;
734 const char* error_string;
735 grpc_call_error error;
736 grpc_slice details;
737 // Send the request
738 memset(ops, 0, sizeof(ops));
739 op = ops;
740 op->op = GRPC_OP_SEND_INITIAL_METADATA;
741 op->data.send_initial_metadata.count = 0;
742 op->flags = 0;
743 op->reserved = nullptr;
744 op++;
745 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
746 op->flags = 0;
747 op->reserved = nullptr;
748 op++;
749 op->op = GRPC_OP_RECV_INITIAL_METADATA;
750 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
751 op->flags = 0;
752 op->reserved = nullptr;
753 op++;
754 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
755 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
756 op->data.recv_status_on_client.status = &status;
757 op->data.recv_status_on_client.status_details = &details;
758 op->data.recv_status_on_client.error_string = &error_string;
759 op->flags = 0;
760 op->reserved = nullptr;
761 op++;
762 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), Tag(101),
763 nullptr);
764 grpc_endpoint_shutdown(tcp_, GRPC_ERROR_CREATE("Server shutdown"));
765 GPR_ASSERT(GRPC_CALL_OK == error);
766 cqv_->Expect(Tag(101), true);
767 cqv_->Verify();
768 // Verify status and metadata
769 EXPECT_EQ(status, GRPC_STATUS_UNAVAILABLE);
770 ASSERT_TRUE(TrailingMetadataRecordingFilter::trailing_metadata_available());
771 ASSERT_TRUE(
772 TrailingMetadataRecordingFilter::stream_network_state().has_value());
773 EXPECT_EQ(TrailingMetadataRecordingFilter::stream_network_state().value(),
774 GrpcStreamNetworkState::kNotSentOnWire);
775 grpc_slice_unref(details);
776 gpr_free(const_cast<char*>(error_string));
777 grpc_metadata_array_destroy(&initial_metadata_recv);
778 grpc_metadata_array_destroy(&trailing_metadata_recv);
779 grpc_call_unref(c);
780 ExecCtx::Get()->Flush();
781 }
782
783 } // namespace
784 } // namespace grpc_core
785
786 int main(int argc, char** argv) {
787 ::testing::InitGoogleTest(&argc, argv);
788 grpc::testing::TestEnvironment env(&argc, argv);
789 int result;
790 grpc_core::CoreConfiguration::RunWithSpecialConfiguration(
791 [](grpc_core::CoreConfiguration::Builder* builder) {
792 grpc_core::BuildCoreConfiguration(builder);
793 auto register_stage = [builder](grpc_channel_stack_type type,
794 const grpc_channel_filter* filter) {
795 builder->channel_init()->RegisterStage(
796 type, INT_MAX, [filter](grpc_core::ChannelStackBuilder* builder) {
797 // Want to add the filter as close to the end as possible, to
798 // make sure that all of the filters work well together.
799 // However, we can't add it at the very end, because the
800 // connected channel filter must be the last one. So we add it
801 // right before the last one.
802 auto it = builder->mutable_stack()->end();
803 --it;
804 builder->mutable_stack()->insert(it, filter);
805 return true;
806 });
807 };
808 register_stage(
809 GRPC_CLIENT_SUBCHANNEL,
810 &grpc_core::TrailingMetadataRecordingFilter::kFilterVtable);
811 },
812 [&] {
813 grpc_core::
814 TestOnlyGlobalHttp2TransportDisableTransientFailureStateNotification(
815 true);
816 grpc_init();
817 {
818 grpc_core::ExecCtx exec_ctx;
819 result = RUN_ALL_TESTS();
820 }
821 grpc_shutdown();
822 });
823 return result;
824 }