xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/retry_non_retriable_status.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 namespace {
33 
34 // Tests that we don't retry for non-retryable status codes.
35 // - 1 retry allowed for ABORTED status
36 // - first attempt gets INVALID_ARGUMENT, so no retry is done
CORE_END2END_TEST(RetryTest,RetryNonRetriableStatus)37 CORE_END2END_TEST(RetryTest, RetryNonRetriableStatus) {
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\": 2,\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(30)).Create();
57   EXPECT_NE(c.GetPeer(), absl::nullopt);
58   IncomingMessage server_message;
59   IncomingMetadata server_initial_metadata;
60   IncomingStatusOnClient server_status;
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   EXPECT_NE(s.GetPeer(), absl::nullopt);
72   EXPECT_NE(c.GetPeer(), absl::nullopt);
73   IncomingCloseOnServer client_close;
74   s.NewBatch(102)
75       .SendInitialMetadata({})
76       .SendStatusFromServer(GRPC_STATUS_INVALID_ARGUMENT, "xyz", {})
77       .RecvCloseOnServer(client_close);
78   Expect(102, true);
79   Expect(1, true);
80   Step();
81   EXPECT_EQ(server_status.status(), GRPC_STATUS_INVALID_ARGUMENT);
82   EXPECT_EQ(server_status.message(), "xyz");
83   EXPECT_EQ(s.method(), "/service/method");
84   EXPECT_FALSE(client_close.was_cancelled());
85 }
86 }  // namespace
87 }  // namespace grpc_core
88