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 when throttled.
35 // - 1 retry allowed for ABORTED status
36 // - first attempt gets ABORTED but is over limit, so no retry is done
CORE_END2END_TEST(RetryTest,RetryThrottled)37 CORE_END2END_TEST(RetryTest, RetryThrottled) {
38 InitServer(ChannelArgs().Set(
39 GRPC_ARG_SERVICE_CONFIG,
40 "{\n"
41 " \"methodConfig\": [ {\n"
42 " \"name\": [\n"
43 " { \"service\": \"service\", \"method\": \"method\" }\n"
44 " ],\n"
45 " \"retryPolicy\": {\n"
46 " \"maxAttempts\": 2,\n"
47 " \"initialBackoff\": \"1s\",\n"
48 " \"maxBackoff\": \"120s\",\n"
49 " \"backoffMultiplier\": 1.6,\n"
50 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
51 " }\n"
52 " } ],\n"
53 // A single failure will cause us to be throttled.
54 // (This is not a very realistic config, but it works for the
55 // purposes of this test.)
56 " \"retryThrottling\": {\n"
57 " \"maxTokens\": 2,\n"
58 " \"tokenRatio\": 1.0\n"
59 " }\n"
60 "}"));
61 InitClient(ChannelArgs());
62 auto c =
63 NewClientCall("/service/method").Timeout(Duration::Seconds(30)).Create();
64 EXPECT_NE(c.GetPeer(), absl::nullopt);
65 IncomingStatusOnClient server_status;
66 IncomingMetadata server_initial_metadata;
67 IncomingMessage server_message;
68 c.NewBatch(1)
69 .SendInitialMetadata({})
70 .SendMessage("foo")
71 .RecvMessage(server_message)
72 .SendCloseFromClient()
73 .RecvInitialMetadata(server_initial_metadata)
74 .RecvStatusOnClient(server_status);
75 auto s = RequestCall(101);
76 Expect(101, true);
77 Step();
78 EXPECT_NE(s.GetPeer(), absl::nullopt);
79 EXPECT_NE(c.GetPeer(), absl::nullopt);
80 IncomingCloseOnServer client_close;
81 s.NewBatch(102)
82 .SendInitialMetadata({})
83 .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {})
84 .RecvCloseOnServer(client_close);
85 Expect(102, true);
86 Expect(1, true);
87 Step();
88 EXPECT_EQ(server_status.status(), GRPC_STATUS_ABORTED);
89 EXPECT_EQ(server_status.message(), "xyz");
90 EXPECT_EQ(s.method(), "/service/method");
91 EXPECT_FALSE(client_close.was_cancelled());
92 }
93
94 } // namespace
95 } // namespace grpc_core
96