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 honor server push-back delay.
35 // - 2 retries allowed for ABORTED status
36 // - first attempt gets ABORTED with a long delay
37 // - second attempt succeeds
CORE_END2END_TEST(RetryTest,RetryServerPushbackDelay)38 CORE_END2END_TEST(RetryTest, RetryServerPushbackDelay) {
39 InitServer(ChannelArgs());
40 InitClient(ChannelArgs().Set(
41 GRPC_ARG_SERVICE_CONFIG,
42 "{\n"
43 " \"methodConfig\": [ {\n"
44 " \"name\": [\n"
45 " { \"service\": \"service\", \"method\": \"method\" }\n"
46 " ],\n"
47 " \"retryPolicy\": {\n"
48 " \"maxAttempts\": 3,\n"
49 " \"initialBackoff\": \"1s\",\n"
50 " \"maxBackoff\": \"120s\",\n"
51 " \"backoffMultiplier\": 1.6,\n"
52 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
53 " }\n"
54 " } ]\n"
55 "}"));
56 auto c =
57 NewClientCall("/service/method").Timeout(Duration::Minutes(1)).Create();
58 EXPECT_NE(c.GetPeer(), absl::nullopt);
59 IncomingMessage server_message;
60 IncomingMetadata server_initial_metadata;
61 IncomingStatusOnClient server_status;
62 c.NewBatch(1)
63 .SendInitialMetadata({})
64 .SendMessage("foo")
65 .RecvMessage(server_message)
66 .SendCloseFromClient()
67 .RecvInitialMetadata(server_initial_metadata)
68 .RecvStatusOnClient(server_status);
69 absl::optional<IncomingCall> s = RequestCall(101);
70 Expect(101, true);
71 Step(Duration::Seconds(20));
72 EXPECT_NE(s->GetPeer(), absl::nullopt);
73 EXPECT_NE(c.GetPeer(), absl::nullopt);
74 IncomingCloseOnServer client_close;
75 s->NewBatch(102)
76 .SendInitialMetadata({})
77 .SendStatusFromServer(GRPC_STATUS_ABORTED, "message1",
78 {{"grpc-retry-pushback-ms", "2000"}})
79 .RecvCloseOnServer(client_close);
80 Expect(102, true);
81 Step();
82 const auto before_retry = Timestamp::Now();
83 s.reset();
84 s.emplace(RequestCall(201));
85 Expect(201, true);
86 Step();
87 const auto after_retry = Timestamp::Now();
88 const auto retry_delay = after_retry - before_retry;
89 // Configured back-off was 1 second, server push-back said 2 seconds.
90 // To avoid flakiness, we allow some fudge factor here.
91 EXPECT_GE(retry_delay, Duration::Milliseconds(1500));
92 EXPECT_NE(s->GetPeer(), absl::nullopt);
93 EXPECT_NE(c.GetPeer(), absl::nullopt);
94 IncomingCloseOnServer client_close2;
95 s->NewBatch(202)
96 .SendInitialMetadata({})
97 .SendStatusFromServer(GRPC_STATUS_OK, "message2", {})
98 .RecvCloseOnServer(client_close2);
99 Expect(202, true);
100 Expect(1, true);
101 Step();
102 EXPECT_EQ(server_status.status(), GRPC_STATUS_OK);
103 EXPECT_EQ(server_status.message(), "message2");
104 EXPECT_EQ(s->method(), "/service/method");
105 EXPECT_FALSE(client_close2.was_cancelled());
106 }
107
108 } // namespace
109 } // namespace grpc_core
110