xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/retry_streaming_after_commit.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 we can continue to send/recv messages on a streaming call
35 // after retries are committed.
CORE_END2END_TEST(RetryTest,RetryStreamingAfterCommit)36 CORE_END2END_TEST(RetryTest, RetryStreamingAfterCommit) {
37   InitServer(ChannelArgs());
38   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\": 3,\n"
47       "      \"initialBackoff\": \"1s\",\n"
48       "      \"maxBackoff\": \"120s\",\n"
49       "      \"backoffMultiplier\": 1.6,\n"
50       "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
51       "    }\n"
52       "  } ]\n"
53       "}"));
54   auto c =
55       NewClientCall("/service/method").Timeout(Duration::Minutes(1)).Create();
56   EXPECT_NE(c.GetPeer(), absl::nullopt);
57   // Client starts a batch for receiving initial metadata and a message.
58   // This will commit retries.
59   IncomingMessage server_message;
60   IncomingMetadata server_initial_metadata;
61   c.NewBatch(2)
62       .RecvInitialMetadata(server_initial_metadata)
63       .RecvMessage(server_message);
64   // Client sends initial metadata and a message.
65   c.NewBatch(3).SendInitialMetadata({}).SendMessage("foo");
66   Expect(3, true);
67   Step();
68   // Server gets a call with received initial metadata.
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   // Server receives a message.
75   IncomingMessage client_message;
76   s.NewBatch(102).RecvMessage(client_message);
77   Expect(102, true);
78   Step();
79   // Server sends initial metadata and a message.
80   s.NewBatch(103).SendInitialMetadata({}).SendMessage("bar");
81   Expect(103, true);
82   // Client receives initial metadata and a message.
83   Expect(2, true);
84   Step();
85   // Client sends a second message and a close.
86   c.NewBatch(4).SendMessage("baz").SendCloseFromClient();
87   Expect(4, true);
88   Step();
89   // Server receives a second message.
90   IncomingMessage client_message2;
91   s.NewBatch(104).RecvMessage(client_message2);
92   Expect(104, true);
93   Step();
94   // Server receives a close, sends a second message, and sends status.
95   // Returning a retriable code, but because retries are already
96   // committed, the client will not retry.
97   IncomingCloseOnServer client_close;
98   s.NewBatch(105)
99       .RecvCloseOnServer(client_close)
100       .SendMessage("quux")
101       .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {});
102   Expect(105, true);
103   Step();
104   // Client receives a second message.
105   IncomingMessage server_message2;
106   c.NewBatch(5).RecvMessage(server_message2);
107   Expect(5, true);
108   Step();
109   // Client receives status.
110   IncomingStatusOnClient server_status;
111   c.NewBatch(1).RecvStatusOnClient(server_status);
112   Expect(1, true);
113   Step();
114   EXPECT_EQ(server_status.status(), GRPC_STATUS_ABORTED);
115   EXPECT_EQ(server_status.message(), "xyz");
116   EXPECT_EQ(s.method(), "/service/method");
117   EXPECT_FALSE(client_close.was_cancelled());
118   EXPECT_EQ(client_message.payload(), "foo");
119   EXPECT_EQ(server_message.payload(), "bar");
120   EXPECT_EQ(client_message2.payload(), "baz");
121   EXPECT_EQ(server_message2.payload(), "quux");
122 }
123 
124 }  // namespace
125 }  // namespace grpc_core
126