xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/keepalive_timeout.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #include <memory>
20 
21 #include "absl/strings/string_view.h"
22 #include "gtest/gtest.h"
23 
24 #include <grpc/impl/channel_arg_names.h>
25 #include <grpc/status.h>
26 
27 #include "src/core/ext/transport/chttp2/transport/internal.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/config/config_vars.h"
30 #include "src/core/lib/gprpp/time.h"
31 #include "src/core/lib/iomgr/port.h"
32 #include "test/core/end2end/end2end_tests.h"
33 
34 namespace grpc_core {
35 namespace {
36 
37 // Client sends a request, then waits for the keepalive watchdog timeouts before
38 // returning status.
CORE_END2END_TEST(Http2SingleHopTest,KeepaliveTimeout)39 CORE_END2END_TEST(Http2SingleHopTest, KeepaliveTimeout) {
40   // Disable ping ack to trigger the keepalive timeout
41   InitServer(ChannelArgs().Set("grpc.http2.ack_pings", false));
42   InitClient(ChannelArgs()
43                  .Set(GRPC_ARG_KEEPALIVE_TIME_MS, 10)
44                  .Set(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 0)
45                  .Set(GRPC_ARG_PING_TIMEOUT_MS, 0)
46                  .Set(GRPC_ARG_HTTP2_BDP_PROBE, false));
47   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
48   IncomingMetadata server_initial_metadata;
49   IncomingStatusOnClient server_status;
50   c.NewBatch(1)
51       .SendInitialMetadata({})
52       .SendCloseFromClient()
53       .RecvInitialMetadata(server_initial_metadata)
54       .RecvStatusOnClient(server_status);
55   Expect(1, true);
56   Step();
57   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNAVAILABLE);
58   EXPECT_EQ(server_status.message(), "ping timeout");
59 }
60 
61 // Verify that reads reset the keepalive ping timer. The client sends 30 pings
62 // with a sleep of 10ms in between. It has a configured keepalive timer of
63 // 200ms. In the success case, each ping ack should reset the keepalive timer so
64 // that the keepalive ping is never sent.
CORE_END2END_TEST(Http2SingleHopTest,ReadDelaysKeepalive)65 CORE_END2END_TEST(Http2SingleHopTest, ReadDelaysKeepalive) {
66 #ifdef GRPC_POSIX_SOCKET
67   // It is hard to get the timing right for the polling engine poll.
68   if (ConfigVars::Get().PollStrategy() == "poll") {
69     GTEST_SKIP() << "Skipping test under poll poller";
70   }
71 #endif  // GRPC_POSIX_SOCKET
72   const auto kPingInterval = Duration::Milliseconds(100);
73   // Disable ping ack to trigger the keepalive timeout
74   InitServer(ChannelArgs().Set("grpc.http2.ack_pings", false));
75   InitClient(ChannelArgs()
76                  .Set(GRPC_ARG_KEEPALIVE_TIME_MS, (20 * kPingInterval).millis())
77                  .Set(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 0)
78                  .Set(GRPC_ARG_HTTP2_BDP_PROBE, false));
79   auto c = NewClientCall("/foo").Timeout(Duration::Seconds(60)).Create();
80   IncomingMetadata server_initial_metadata;
81   IncomingStatusOnClient server_status;
82   c.NewBatch(1)
83       .SendInitialMetadata({})
84       .RecvInitialMetadata(server_initial_metadata)
85       .RecvStatusOnClient(server_status);
86   auto s = RequestCall(100);
87   Expect(100, true);
88   Step();
89   IncomingCloseOnServer client_close;
90   s.NewBatch(101).SendInitialMetadata({}).RecvCloseOnServer(client_close);
91   for (int i = 0; i < 30; i++) {
92     IncomingMessage server_message;
93     IncomingMessage client_message;
94     c.NewBatch(2).SendMessage("hello world").RecvMessage(server_message);
95     s.NewBatch(102).RecvMessage(client_message);
96     Expect(102, true);
97     Step();
98     s.NewBatch(103).SendMessage("hello you");
99     Expect(103, true);
100     Expect(2, true);
101     Step();
102     // Sleep for a short interval to check if the client sends any pings
103     Step(kPingInterval);
104   }
105   c.NewBatch(3).SendCloseFromClient();
106   s.NewBatch(104).SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {});
107   Expect(1, true);
108   Expect(3, true);
109   Expect(101, true);
110   Expect(104, true);
111   Step();
112 }
113 
114 }  // namespace
115 }  // namespace grpc_core
116