Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "test/test_main_lib.h" |
| 12 | |
| 13 | #include <fstream> |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 14 | #include <memory> |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 17 | #include "absl/flags/flag.h" |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Tommi | ec3ba73 | 2020-05-17 12:33:40 | [diff] [blame] | 19 | #include "absl/strings/match.h" |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 22 | #include "rtc_base/event_tracer.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Niels Möller | 04a3cc1 | 2019-05-21 11:01:58 | [diff] [blame] | 24 | #include "rtc_base/ssl_adapter.h" |
| 25 | #include "rtc_base/ssl_stream_adapter.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
| 27 | #include "system_wrappers/include/field_trial.h" |
| 28 | #include "system_wrappers/include/metrics.h" |
| 29 | #include "test/field_trial.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 30 | #include "test/gtest.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 31 | #include "test/testsupport/perf_test.h" |
Patrik Höglund | 844600e | 2019-10-15 10:19:36 | [diff] [blame] | 32 | #include "test/testsupport/resources_dir_flag.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 33 | |
| 34 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 35 | #include "rtc_base/win32_socket_init.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | #if defined(WEBRTC_IOS) |
| 39 | #include "test/ios/test_support.h" |
| 40 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 41 | ABSL_FLAG(std::string, |
| 42 | NSTreatUnknownArgumentsAsOpen, |
| 43 | "", |
| 44 | "Intentionally ignored flag intended for iOS simulator."); |
| 45 | ABSL_FLAG(std::string, |
| 46 | ApplePersistenceIgnoreState, |
| 47 | "", |
| 48 | "Intentionally ignored flag intended for iOS simulator."); |
Patrik Höglund | 3428827 | 2020-03-19 07:51:35 | [diff] [blame] | 49 | |
| 50 | // This is the cousin of isolated_script_test_perf_output, but we can't dictate |
| 51 | // where to write on iOS so the semantics of this flag are a bit different. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 52 | ABSL_FLAG( |
| 53 | bool, |
Patrik Höglund | 3428827 | 2020-03-19 07:51:35 | [diff] [blame] | 54 | write_perf_output_on_ios, |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 55 | false, |
Patrik Höglund | 3428827 | 2020-03-19 07:51:35 | [diff] [blame] | 56 | "Store the perf results in Documents/perftest_result.pb in the format " |
| 57 | "described by histogram.proto in " |
| 58 | "https://chromium.googlesource.com/catapult/."); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 59 | |
| 60 | #else |
| 61 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 62 | ABSL_FLAG( |
| 63 | std::string, |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 64 | isolated_script_test_perf_output, |
| 65 | "", |
Patrik Höglund | 3428827 | 2020-03-19 07:51:35 | [diff] [blame] | 66 | "Path where the perf results should be stored in proto format described " |
| 67 | "described by histogram.proto in " |
| 68 | "https://chromium.googlesource.com/catapult/."); |
| 69 | |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 70 | #endif |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 71 | |
Artem Titov | 087be5c | 2019-09-12 18:30:54 | [diff] [blame] | 72 | constexpr char kPlotAllMetrics[] = "all"; |
| 73 | ABSL_FLAG(std::vector<std::string>, |
| 74 | plot, |
| 75 | {}, |
| 76 | "List of metrics that should be exported for plotting (if they are " |
| 77 | "available). Example: psnr,ssim,encode_time. To plot all available " |
| 78 | " metrics pass 'all' as flag value"); |
| 79 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 80 | ABSL_FLAG(bool, logs, true, "print logs to stderr"); |
| 81 | ABSL_FLAG(bool, verbose, false, "verbose logs to stderr"); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 82 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 83 | ABSL_FLAG(std::string, |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 84 | trace_event, |
| 85 | "", |
| 86 | "Path to collect trace events (json file) for chrome://tracing. " |
| 87 | "If not set, events aren't captured."); |
| 88 | |
| 89 | ABSL_FLAG(std::string, |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 90 | force_fieldtrials, |
| 91 | "", |
| 92 | "Field trials control experimental feature code which can be forced. " |
| 93 | "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/" |
| 94 | " will assign the group Enable to field trial WebRTC-FooFeature."); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 95 | |
| 96 | namespace webrtc { |
| 97 | |
| 98 | namespace { |
| 99 | |
| 100 | class TestMainImpl : public TestMain { |
| 101 | public: |
Tommi | 9b7232a | 2020-05-15 08:09:27 | [diff] [blame] | 102 | // In order to set up a fresh rtc::Thread state for each test and avoid |
| 103 | // accidentally carrying over pending tasks that might be sent from one test |
| 104 | // and executed while another test is running, we inject a TestListener |
| 105 | // that sets up a new rtc::Thread instance for the main thread, per test. |
| 106 | class TestListener : public ::testing::EmptyTestEventListener { |
| 107 | public: |
| 108 | TestListener() = default; |
| 109 | |
| 110 | private: |
Tommi | ec3ba73 | 2020-05-17 12:33:40 | [diff] [blame] | 111 | bool IsDeathTest(const char* test_case_name, const char* test_name) { |
| 112 | // Workaround to avoid wrapping the main thread when we run death tests. |
| 113 | // The approach we take for detecting death tests is essentially the same |
| 114 | // as gtest does internally. Gtest does this: |
| 115 | // |
| 116 | // static const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*"; |
| 117 | // ::testing::internal::UnitTestOptions::MatchesFilter( |
| 118 | // test_case_name, kDeathTestCaseFilter); |
| 119 | // |
| 120 | // Our approach is a little more straight forward. |
| 121 | if (absl::EndsWith(test_case_name, "DeathTest")) |
| 122 | return true; |
| 123 | |
| 124 | return absl::EndsWith(test_name, "DeathTest"); |
| 125 | } |
| 126 | |
Tommi | 9b7232a | 2020-05-15 08:09:27 | [diff] [blame] | 127 | void OnTestStart(const ::testing::TestInfo& test_info) override { |
Tommi | ec3ba73 | 2020-05-17 12:33:40 | [diff] [blame] | 128 | if (!IsDeathTest(test_info.test_suite_name(), test_info.name())) { |
| 129 | // Ensure that main thread gets wrapped as an rtc::Thread. |
| 130 | // TODO(bugs.webrtc.org/9714): It might be better to avoid wrapping the |
| 131 | // main thread, or leave it to individual tests that need it. But as |
| 132 | // long as we have automatic thread wrapping, we need this to avoid that |
| 133 | // some other random thread (which one depending on which tests are run) |
| 134 | // gets automatically wrapped. |
| 135 | thread_ = rtc::Thread::CreateWithSocketServer(); |
| 136 | thread_->WrapCurrent(); |
| 137 | RTC_DCHECK_EQ(rtc::Thread::Current(), thread_.get()); |
| 138 | } else { |
| 139 | RTC_LOG(LS_INFO) << "No thread auto wrap for death test."; |
| 140 | } |
Tommi | 9b7232a | 2020-05-15 08:09:27 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void OnTestEnd(const ::testing::TestInfo& test_info) override { |
| 144 | // Terminate the message loop. Note that if the test failed to clean |
| 145 | // up pending messages, this may execute part of the test. Ideally we |
| 146 | // should print a warning message here, or even fail the test if it leaks. |
Tommi | ec3ba73 | 2020-05-17 12:33:40 | [diff] [blame] | 147 | if (thread_) { |
| 148 | thread_->Quit(); // Signal quit. |
| 149 | thread_->Run(); // Flush + process Quit signal. |
| 150 | thread_->UnwrapCurrent(); |
| 151 | thread_ = nullptr; |
| 152 | } |
Tommi | 9b7232a | 2020-05-15 08:09:27 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | std::unique_ptr<rtc::Thread> thread_; |
| 156 | }; |
| 157 | |
Artem Titov | bcb42f1 | 2020-08-11 10:19:18 | [diff] [blame] | 158 | int Init(int* argc, char* argv[]) override { return Init(); } |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 159 | |
Artem Titov | bcb42f1 | 2020-08-11 10:19:18 | [diff] [blame] | 160 | int Init() override { |
Patrik Höglund | 2bc1ea0 | 2019-10-16 08:24:35 | [diff] [blame] | 161 | // Make sure we always pull in the --resources_dir flag, even if the test |
| 162 | // binary doesn't link with fileutils (downstream expects all test mains to |
| 163 | // have this flag). |
| 164 | (void)absl::GetFlag(FLAGS_resources_dir); |
Patrik Höglund | 2f28370 | 2019-10-14 08:12:18 | [diff] [blame] | 165 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 166 | // Default to LS_INFO, even for release builds to provide better test |
| 167 | // logging. |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 168 | if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) |
| 169 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 170 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 171 | if (absl::GetFlag(FLAGS_verbose)) |
Niels Möller | d0def19 | 2018-12-21 10:28:45 | [diff] [blame] | 172 | rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE); |
| 173 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 174 | rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) || |
| 175 | absl::GetFlag(FLAGS_verbose)); |
Niels Möller | d0def19 | 2018-12-21 10:28:45 | [diff] [blame] | 176 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 177 | // InitFieldTrialsFromString stores the char*, so the char array must |
| 178 | // outlive the application. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 179 | field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials); |
| 180 | webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str()); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 181 | webrtc::metrics::Enable(); |
| 182 | |
| 183 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 184 | winsock_init_ = std::make_unique<rtc::WinsockInitializer>(); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 185 | #endif |
| 186 | |
Niels Möller | 04a3cc1 | 2019-05-21 11:01:58 | [diff] [blame] | 187 | // Initialize SSL which are used by several tests. |
| 188 | rtc::InitializeSSL(); |
| 189 | rtc::SSLStreamAdapter::EnableTimeCallbackForTesting(); |
| 190 | |
Tommi | 9b7232a | 2020-05-15 08:09:27 | [diff] [blame] | 191 | ::testing::UnitTest::GetInstance()->listeners().Append(new TestListener()); |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 192 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int Run(int argc, char* argv[]) override { |
Yves Gerey | 071d025 | 2020-01-28 19:07:19 | [diff] [blame] | 197 | std::string trace_event_path = absl::GetFlag(FLAGS_trace_event); |
| 198 | const bool capture_events = !trace_event_path.empty(); |
| 199 | if (capture_events) { |
| 200 | rtc::tracing::SetupInternalTracer(); |
| 201 | rtc::tracing::StartInternalCapture(trace_event_path.c_str()); |
| 202 | } |
| 203 | |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 204 | absl::optional<std::vector<std::string>> metrics_to_plot = |
| 205 | absl::GetFlag(FLAGS_plot); |
| 206 | |
| 207 | if (metrics_to_plot->empty()) { |
| 208 | metrics_to_plot = absl::nullopt; |
| 209 | } else { |
| 210 | if (metrics_to_plot->size() == 1 && |
| 211 | (*metrics_to_plot)[0] == kPlotAllMetrics) { |
| 212 | metrics_to_plot->clear(); |
| 213 | } |
| 214 | } |
| 215 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 216 | #if defined(WEBRTC_IOS) |
| 217 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, |
Patrik Höglund | 3428827 | 2020-03-19 07:51:35 | [diff] [blame] | 218 | absl::GetFlag(FLAGS_write_perf_output_on_ios), |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 219 | metrics_to_plot); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 220 | rtc::test::RunTestsFromIOSApp(); |
Yves Gerey | 071d025 | 2020-01-28 19:07:19 | [diff] [blame] | 221 | int exit_code = 0; |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 222 | #else |
| 223 | int exit_code = RUN_ALL_TESTS(); |
| 224 | |
Patrik Höglund | a7a0173 | 2020-03-27 18:22:36 | [diff] [blame] | 225 | std::string perf_output_file = |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 226 | absl::GetFlag(FLAGS_isolated_script_test_perf_output); |
Patrik Höglund | a7a0173 | 2020-03-27 18:22:36 | [diff] [blame] | 227 | if (!perf_output_file.empty()) { |
| 228 | if (!webrtc::test::WritePerfResults(perf_output_file)) { |
Patrik Höglund | b8e69ef | 2020-03-12 08:39:40 | [diff] [blame] | 229 | return 1; |
| 230 | } |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 231 | } |
Artem Titov | 694b74b | 2020-02-20 22:52:26 | [diff] [blame] | 232 | if (metrics_to_plot) { |
| 233 | webrtc::test::PrintPlottableResults(*metrics_to_plot); |
Artem Titov | 087be5c | 2019-09-12 18:30:54 | [diff] [blame] | 234 | } |
Yves Gerey | 071d025 | 2020-01-28 19:07:19 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | if (capture_events) { |
| 238 | rtc::tracing::StopInternalCapture(); |
| 239 | } |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 240 | |
Yves Gerey | 53347b7 | 2018-10-19 13:04:04 | [diff] [blame] | 241 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 242 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 243 | defined(UNDEFINED_SANITIZER) |
| 244 | // We want the test flagged as failed only for sanitizer defects, |
| 245 | // in which case the sanitizer will override exit code with 66. |
Yves Gerey | 071d025 | 2020-01-28 19:07:19 | [diff] [blame] | 246 | exit_code = 0; |
Yves Gerey | 53347b7 | 2018-10-19 13:04:04 | [diff] [blame] | 247 | #endif |
| 248 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 249 | return exit_code; |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | ~TestMainImpl() override = default; |
| 253 | |
| 254 | private: |
| 255 | #if defined(WEBRTC_WIN) |
| 256 | std::unique_ptr<rtc::WinsockInitializer> winsock_init_; |
| 257 | #endif |
| 258 | }; |
| 259 | |
| 260 | } // namespace |
| 261 | |
| 262 | std::unique_ptr<TestMain> TestMain::Create() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 263 | return std::make_unique<TestMainImpl>(); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | } // namespace webrtc |