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