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 <string>
20
21 #include "absl/types/optional.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 #include <grpc/impl/channel_arg_names.h>
26 #include <grpc/status.h>
27
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/channel/channelz.h"
30 #include "src/core/lib/gprpp/time.h"
31 #include "src/core/lib/surface/channel.h"
32 #include "test/core/end2end/end2end_tests.h"
33
34 using testing::HasSubstr;
35
36 namespace grpc_core {
37 namespace {
38
39 // Tests retrying a streaming RPC. This is the same as
40 // the basic retry test, except that the client sends two messages on the
41 // call before the initial attempt fails.
42 // FIXME: We should also test the case where the retry is committed after
43 // replaying 1 of 2 previously-completed send_message ops. However,
44 // there's no way to trigger that from an end2end test, because the
45 // replayed ops happen under the hood -- they are not surfaced to the
46 // C-core API, and therefore we have no way to inject the commit at the
47 // right point.
CORE_END2END_TEST(RetryTest,RetryStreaming)48 CORE_END2END_TEST(RetryTest, RetryStreaming) {
49 InitServer(ChannelArgs());
50 InitClient(
51 ChannelArgs()
52 .Set(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE, 1024 * 8)
53 .Set(GRPC_ARG_ENABLE_CHANNELZ, true)
54 .Set(GRPC_ARG_SERVICE_CONFIG,
55 "{\n"
56 " \"methodConfig\": [ {\n"
57 " \"name\": [\n"
58 " { \"service\": \"service\", \"method\": \"method\" }\n"
59 " ],\n"
60 " \"retryPolicy\": {\n"
61 " \"maxAttempts\": 3,\n"
62 " \"initialBackoff\": \"1s\",\n"
63 " \"maxBackoff\": \"120s\",\n"
64 " \"backoffMultiplier\": 1.6,\n"
65 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
66 " }\n"
67 " } ]\n"
68 "}"));
69 auto c =
70 NewClientCall("/service/method").Timeout(Duration::Seconds(5)).Create();
71 channelz::ChannelNode* channelz_channel =
72 grpc_channel_get_channelz_node(client());
73 EXPECT_NE(c.GetPeer(), absl::nullopt);
74 // Client starts a batch for receiving initial metadata, a message,
75 // and trailing metadata.
76 IncomingStatusOnClient server_status;
77 IncomingMetadata server_initial_metadata;
78 IncomingMessage server_message;
79 c.NewBatch(1)
80 .RecvInitialMetadata(server_initial_metadata)
81 .RecvMessage(server_message)
82 .RecvStatusOnClient(server_status);
83 // Client sends initial metadata and a message.
84 c.NewBatch(2).SendInitialMetadata({}).SendMessage("foo");
85 Expect(2, true);
86 Step();
87 // Server gets a call with received initial metadata.
88 auto s = RequestCall(101);
89 Expect(101, true);
90 Step();
91 EXPECT_NE(s.GetPeer(), absl::nullopt);
92 EXPECT_NE(c.GetPeer(), absl::nullopt);
93 // Server receives a message.
94 IncomingMessage client_message;
95 s.NewBatch(102).RecvMessage(client_message);
96 Expect(102, true);
97 Step();
98 // Client sends a second message.
99 c.NewBatch(3).SendMessage("bar");
100 Expect(3, true);
101 Step();
102 // Server receives the second message.
103 IncomingMessage client_message2;
104 s.NewBatch(103).RecvMessage(client_message2);
105 Expect(103, true);
106 Step();
107 // Server sends both initial and trailing metadata.
108 IncomingCloseOnServer client_close;
109 s.NewBatch(104)
110 .RecvCloseOnServer(client_close)
111 .SendInitialMetadata({})
112 .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {});
113 Expect(104, true);
114 Step();
115 // Clean up from first attempt.
116 EXPECT_EQ(client_message.payload(), "foo");
117 EXPECT_EQ(client_message2.payload(), "bar");
118 // Server gets a second call (the retry).
119 auto s2 = RequestCall(201);
120 Expect(201, true);
121 Step();
122 EXPECT_NE(s.GetPeer(), absl::nullopt);
123 EXPECT_NE(c.GetPeer(), absl::nullopt);
124 // Server receives a message.
125 IncomingMessage client_message3;
126 s2.NewBatch(202).RecvMessage(client_message3);
127 Expect(202, true);
128 Step();
129 // Server receives a second message.
130 IncomingMessage client_message4;
131 s2.NewBatch(203).RecvMessage(client_message4);
132 Expect(203, true);
133 Step();
134 // Client sends a third message and a close.
135 c.NewBatch(4).SendMessage("baz").SendCloseFromClient();
136 Expect(4, true);
137 Step();
138 // Server receives a third message.
139 IncomingMessage client_message5;
140 s2.NewBatch(204).RecvMessage(client_message5);
141 Expect(204, true);
142 Step();
143 // Server receives a close and sends initial metadata, a message, and
144 // trailing metadata.
145 IncomingCloseOnServer client_close2;
146 s2.NewBatch(205)
147 .RecvCloseOnServer(client_close2)
148 .SendInitialMetadata({})
149 .SendMessage("quux")
150 // Returning a retriable code, but because we are also sending a
151 // message, the client will commit instead of retrying again.
152 .SendStatusFromServer(GRPC_STATUS_ABORTED, "xyz", {});
153 Expect(205, true);
154 Expect(1, true);
155 Step();
156 EXPECT_EQ(server_status.status(), GRPC_STATUS_ABORTED);
157 EXPECT_EQ(server_status.message(), "xyz");
158 EXPECT_EQ(s.method(), "/service/method");
159 EXPECT_FALSE(client_close.was_cancelled());
160 EXPECT_NE(channelz_channel, nullptr);
161 // TODO(roth): consider using a regex check here.
162 std::string json = channelz_channel->RenderJsonString();
163 EXPECT_THAT(json, HasSubstr("\"trace\""));
164 EXPECT_THAT(json, HasSubstr("\"description\":\"Channel created\""));
165 EXPECT_THAT(json, HasSubstr("\"severity\":\"CT_INFO\""));
166 EXPECT_THAT(json, HasSubstr("Resolution event"));
167 EXPECT_THAT(json, HasSubstr("Created new LB policy"));
168 EXPECT_THAT(json, HasSubstr("Service config changed"));
169 EXPECT_THAT(json, HasSubstr("Address list became non-empty"));
170 EXPECT_THAT(json, HasSubstr("Channel state change to CONNECTING"));
171 EXPECT_EQ(client_message3.payload(), "foo");
172 EXPECT_EQ(client_message4.payload(), "bar");
173 EXPECT_EQ(client_message5.payload(), "baz");
174 }
175
176 } // namespace
177 } // namespace grpc_core
178