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