xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/retry_recv_message.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 receiving a message commits the call.
35 // - 1 retry allowed for ABORTED status
36 // - first attempt receives a message and therefore does not retry even
37 //   though the final status is ABORTED
CORE_END2END_TEST(RetryTest,RetryRecvMessage)38 CORE_END2END_TEST(RetryTest, RetryRecvMessage) {
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\": 2,\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   auto s = RequestCall(101);
70   Expect(101, true);
71   Step();
72   EXPECT_NE(s.GetPeer(), absl::nullopt);
73   EXPECT_NE(c.GetPeer(), absl::nullopt);
74   IncomingCloseOnServer client_close;
75   s.NewBatch(103)
76       .SendInitialMetadata({})
77       .SendMessage("bar")
78       .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {})
79       .RecvCloseOnServer(client_close);
80   Expect(103, true);
81   Expect(1, true);
82   Step();
83   EXPECT_EQ(server_status.status(), GRPC_STATUS_ABORTED);
84   EXPECT_EQ(server_status.message(), "xyz");
85   EXPECT_EQ(s.method(), "/service/method");
86   EXPECT_FALSE(client_close.was_cancelled());
87 }
88 
89 }  // namespace
90 }  // namespace grpc_core
91