1 //
2 // Copyright 2017 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <memory>
18
19 #include "absl/strings/str_format.h"
20 #include "absl/types/optional.h"
21 #include "gtest/gtest.h"
22
23 #include <grpc/impl/channel_arg_names.h>
24 #include <grpc/status.h>
25
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/gprpp/time.h"
28 #include "test/core/end2end/end2end_tests.h"
29 #include "test/core/end2end/tests/cancel_test_helpers.h"
30 #include "test/core/util/test_config.h"
31
32 namespace grpc_core {
33 namespace {
34
TestRetryCancelDuringDelay(CoreEnd2endTest & test,std::unique_ptr<CancellationMode> cancellation_mode)35 void TestRetryCancelDuringDelay(
36 CoreEnd2endTest& test,
37 std::unique_ptr<CancellationMode> cancellation_mode) {
38 test.InitServer(ChannelArgs());
39 test.InitClient(ChannelArgs().Set(
40 GRPC_ARG_SERVICE_CONFIG,
41 absl::StrFormat(
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\": \"%ds\",\n"
50 " \"maxBackoff\": \"120s\",\n"
51 " \"backoffMultiplier\": 1.6,\n"
52 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
53 " }\n"
54 " } ]\n"
55 "}",
56 35 * grpc_test_slowdown_factor())));
57 auto expect_finish_before = test.TimestampAfterDuration(Duration::Minutes(2));
58 auto c = test.NewClientCall("/service/method")
59 .Timeout(Duration::Seconds(30))
60 .Create();
61 EXPECT_NE(c.GetPeer(), absl::nullopt);
62 // Client starts a batch with all 6 ops.
63 CoreEnd2endTest::IncomingMetadata server_initial_metadata;
64 CoreEnd2endTest::IncomingMessage server_message;
65 CoreEnd2endTest::IncomingStatusOnClient server_status;
66 c.NewBatch(1)
67 .SendInitialMetadata({})
68 .SendMessage("foo")
69 .RecvMessage(server_message)
70 .RecvInitialMetadata(server_initial_metadata)
71 .RecvStatusOnClient(server_status);
72 // Server gets a call and fails with retryable status.
73 auto s = test.RequestCall(101);
74 test.Expect(101, true);
75 test.Step();
76 EXPECT_NE(s.GetPeer(), absl::nullopt);
77 EXPECT_NE(c.GetPeer(), absl::nullopt);
78 CoreEnd2endTest::IncomingCloseOnServer client_close;
79 s.NewBatch(102)
80 .SendInitialMetadata({})
81 .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {})
82 .RecvCloseOnServer(client_close);
83 test.Expect(102, true);
84 test.Step();
85 // Server should never get a second call, because the initial retry
86 // delay is longer than the call's deadline.
87 auto s2 = test.RequestCall(201);
88 // Initiate cancellation.
89 cancellation_mode->Apply(c);
90 test.Expect(1, true);
91 test.Step(Duration::Minutes(1));
92 auto finish_time = Timestamp::Now();
93 EXPECT_EQ(server_status.status(), cancellation_mode->ExpectedStatus())
94 << server_status.message();
95 EXPECT_FALSE(client_close.was_cancelled());
96 // Make sure we didn't wait the full deadline before failing.
97 EXPECT_LT(finish_time, expect_finish_before);
98 // Shutdown the server to gc the requested call.
99 test.ShutdownServerAndNotify(1000);
100 test.Expect(1000, true);
101 test.Expect(201, false);
102 test.Step();
103 }
104
CORE_END2END_TEST(RetryTest,CancelDuringDelay)105 CORE_END2END_TEST(RetryTest, CancelDuringDelay) {
106 TestRetryCancelDuringDelay(*this, std::make_unique<CancelCancellationMode>());
107 }
108
CORE_END2END_TEST(RetryTest,DeadlineDuringDelay)109 CORE_END2END_TEST(RetryTest, DeadlineDuringDelay) {
110 TestRetryCancelDuringDelay(*this,
111 std::make_unique<DeadlineCancellationMode>());
112 }
113
114 } // namespace
115 } // namespace grpc_core
116