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