xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/retry.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/types/optional.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/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/time.h"
29 #include "test/core/end2end/end2end_tests.h"
30 
31 namespace grpc_core {
32 
33 // Tests a basic retry scenario:
34 // - 2 retries allowed for ABORTED status
35 // - first attempt returns ABORTED
36 // - second attempt returns OK
CORE_END2END_TEST(RetryTest,Retry)37 CORE_END2END_TEST(RetryTest, Retry) {
38   InitServer(ChannelArgs());
39   InitClient(ChannelArgs().Set(
40       GRPC_ARG_SERVICE_CONFIG,
41       "{\n"
42       "  \"methodConfig\": [ {\n"
43       "    \"name\": [\n"
44       "      { \"service\": \"service\", \"method\": \"method\" }\n"
45       "    ],\n"
46       "    \"retryPolicy\": {\n"
47       "      \"maxAttempts\": 3,\n"
48       "      \"initialBackoff\": \"1s\",\n"
49       "      \"maxBackoff\": \"120s\",\n"
50       "      \"backoffMultiplier\": 1.6,\n"
51       "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
52       "    }\n"
53       "  } ]\n"
54       "}"));
55   auto c =
56       NewClientCall("/service/method").Timeout(Duration::Seconds(5)).Create();
57   EXPECT_NE(c.GetPeer(), absl::nullopt);
58   IncomingStatusOnClient server_status;
59   IncomingMetadata server_initial_metadata;
60   IncomingMessage server_message;
61   c.NewBatch(1)
62       .SendInitialMetadata({})
63       .SendMessage("foo")
64       .RecvMessage(server_message)
65       .SendCloseFromClient()
66       .RecvInitialMetadata(server_initial_metadata)
67       .RecvStatusOnClient(server_status);
68   auto s = RequestCall(101);
69   Expect(101, true);
70   Step();
71 
72   // Make sure the "grpc-previous-rpc-attempts" header was not sent in the
73   // initial attempt.
74   EXPECT_EQ(s.GetInitialMetadata("grpc-previous-rpc-attempts"), absl::nullopt);
75 
76   EXPECT_NE(s.GetPeer(), absl::nullopt);
77   EXPECT_NE(c.GetPeer(), absl::nullopt);
78 
79   IncomingCloseOnServer client_close;
80   s.NewBatch(102)
81       .SendInitialMetadata({})
82       .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {})
83       .RecvCloseOnServer(client_close);
84   Expect(102, true);
85   Step();
86 
87   auto s2 = RequestCall(201);
88   Expect(201, true);
89   Step();
90 
91   // Make sure the "grpc-previous-rpc-attempts" header was sent in the retry.
92   EXPECT_EQ(s2.GetInitialMetadata("grpc-previous-rpc-attempts"), "1");
93 
94   EXPECT_NE(s2.GetPeer(), absl::nullopt);
95   EXPECT_NE(c.GetPeer(), absl::nullopt);
96 
97   IncomingMessage server_message2;
98   s2.NewBatch(202)
99       .SendInitialMetadata({})
100       .RecvMessage(server_message2)
101       .SendMessage("bar");
102   IncomingCloseOnServer client_close2;
103   s2.NewBatch(203)
104       .SendStatusFromServer(GRPC_STATUS_OK, "xyz", {})
105       .RecvCloseOnServer(client_close2);
106   Expect(202, true);
107   Expect(203, true);
108   Expect(1, true);
109   Step();
110 
111   EXPECT_EQ(server_status.status(), GRPC_STATUS_OK);
112   EXPECT_EQ(server_status.message(), "xyz");
113   EXPECT_EQ(s.method(), "/service/method");
114   EXPECT_FALSE(client_close2.was_cancelled());
115 }
116 
117 }  // namespace grpc_core
118