xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/cancel_with_status.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 "gtest/gtest.h"
22 
23 #include <grpc/status.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/string_util.h>
26 
27 #include "src/core/ext/transport/chttp2/transport/internal.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 
CORE_END2END_TEST(CoreEnd2endTest,CancelWithStatus1)34 CORE_END2END_TEST(CoreEnd2endTest, CancelWithStatus1) {
35   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
36   CoreEnd2endTest::IncomingStatusOnClient server_status;
37   c.NewBatch(1).RecvStatusOnClient(server_status);
38   char* dynamic_string = gpr_strdup("xyz");
39   c.CancelWithStatus(GRPC_STATUS_UNIMPLEMENTED, dynamic_string);
40   // The API of \a description allows for it to be a dynamic/non-const
41   // string, test this guarantee.
42   gpr_free(dynamic_string);
43   Expect(1, true);
44   Step();
45   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
46   EXPECT_EQ(server_status.message(), "xyz");
47 }
48 
CORE_END2END_TEST(CoreEnd2endTest,CancelWithStatus2)49 CORE_END2END_TEST(CoreEnd2endTest, CancelWithStatus2) {
50   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
51   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
52   CoreEnd2endTest::IncomingStatusOnClient server_status;
53   c.NewBatch(1)
54       .RecvStatusOnClient(server_status)
55       .RecvInitialMetadata(server_initial_metadata);
56   char* dynamic_string = gpr_strdup("xyz");
57   c.CancelWithStatus(GRPC_STATUS_UNIMPLEMENTED, dynamic_string);
58   // The API of \a description allows for it to be a dynamic/non-const
59   // string, test this guarantee.
60   gpr_free(dynamic_string);
61   Expect(1, true);
62   Step();
63   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
64   EXPECT_EQ(server_status.message(), "xyz");
65 }
66 
CORE_END2END_TEST(CoreEnd2endTest,CancelWithStatus3)67 CORE_END2END_TEST(CoreEnd2endTest, CancelWithStatus3) {
68   InitClient(ChannelArgs());
69   // This is a workaround for the flakiness that if the server ever enters
70   // GracefulShutdown for whatever reason while the client has already been
71   // shutdown, the test would not timeout and fail.
72   InitServer(ChannelArgs().Set(GRPC_ARG_PING_TIMEOUT_MS, 5000));
73   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
74   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
75   CoreEnd2endTest::IncomingStatusOnClient server_status;
76   c.NewBatch(1)
77       .RecvStatusOnClient(server_status)
78       .RecvInitialMetadata(server_initial_metadata)
79       .SendInitialMetadata({});
80   char* dynamic_string = gpr_strdup("xyz");
81   c.CancelWithStatus(GRPC_STATUS_UNIMPLEMENTED, dynamic_string);
82   // The API of \a description allows for it to be a dynamic/non-const
83   // string, test this guarantee.
84   gpr_free(dynamic_string);
85   Expect(1, true);
86   Step();
87   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
88   EXPECT_EQ(server_status.message(), "xyz");
89 }
90 
CORE_END2END_TEST(CoreEnd2endTest,CancelWithStatus4)91 CORE_END2END_TEST(CoreEnd2endTest, CancelWithStatus4) {
92   InitClient(ChannelArgs());
93   // This is a workaround for the flakiness that if the server ever enters
94   // GracefulShutdown for whatever reason while the client has already been
95   // shutdown, the test would not timeout and fail.
96   InitServer(ChannelArgs().Set(GRPC_ARG_PING_TIMEOUT_MS, 5000));
97   auto c = NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
98   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
99   CoreEnd2endTest::IncomingStatusOnClient server_status;
100   c.NewBatch(1)
101       .RecvStatusOnClient(server_status)
102       .RecvInitialMetadata(server_initial_metadata)
103       .SendInitialMetadata({})
104       .SendCloseFromClient();
105   char* dynamic_string = gpr_strdup("xyz");
106   c.CancelWithStatus(GRPC_STATUS_UNIMPLEMENTED, dynamic_string);
107   // The API of \a description allows for it to be a dynamic/non-const
108   // string, test this guarantee.
109   gpr_free(dynamic_string);
110   Expect(1, true);
111   Step();
112   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
113   EXPECT_EQ(server_status.message(), "xyz");
114 }
115 
116 }  // namespace
117 }  // namespace grpc_core
118