xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/max_connection_age.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 <limits.h>
20 
21 #include <memory>
22 
23 #include "gtest/gtest.h"
24 
25 #include <grpc/impl/channel_arg_names.h>
26 #include <grpc/status.h>
27 
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/gprpp/time.h"
30 #include "test/core/end2end/end2end_tests.h"
31 #include "test/core/util/test_config.h"
32 
33 #define MAX_CONNECTION_AGE_MS 500
34 #define MAX_CONNECTION_AGE_GRACE_MS 2000
35 #define MAX_CONNECTION_IDLE_MS 9999
36 
37 #define MAX_CONNECTION_AGE_JITTER_MULTIPLIER 1.1
38 #define CALL_DEADLINE_S 30
39 // The amount of time we wait for the connection to time out, but after it the
40 // connection should not use up its grace period. It should be a number between
41 // MAX_CONNECTION_AGE_MS and MAX_CONNECTION_AGE_MS +
42 // MAX_CONNECTION_AGE_GRACE_MS
43 #define CQ_MAX_CONNECTION_AGE_WAIT_TIME_S 1
44 // The amount of time we wait after the connection reaches its max age, it
45 // should be shorter than CALL_DEADLINE_S - CQ_MAX_CONNECTION_AGE_WAIT_TIME_S
46 #define CQ_MAX_CONNECTION_AGE_GRACE_WAIT_TIME_S 2
47 // The grace period for the test to observe the channel shutdown process
48 #define IMMEDIATE_SHUTDOWN_GRACE_TIME_MS 3000
49 
50 namespace grpc_core {
51 namespace {
52 
CORE_END2END_TEST(Http2Test,MaxAgeForciblyClose)53 CORE_END2END_TEST(Http2Test, MaxAgeForciblyClose) {
54   SKIP_IF_MINSTACK();
55   InitClient(ChannelArgs());
56   InitServer(ChannelArgs()
57                  .Set(GRPC_ARG_MAX_CONNECTION_AGE_MS, MAX_CONNECTION_AGE_MS)
58                  .Set(GRPC_ARG_MAX_CONNECTION_AGE_GRACE_MS,
59                       MAX_CONNECTION_AGE_GRACE_MS)
60                  .Set(GRPC_ARG_MAX_CONNECTION_IDLE_MS, MAX_CONNECTION_IDLE_MS));
61   auto c = NewClientCall("/foo")
62                .Timeout(Duration::Seconds(CALL_DEADLINE_S))
63                .Create();
64   const auto expect_shutdown_time =
65       Timestamp::FromTimespecRoundUp(grpc_timeout_milliseconds_to_deadline(
66           static_cast<int>(MAX_CONNECTION_AGE_MS *
67                            MAX_CONNECTION_AGE_JITTER_MULTIPLIER) +
68           MAX_CONNECTION_AGE_GRACE_MS + IMMEDIATE_SHUTDOWN_GRACE_TIME_MS));
69   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
70   CoreEnd2endTest::IncomingStatusOnClient server_status;
71   c.NewBatch(1)
72       .SendInitialMetadata({})
73       .SendCloseFromClient()
74       .RecvInitialMetadata(server_initial_metadata)
75       .RecvStatusOnClient(server_status);
76   auto s = RequestCall(101);
77   bool got_client = false;
78   bool got_server = false;
79   Expect(1, Maybe{&got_client});
80   Expect(101, Maybe{&got_server});
81   Step();
82   if (got_server) {
83     // Request got through to the server before connection timeout
84     // Wait for the channel to reach its max age
85     Step(Duration::Seconds(CQ_MAX_CONNECTION_AGE_WAIT_TIME_S));
86     // After the channel reaches its max age, we still do nothing here. And wait
87     // for it to use up its max age grace period.
88     Expect(1, true);
89     Step();
90     EXPECT_LT(Timestamp::Now(), expect_shutdown_time);
91     CoreEnd2endTest::IncomingCloseOnServer client_close;
92     s.NewBatch(102)
93         .SendInitialMetadata({})
94         .SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {})
95         .RecvCloseOnServer(client_close);
96     Expect(102, true);
97     Step();
98     EXPECT_EQ(s.method(), "/foo");
99     EXPECT_TRUE(client_close.was_cancelled());
100   } else {
101     // Request failed before getting to the server
102   }
103   ShutdownServerAndNotify(1000);
104   Expect(1000, true);
105   if (got_client) {
106     Expect(101, false);
107   }
108   Step();
109   // The connection should be closed immediately after the max age grace period,
110   // the in-progress RPC should fail.
111   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNAVAILABLE);
112 }
113 
CORE_END2END_TEST(Http2Test,MaxAgeGracefullyClose)114 CORE_END2END_TEST(Http2Test, MaxAgeGracefullyClose) {
115   SKIP_IF_MINSTACK();
116   SKIP_IF_FUZZING();
117 
118   InitClient(ChannelArgs());
119   InitServer(ChannelArgs()
120                  .Set(GRPC_ARG_MAX_CONNECTION_AGE_MS, MAX_CONNECTION_AGE_MS)
121                  .Set(GRPC_ARG_MAX_CONNECTION_AGE_GRACE_MS, INT_MAX)
122                  .Set(GRPC_ARG_MAX_CONNECTION_IDLE_MS, MAX_CONNECTION_IDLE_MS));
123   auto c = NewClientCall("/foo")
124                .Timeout(Duration::Seconds(CALL_DEADLINE_S))
125                .Create();
126   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
127   CoreEnd2endTest::IncomingStatusOnClient server_status;
128   c.NewBatch(1)
129       .SendInitialMetadata({})
130       .SendCloseFromClient()
131       .RecvInitialMetadata(server_initial_metadata)
132       .RecvStatusOnClient(server_status);
133   auto s = RequestCall(101);
134   bool got_client = false;
135   bool got_server = false;
136   Expect(1, Maybe{&got_client});
137   Expect(101, Maybe{&got_server});
138   Step();
139   if (got_server) {
140     // Request got through to the server before connection timeout
141     // Wait for the channel to reach its max age
142     Step(Duration::Seconds(CQ_MAX_CONNECTION_AGE_WAIT_TIME_S));
143     // The connection is shutting down gracefully. In-progress rpc should not be
144     // closed, hence the completion queue should see nothing here.
145     Step(Duration::Seconds(CQ_MAX_CONNECTION_AGE_GRACE_WAIT_TIME_S));
146     CoreEnd2endTest::IncomingCloseOnServer client_close;
147     s.NewBatch(102)
148         .SendInitialMetadata({})
149         .SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {})
150         .RecvCloseOnServer(client_close);
151     Expect(102, true);
152     Expect(1, true);
153     Step();
154     EXPECT_EQ(s.method(), "/foo");
155     EXPECT_FALSE(client_close.was_cancelled());
156   } else {
157     // Request failed before getting to the server
158   }
159   ShutdownServerAndNotify(1000);
160   Expect(1000, true);
161   if (got_client) {
162     Expect(101, false);
163   }
164   Step();
165   // The connection is closed gracefully with goaway, the rpc should still be
166   // completed.
167   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
168   EXPECT_EQ(server_status.message(), "xyz");
169 }
170 
171 }  // namespace
172 }  // namespace grpc_core
173