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 #include "test/core/end2end/tests/cancel_test_helpers.h"
31
32 namespace grpc_core {
33 namespace {
34
35 // Tests retry cancellation.
TestRetryCancellation(CoreEnd2endTest & test,std::unique_ptr<CancellationMode> mode)36 void TestRetryCancellation(CoreEnd2endTest& test,
37 std::unique_ptr<CancellationMode> mode) {
38 test.InitServer(ChannelArgs());
39 test.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\": 5,\n"
48 " \"initialBackoff\": \"1s\",\n"
49 " \"maxBackoff\": \"120s\",\n"
50 " \"backoffMultiplier\": 1.6,\n"
51 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
52 " },\n"
53 " \"timeout\": \"10s\"\n"
54 " } ]\n"
55 "}"));
56 auto c = test.NewClientCall("/service/method")
57 .Timeout(Duration::Seconds(5))
58 .Create();
59 EXPECT_NE(c.GetPeer(), absl::nullopt);
60 // Client starts a batch with all 6 ops.
61 CoreEnd2endTest::IncomingMetadata server_initial_metadata;
62 CoreEnd2endTest::IncomingMessage server_message;
63 CoreEnd2endTest::IncomingStatusOnClient server_status;
64 c.NewBatch(1)
65 .SendInitialMetadata({})
66 .SendMessage("foo")
67 .RecvMessage(server_message)
68 .SendCloseFromClient()
69 .RecvInitialMetadata(server_initial_metadata)
70 .RecvStatusOnClient(server_status);
71 // Server gets a call and fails with retryable status.
72 absl::optional<CoreEnd2endTest::IncomingCall> s = test.RequestCall(101);
73 test.Expect(101, true);
74 test.Step();
75 EXPECT_NE(s->GetPeer(), absl::nullopt);
76 EXPECT_NE(c.GetPeer(), absl::nullopt);
77 CoreEnd2endTest::IncomingCloseOnServer client_close;
78 s->NewBatch(102)
79 .SendInitialMetadata({})
80 .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {})
81 .RecvCloseOnServer(client_close);
82 test.Expect(102, true);
83 test.Step();
84 s.reset();
85 // Server gets a second call (the retry).
86 s.emplace(test.RequestCall(201));
87 test.Expect(201, true);
88 test.Step();
89 // Initiate cancellation.
90 mode->Apply(c);
91 test.Expect(1, true);
92 test.Step();
93 EXPECT_EQ(server_status.status(), mode->ExpectedStatus());
94 EXPECT_FALSE(client_close.was_cancelled());
95 }
96
CORE_END2END_TEST(RetryTest,RetryCancellation)97 CORE_END2END_TEST(RetryTest, RetryCancellation) {
98 TestRetryCancellation(*this, std::make_unique<CancelCancellationMode>());
99 }
100
CORE_END2END_TEST(RetryTest,RetryDeadline)101 CORE_END2END_TEST(RetryTest, RetryDeadline) {
102 TestRetryCancellation(*this, std::make_unique<DeadlineCancellationMode>());
103 }
104
105 } // namespace
106 } // namespace grpc_core
107