xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/no_logging.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2016 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 <atomic>
20 #include <map>
21 #include <regex>
22 #include <string>
23 #include <utility>
24 
25 #include "absl/strings/str_cat.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/types/optional.h"
28 #include "gtest/gtest.h"
29 
30 #include <grpc/grpc.h>
31 #include <grpc/status.h>
32 #include <grpc/support/log.h>
33 
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/gprpp/time.h"
36 #include "test/core/end2end/end2end_tests.h"
37 
38 void gpr_default_log(gpr_log_func_args* args);
39 
40 namespace grpc_core {
41 
42 class Verifier {
43  public:
Verifier()44   Verifier() {
45     if (gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) {
46       saved_severity_ = GPR_LOG_SEVERITY_DEBUG;
47     } else if (gpr_should_log(GPR_LOG_SEVERITY_INFO)) {
48       saved_severity_ = GPR_LOG_SEVERITY_INFO;
49     } else if (gpr_should_log(GPR_LOG_SEVERITY_ERROR)) {
50       saved_severity_ = GPR_LOG_SEVERITY_ERROR;
51     } else {
52       saved_severity_ =
53           static_cast<gpr_log_severity>(GPR_LOG_SEVERITY_ERROR + 1);
54     }
55     grpc_tracer_set_enabled("all", 0);
56     gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
57     gpr_set_log_function(DispatchLog);
58   }
~Verifier()59   ~Verifier() {
60     gpr_set_log_function(gpr_default_log);
61     saved_trace_flags_.Restore();
62     gpr_set_log_verbosity(saved_severity_);
63   }
64   Verifier(const Verifier&) = delete;
65   Verifier& operator=(const Verifier&) = delete;
66 
FailOnAnyLog()67   void FailOnAnyLog() { g_log_func_.store(NoLog); }
FailOnNonErrorLog()68   void FailOnNonErrorLog() { g_log_func_.store(NoErrorLog); }
69 
70  private:
DispatchLog(gpr_log_func_args * args)71   static void DispatchLog(gpr_log_func_args* args) { g_log_func_.load()(args); }
72 
NoLog(gpr_log_func_args * args)73   static void NoLog(gpr_log_func_args* args) {
74     static const auto* const allowed_logs_by_module =
75         new std::map<absl::string_view, std::regex>(
76             {{"cq_verifier.cc", std::regex("^Verify .* for [0-9]+ms")},
77              {"chttp2_transport.cc",
78               std::regex("Sending goaway.*Channel Destroyed")}});
79     absl::string_view filename = args->file;
80     auto slash = filename.rfind('/');
81     if (slash != absl::string_view::npos) {
82       filename = filename.substr(slash + 1);
83     }
84     slash = filename.rfind('\\');
85     if (slash != absl::string_view::npos) {
86       filename = filename.substr(slash + 1);
87     }
88     auto it = allowed_logs_by_module->find(filename);
89     if (it != allowed_logs_by_module->end() &&
90         std::regex_search(args->message, it->second)) {
91       gpr_default_log(args);
92       return;
93     }
94     std::string message = absl::StrCat("Unwanted log: ", args->message);
95     args->message = message.c_str();
96     gpr_default_log(args);
97     GTEST_FAIL();
98   }
99 
NoErrorLog(gpr_log_func_args * args)100   static void NoErrorLog(gpr_log_func_args* args) {
101     if (args->severity == GPR_LOG_SEVERITY_ERROR) {
102       NoLog(args);
103     }
104   }
105 
106   gpr_log_severity saved_severity_;
107   SavedTraceFlags saved_trace_flags_;
108   static std::atomic<gpr_log_func> g_log_func_;
109 };
110 
111 std::atomic<gpr_log_func> Verifier::g_log_func_(gpr_default_log);
112 
SimpleRequest(CoreEnd2endTest & test)113 void SimpleRequest(CoreEnd2endTest& test) {
114   auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(5)).Create();
115   EXPECT_NE(c.GetPeer(), absl::nullopt);
116   CoreEnd2endTest::IncomingMetadata server_initial_metadata;
117   CoreEnd2endTest::IncomingStatusOnClient server_status;
118   c.NewBatch(1)
119       .SendInitialMetadata({})
120       .SendCloseFromClient()
121       .RecvInitialMetadata(server_initial_metadata)
122       .RecvStatusOnClient(server_status);
123   auto s = test.RequestCall(101);
124   test.Expect(101, true);
125   test.Step();
126   EXPECT_NE(c.GetPeer(), absl::nullopt);
127   EXPECT_NE(s.GetPeer(), absl::nullopt);
128   CoreEnd2endTest::IncomingCloseOnServer client_close;
129   s.NewBatch(102)
130       .SendInitialMetadata({})
131       .SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {})
132       .RecvCloseOnServer(client_close);
133   test.Expect(102, true);
134   test.Expect(1, true);
135   test.Step();
136   EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
137   EXPECT_EQ(server_status.message(), "xyz");
138   EXPECT_EQ(s.method(), "/foo");
139   EXPECT_FALSE(client_close.was_cancelled());
140 }
141 
CORE_END2END_TEST(NoLoggingTest,NoLoggingTest)142 CORE_END2END_TEST(NoLoggingTest, NoLoggingTest) {
143 // TODO(hork): remove when the listener flake is identified
144 #ifdef GPR_WINDOWS
145   if (IsEventEngineListenerEnabled()) {
146     GTEST_SKIP() << "not for windows + event engine listener";
147   }
148 #endif
149   Verifier verifier;
150   verifier.FailOnNonErrorLog();
151   for (int i = 0; i < 10; i++) {
152     SimpleRequest(*this);
153   }
154   verifier.FailOnAnyLog();
155   SimpleRequest(*this);
156 }
157 
158 }  // namespace grpc_core
159