xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/retry_unref_before_recv.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 "absl/types/optional.h"
20 #include "gtest/gtest.h"
21 
22 #include <grpc/impl/channel_arg_names.h>
23 #include <grpc/status.h>
24 
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gprpp/time.h"
27 #include "test/core/end2end/end2end_tests.h"
28 
29 namespace grpc_core {
30 namespace {
31 
32 // Tests that we can unref a call while recv ops are started but before
33 // they complete.  This ensures that we don't drop callbacks or cause a
34 // memory leak.
CORE_END2END_TEST(RetryTest,UnrefBeforeRecv)35 CORE_END2END_TEST(RetryTest, UnrefBeforeRecv) {
36   InitServer(ChannelArgs());
37   InitClient(ChannelArgs().Set(
38       GRPC_ARG_SERVICE_CONFIG,
39       "{\n"
40       "  \"methodConfig\": [ {\n"
41       "    \"name\": [\n"
42       "      { \"service\": \"service\", \"method\": \"method\" }\n"
43       "    ],\n"
44       "    \"retryPolicy\": {\n"
45       "      \"maxAttempts\": 3,\n"
46       "      \"initialBackoff\": \"1s\",\n"
47       "      \"maxBackoff\": \"120s\",\n"
48       "      \"backoffMultiplier\": 1.6,\n"
49       "      \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
50       "    }\n"
51       "  } ]\n"
52       "}"));
53   absl::optional<Call> c{
54       NewClientCall("/service/method").Timeout(Duration::Seconds(60)).Create()};
55 
56   // Client starts send ops.
57   c->NewBatch(1)
58       .SendInitialMetadata({})
59       .SendMessage("foo")
60       .SendCloseFromClient();
61   // Client starts recv_initial_metadata and recv_message, but not
62   // recv_trailing_metadata.
63   IncomingMetadata server_initial_metadata;
64   IncomingMessage server_message;
65   c->NewBatch(2)
66       .RecvInitialMetadata(server_initial_metadata)
67       .RecvMessage(server_message);
68   // Server gets a call and client send ops complete.
69   auto s = RequestCall(101);
70   Expect(1, true);
71   Expect(101, true);
72   Step();
73   // Client unrefs the call without starting recv_trailing_metadata.
74   // This should trigger a cancellation.
75   c.reset();
76   // Server immediately sends FAILED_PRECONDITION status (not retriable).
77   // This forces the retry filter to start a recv_trailing_metadata op
78   // internally, since the application hasn't started it yet.
79   IncomingCloseOnServer client_close;
80   s.NewBatch(102)
81       .SendInitialMetadata({})
82       .SendStatusFromServer(GRPC_STATUS_FAILED_PRECONDITION, "xyz", {})
83       .RecvCloseOnServer(client_close);
84   // Server ops complete and client recv ops complete.
85   Expect(2, false);  // Failure!
86   Expect(102, true);
87   Step();
88 
89   EXPECT_EQ(s.method(), "/service/method");
90   // Note: Not checking the value of was_cancelled here, because it will
91   // be flaky, depending on whether the server sent its response before
92   // the client sent its cancellation.
93 }
94 
95 }  // namespace
96 }  // namespace grpc_core
97