| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/launcher/test_launcher.h" | 5 #include "base/test/launcher/test_launcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/environment.h" | 12 #include "base/environment.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
| 17 #include "base/hash.h" | 17 #include "base/hash.h" |
| 18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 #include "base/macros.h" | 21 #include "base/macros.h" |
| 22 #include "base/memory/ptr_util.h" |
| 22 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
| 23 #include "base/process/kill.h" | 24 #include "base/process/kill.h" |
| 24 #include "base/process/launch.h" | 25 #include "base/process/launch.h" |
| 25 #include "base/run_loop.h" | 26 #include "base/run_loop.h" |
| 26 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
| 27 #include "base/strings/pattern.h" | 28 #include "base/strings/pattern.h" |
| 28 #include "base/strings/string_number_conversions.h" | 29 #include "base/strings/string_number_conversions.h" |
| 29 #include "base/strings/string_split.h" | 30 #include "base/strings/string_split.h" |
| 30 #include "base/strings/string_util.h" | 31 #include "base/strings/string_util.h" |
| 31 #include "base/strings/stringize_macros.h" | 32 #include "base/strings/stringize_macros.h" |
| 32 #include "base/strings/stringprintf.h" | 33 #include "base/strings/stringprintf.h" |
| 33 #include "base/strings/utf_string_conversions.h" | 34 #include "base/strings/utf_string_conversions.h" |
| 34 #include "base/test/gtest_util.h" | 35 #include "base/test/gtest_util.h" |
| 35 #include "base/test/launcher/test_launcher_tracer.h" | 36 #include "base/test/launcher/test_launcher_tracer.h" |
| 36 #include "base/test/launcher/test_results_tracker.h" | 37 #include "base/test/launcher/test_results_tracker.h" |
| 37 #include "base/test/sequenced_worker_pool_owner.h" | 38 #include "base/test/sequenced_worker_pool_owner.h" |
| 38 #include "base/test/test_switches.h" | 39 #include "base/test/test_switches.h" |
| 39 #include "base/test/test_timeouts.h" | 40 #include "base/test/test_timeouts.h" |
| 41 #include "base/threading/thread.h" |
| 40 #include "base/threading/thread_checker.h" | 42 #include "base/threading/thread_checker.h" |
| 41 #include "base/threading/thread_task_runner_handle.h" | 43 #include "base/threading/thread_task_runner_handle.h" |
| 42 #include "base/time/time.h" | 44 #include "base/time/time.h" |
| 43 #include "build/build_config.h" | 45 #include "build/build_config.h" |
| 44 #include "testing/gtest/include/gtest/gtest.h" | 46 #include "testing/gtest/include/gtest/gtest.h" |
| 45 | 47 |
| 46 #if defined(OS_POSIX) | 48 #if defined(OS_POSIX) |
| 47 #include <fcntl.h> | 49 #include <fcntl.h> |
| 48 #endif | 50 #endif |
| 49 | 51 |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 | 568 |
| 567 // Record the exact command line used to launch the child. | 569 // Record the exact command line used to launch the child. |
| 568 CommandLine new_command_line( | 570 CommandLine new_command_line( |
| 569 PrepareCommandLineForGTest(command_line, wrapper)); | 571 PrepareCommandLineForGTest(command_line, wrapper)); |
| 570 | 572 |
| 571 // When running in parallel mode we need to redirect stdio to avoid mixed-up | 573 // When running in parallel mode we need to redirect stdio to avoid mixed-up |
| 572 // output. We also always redirect on the bots to get the test output into | 574 // output. We also always redirect on the bots to get the test output into |
| 573 // JSON summary. | 575 // JSON summary. |
| 574 bool redirect_stdio = (parallel_jobs_ > 1) || BotModeEnabled(); | 576 bool redirect_stdio = (parallel_jobs_ > 1) || BotModeEnabled(); |
| 575 | 577 |
| 576 worker_pool_owner_->pool()->PostWorkerTask( | 578 GetTaskRunner()->PostTask( |
| 577 FROM_HERE, | 579 FROM_HERE, |
| 578 Bind(&DoLaunchChildTestProcess, new_command_line, timeout, options, | 580 Bind(&DoLaunchChildTestProcess, new_command_line, timeout, options, |
| 579 redirect_stdio, RetainedRef(ThreadTaskRunnerHandle::Get()), | 581 redirect_stdio, RetainedRef(ThreadTaskRunnerHandle::Get()), |
| 580 Bind(&TestLauncher::OnLaunchTestProcessFinished, Unretained(this), | 582 Bind(&TestLauncher::OnLaunchTestProcessFinished, Unretained(this), |
| 581 completed_callback), | 583 completed_callback), |
| 582 launched_callback)); | 584 launched_callback)); |
| 583 } | 585 } |
| 584 | 586 |
| 585 void TestLauncher::OnTestFinished(const TestResult& result) { | 587 void TestLauncher::OnTestFinished(const TestResult& result) { |
| 586 ++test_finished_count_; | 588 ++test_finished_count_; |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 } else if (!command_line->HasSwitch(kGTestFilterFlag) || BotModeEnabled()) { | 796 } else if (!command_line->HasSwitch(kGTestFilterFlag) || BotModeEnabled()) { |
| 795 // Retry failures 3 times by default if we are running all of the tests or | 797 // Retry failures 3 times by default if we are running all of the tests or |
| 796 // in bot mode. | 798 // in bot mode. |
| 797 retry_limit_ = 3; | 799 retry_limit_ = 3; |
| 798 } | 800 } |
| 799 | 801 |
| 800 if (command_line->HasSwitch(switches::kTestLauncherForceRunBrokenTests)) | 802 if (command_line->HasSwitch(switches::kTestLauncherForceRunBrokenTests)) |
| 801 force_run_broken_tests_ = true; | 803 force_run_broken_tests_ = true; |
| 802 | 804 |
| 803 if (command_line->HasSwitch(switches::kTestLauncherJobs)) { | 805 if (command_line->HasSwitch(switches::kTestLauncherJobs)) { |
| 804 int jobs = -1; | 806 size_t jobs = 0U; |
| 805 if (!StringToInt(command_line->GetSwitchValueASCII( | 807 if (!StringToSizeT(command_line->GetSwitchValueASCII( |
| 806 switches::kTestLauncherJobs), &jobs) || | 808 switches::kTestLauncherJobs), &jobs) || |
| 807 jobs < 0) { | 809 !jobs) { |
| 808 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherJobs; | 810 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherJobs; |
| 809 return false; | 811 return false; |
| 810 } | 812 } |
| 811 | 813 |
| 812 parallel_jobs_ = jobs; | 814 parallel_jobs_ = jobs; |
| 813 } else if (command_line->HasSwitch(kGTestFilterFlag) && !BotModeEnabled()) { | 815 } else if (command_line->HasSwitch(kGTestFilterFlag) && !BotModeEnabled()) { |
| 814 // Do not run jobs in parallel by default if we are running a subset of | 816 // Do not run jobs in parallel by default if we are running a subset of |
| 815 // the tests and if bot mode is off. | 817 // the tests and if bot mode is off. |
| 816 parallel_jobs_ = 1; | 818 parallel_jobs_ = 1U; |
| 817 } | 819 } |
| 818 | 820 |
| 819 fprintf(stdout, "Using %" PRIuS " parallel jobs.\n", parallel_jobs_); | 821 fprintf(stdout, "Using %" PRIuS " parallel jobs.\n", parallel_jobs_); |
| 820 fflush(stdout); | 822 fflush(stdout); |
| 821 worker_pool_owner_.reset( | 823 if (parallel_jobs_ > 1U) { |
| 822 new SequencedWorkerPoolOwner(parallel_jobs_, "test_launcher")); | 824 worker_pool_owner_ = MakeUnique<SequencedWorkerPoolOwner>( |
| 825 parallel_jobs_, "test_launcher"); |
| 826 } else { |
| 827 worker_thread_ = MakeUnique<Thread>("test_launcher"); |
| 828 worker_thread_->Start(); |
| 829 } |
| 823 | 830 |
| 824 if (command_line->HasSwitch(switches::kTestLauncherFilterFile) && | 831 if (command_line->HasSwitch(switches::kTestLauncherFilterFile) && |
| 825 command_line->HasSwitch(kGTestFilterFlag)) { | 832 command_line->HasSwitch(kGTestFilterFlag)) { |
| 826 LOG(ERROR) << "Only one of --test-launcher-filter-file and --gtest_filter " | 833 LOG(ERROR) << "Only one of --test-launcher-filter-file and --gtest_filter " |
| 827 << "at a time is allowed."; | 834 << "at a time is allowed."; |
| 828 return false; | 835 return false; |
| 829 } | 836 } |
| 830 | 837 |
| 831 if (command_line->HasSwitch(switches::kTestLauncherFilterFile)) { | 838 if (command_line->HasSwitch(switches::kTestLauncherFilterFile)) { |
| 832 base::FilePath filter_file_path = base::MakeAbsoluteFilePath( | 839 base::FilePath filter_file_path = base::MakeAbsoluteFilePath( |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1106 fprintf(stdout, "\t%s\n", i->second.GetCommandLineString().c_str()); | 1113 fprintf(stdout, "\t%s\n", i->second.GetCommandLineString().c_str()); |
| 1107 #endif | 1114 #endif |
| 1108 } | 1115 } |
| 1109 | 1116 |
| 1110 fflush(stdout); | 1117 fflush(stdout); |
| 1111 | 1118 |
| 1112 // Arm the timer again - otherwise it would fire only once. | 1119 // Arm the timer again - otherwise it would fire only once. |
| 1113 watchdog_timer_.Reset(); | 1120 watchdog_timer_.Reset(); |
| 1114 } | 1121 } |
| 1115 | 1122 |
| 1123 scoped_refptr<TaskRunner> TestLauncher::GetTaskRunner() { |
| 1124 // One and only one of |worker_pool_owner_| or |worker_thread_| should be |
| 1125 // ready. |
| 1126 DCHECK_NE(!!worker_pool_owner_, !!worker_thread_); |
| 1127 |
| 1128 if (worker_pool_owner_) |
| 1129 return worker_pool_owner_->pool(); |
| 1130 DCHECK(worker_thread_->IsRunning()); |
| 1131 return worker_thread_->task_runner(); |
| 1132 } |
| 1133 |
| 1116 std::string GetTestOutputSnippet(const TestResult& result, | 1134 std::string GetTestOutputSnippet(const TestResult& result, |
| 1117 const std::string& full_output) { | 1135 const std::string& full_output) { |
| 1118 size_t run_pos = full_output.find(std::string("[ RUN ] ") + | 1136 size_t run_pos = full_output.find(std::string("[ RUN ] ") + |
| 1119 result.full_name); | 1137 result.full_name); |
| 1120 if (run_pos == std::string::npos) | 1138 if (run_pos == std::string::npos) |
| 1121 return std::string(); | 1139 return std::string(); |
| 1122 | 1140 |
| 1123 size_t end_pos = full_output.find(std::string("[ FAILED ] ") + | 1141 size_t end_pos = full_output.find(std::string("[ FAILED ] ") + |
| 1124 result.full_name, | 1142 result.full_name, |
| 1125 run_pos); | 1143 run_pos); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1138 } | 1156 } |
| 1139 | 1157 |
| 1140 std::string snippet(full_output.substr(run_pos)); | 1158 std::string snippet(full_output.substr(run_pos)); |
| 1141 if (end_pos != std::string::npos) | 1159 if (end_pos != std::string::npos) |
| 1142 snippet = full_output.substr(run_pos, end_pos - run_pos); | 1160 snippet = full_output.substr(run_pos, end_pos - run_pos); |
| 1143 | 1161 |
| 1144 return snippet; | 1162 return snippet; |
| 1145 } | 1163 } |
| 1146 | 1164 |
| 1147 } // namespace base | 1165 } // namespace base |
| OLD | NEW |