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 <memory>
18
19 #include "gtest/gtest.h"
20
21 #include <grpc/impl/channel_arg_names.h>
22 #include <grpc/status.h>
23
24 #include "src/core/lib/channel/channel_args.h"
25 #include "src/core/lib/gprpp/time.h"
26 #include "test/core/end2end/end2end_tests.h"
27
28 namespace grpc_core {
29 namespace {
30 // Tests that we can unref a call whose status is cached but not yet
31 // requested by the application. This should not cause a memory leak.
CORE_END2END_TEST(RetryTest,RetryUnrefBeforeFinish)32 CORE_END2END_TEST(RetryTest, RetryUnrefBeforeFinish) {
33 InitServer(ChannelArgs());
34 InitClient(ChannelArgs().Set(
35 GRPC_ARG_SERVICE_CONFIG,
36 "{\n"
37 " \"methodConfig\": [ {\n"
38 " \"name\": [\n"
39 " { \"service\": \"service\", \"method\": \"method\" }\n"
40 " ],\n"
41 " \"retryPolicy\": {\n"
42 " \"maxAttempts\": 3,\n"
43 " \"initialBackoff\": \"1s\",\n"
44 " \"maxBackoff\": \"120s\",\n"
45 " \"backoffMultiplier\": 1.6,\n"
46 " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
47 " }\n"
48 " } ]\n"
49 "}"));
50 auto c =
51 NewClientCall("/service/method").Timeout(Duration::Seconds(5)).Create();
52 // Client starts send ops.
53 c.NewBatch(1)
54 .SendInitialMetadata({})
55 .SendMessage("foo")
56 .SendCloseFromClient();
57 // Client starts recv_initial_metadata and recv_message, but not
58 // recv_trailing_metadata.
59 IncomingMetadata server_initial_metadata;
60 IncomingMessage server_message;
61 c.NewBatch(2)
62 .RecvInitialMetadata(server_initial_metadata)
63 .RecvMessage(server_message);
64 // Server gets a call and client send ops complete.
65 auto s = RequestCall(101);
66 Expect(1, true);
67 Expect(101, true);
68 Step();
69 // Server immediately sends FAILED_PRECONDITION status (not retriable).
70 // This forces the retry filter to start a recv_trailing_metadata op
71 // internally, since the application hasn't started it yet.
72 IncomingCloseOnServer client_close;
73 s.NewBatch(102)
74 .SendInitialMetadata({})
75 .SendStatusFromServer(GRPC_STATUS_FAILED_PRECONDITION, "xyz", {})
76 .RecvCloseOnServer(client_close);
77 // Server ops complete and client recv ops complete.
78 Expect(2, true);
79 Expect(102, true);
80 Step();
81 EXPECT_EQ(s.method(), "/service/method");
82 EXPECT_FALSE(client_close.was_cancelled());
83 }
84 } // namespace
85 } // namespace grpc_core
86