xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/simple_request.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 <stdint.h>
20 
21 #include <algorithm>
22 #include <memory>
23 #include <string>
24 
25 #include "absl/strings/match.h"
26 #include "absl/types/optional.h"
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 
30 #include <grpc/status.h>
31 #include <grpc/support/log.h>
32 
33 #include "src/core/lib/debug/stats.h"
34 #include "src/core/lib/debug/stats_data.h"
35 #include "src/core/lib/gprpp/time.h"
36 #include "test/core/end2end/end2end_tests.h"
37 
38 using testing::HasSubstr;
39 using testing::StartsWith;
40 
41 namespace grpc_core {
42 namespace {
CheckPeer(std::string peer_name)43 void CheckPeer(std::string peer_name) {
44   // If the peer name is a uds path, then check if it is filled
45   if (absl::StartsWith(peer_name, "unix:/")) {
46     EXPECT_THAT(peer_name, StartsWith("unix:/tmp/grpc_fullstack_test."));
47   }
48 }
49 
SimpleRequestBody(CoreEnd2endTest & test)50 void SimpleRequestBody(CoreEnd2endTest& test) {
51   auto before = global_stats().Collect();
52   auto c = test.NewClientCall("/foo").Timeout(Duration::Minutes(1)).Create();
53   EXPECT_NE(c.GetPeer(), absl::nullopt);
54   CoreEnd2endTest::IncomingStatusOnClient server_status;
55   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
56   c.NewBatch(1)
57       .SendInitialMetadata({})
58       .SendCloseFromClient()
59       .RecvInitialMetadata(server_initial_metadata)
60       .RecvStatusOnClient(server_status);
61   auto s = test.RequestCall(101);
62   test.Expect(101, true);
63   test.Step();
64   EXPECT_NE(s.GetPeer(), absl::nullopt);
65   CheckPeer(*s.GetPeer());
66   EXPECT_NE(c.GetPeer(), absl::nullopt);
67   CheckPeer(*c.GetPeer());
68   CoreEnd2endTest::IncomingCloseOnServer client_close;
69   s.NewBatch(102)
70       .SendInitialMetadata({})
71       .SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {})
72       .RecvCloseOnServer(client_close);
73   test.Expect(102, true);
74   test.Expect(1, true);
75   test.Step();
76   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
77   EXPECT_EQ(server_status.message(), "xyz");
78   // the following sanity check makes sure that the requested error string is
79   // correctly populated by the core. It looks for certain substrings that are
80   // not likely to change much. Some parts of the error, like time created,
81   // obviously are not checked.
82   EXPECT_THAT(server_status.error_string(), HasSubstr("xyz"));
83   EXPECT_THAT(server_status.error_string(),
84               HasSubstr("Error received from peer"));
85   EXPECT_THAT(server_status.error_string(), HasSubstr("grpc_message"));
86   EXPECT_THAT(server_status.error_string(), HasSubstr("grpc_status"));
87   EXPECT_EQ(s.method(), "/foo");
88   EXPECT_FALSE(client_close.was_cancelled());
89   uint64_t expected_calls = 1;
90   if (test.GetParam()->feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
91     expected_calls *= 2;
92   }
93   auto after = global_stats().Collect();
94   gpr_log(GPR_DEBUG, "%s", StatsAsJson(after.get()).c_str());
95   EXPECT_EQ(after->client_calls_created - before->client_calls_created,
96             expected_calls);
97   EXPECT_EQ(after->server_calls_created - before->server_calls_created,
98             expected_calls);
99 }
100 
CORE_END2END_TEST(CoreEnd2endTest,SimpleRequest)101 CORE_END2END_TEST(CoreEnd2endTest, SimpleRequest) { SimpleRequestBody(*this); }
102 
CORE_END2END_TEST(CoreEnd2endTest,SimpleRequest10)103 CORE_END2END_TEST(CoreEnd2endTest, SimpleRequest10) {
104   for (int i = 0; i < 10; i++) {
105     SimpleRequestBody(*this);
106   }
107 }
108 }  // namespace
109 }  // namespace grpc_core
110