1 //
2 // Copyright 2017 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <atomic>
18 #include <memory>
19
20 #include "absl/status/status.h"
21 #include "gtest/gtest.h"
22
23 #include <grpc/impl/channel_arg_names.h>
24 #include <grpc/status.h>
25
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/config/core_configuration.h"
28 #include "src/core/lib/gprpp/time.h"
29 #include "test/core/end2end/end2end_tests.h"
30 #include "test/core/util/test_lb_policies.h"
31
32 namespace grpc_core {
33 namespace {
34 std::atomic<int> g_num_lb_picks;
35
36 // Tests that we retry properly when the LB policy fails the call before
37 // it ever gets to the transport, even if recv_trailing_metadata isn't
38 // started by the application until after the LB pick fails.
39 // - 1 retry allowed for ABORTED status
40 // - on first attempt, LB policy fails with ABORTED before application
41 // starts recv_trailing_metadata op
CORE_END2END_TEST(RetryTest,RetryLbFail)42 CORE_END2END_TEST(RetryTest, RetryLbFail) {
43 CoreConfiguration::RegisterBuilder([](CoreConfiguration::Builder* builder) {
44 RegisterFailLoadBalancingPolicy(
45 builder, absl::UnavailableError("LB pick failed"), &g_num_lb_picks);
46 });
47 g_num_lb_picks.store(0, std::memory_order_relaxed);
48 InitServer(ChannelArgs());
49 InitClient(
50 ChannelArgs()
51 .Set(GRPC_ARG_ENABLE_RETRIES, true)
52 .Set(GRPC_ARG_SERVICE_CONFIG,
53 "{\n"
54 " \"loadBalancingConfig\": [ {\n"
55 " \"fail_lb\": {}\n"
56 " } ],\n"
57 " \"methodConfig\": [ {\n"
58 " \"name\": [\n"
59 " { \"service\": \"service\", \"method\": \"method\" }\n"
60 " ],\n"
61 " \"retryPolicy\": {\n"
62 " \"maxAttempts\": 2,\n"
63 " \"initialBackoff\": \"1s\",\n"
64 " \"maxBackoff\": \"120s\",\n"
65 " \"backoffMultiplier\": 1.6,\n"
66 " \"retryableStatusCodes\": [ \"UNAVAILABLE\" ]\n"
67 " }\n"
68 " } ]\n"
69 "}"));
70 auto c =
71 NewClientCall("/service/method").Timeout(Duration::Seconds(5)).Create();
72 c.NewBatch(1).SendInitialMetadata({});
73 Expect(1, false);
74 Step();
75 IncomingStatusOnClient server_status;
76 c.NewBatch(2).RecvStatusOnClient(server_status);
77 Expect(2, true);
78 Step();
79 EXPECT_EQ(server_status.status(), GRPC_STATUS_UNAVAILABLE);
80 EXPECT_EQ(server_status.message(), "LB pick failed");
81 EXPECT_EQ(g_num_lb_picks.load(std::memory_order_relaxed), 2);
82 }
83
84 } // namespace
85 } // namespace grpc_core
86