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/unit_test_launcher.h" | 5 #include "base/test/unit_test_launcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/sys_info.h" |
17 #include "base/test/gtest_xml_util.h" | 19 #include "base/test/gtest_xml_util.h" |
18 #include "base/test/parallel_test_launcher.h" | 20 #include "base/test/parallel_test_launcher.h" |
19 #include "base/test/test_launcher.h" | 21 #include "base/test/test_launcher.h" |
20 #include "base/test/test_switches.h" | 22 #include "base/test/test_switches.h" |
21 #include "base/test/test_timeouts.h" | 23 #include "base/test/test_timeouts.h" |
22 #include "base/threading/thread_checker.h" | 24 #include "base/threading/thread_checker.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 namespace base { | 27 namespace base { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 // This constant controls how many tests are run in a single batch. | 31 // This constant controls how many tests are run in a single batch by default. |
30 const size_t kTestBatchLimit = 10; | 32 const size_t kDefaultTestBatchLimit = 10; |
31 | 33 |
32 // Flag to enable the new launcher logic. | 34 // Flag to enable the new launcher logic. |
33 // TODO(phajdan.jr): Remove it, http://crbug.com/236893 . | 35 // TODO(phajdan.jr): Remove it, http://crbug.com/236893 . |
34 const char kBraveNewTestLauncherFlag[] = "brave-new-test-launcher"; | 36 const char kBraveNewTestLauncherFlag[] = "brave-new-test-launcher"; |
35 | 37 |
36 // Flag to run all tests in a single process. | 38 // Flag to run all tests in a single process. |
37 const char kSingleProcessTestsFlag[] = "single-process-tests"; | 39 const char kSingleProcessTestsFlag[] = "single-process-tests"; |
38 | 40 |
39 // Returns command line for child GTest process based on the command line | 41 // Returns command line for child GTest process based on the command line |
40 // of current process. |test_names| is a vector of test full names | 42 // of current process. |test_names| is a vector of test full names |
41 // (e.g. "A.B"), |output_file| is path to the GTest XML output file. | 43 // (e.g. "A.B"), |output_file| is path to the GTest XML output file. |
42 CommandLine GetCommandLineForChildGTestProcess( | 44 CommandLine GetCommandLineForChildGTestProcess( |
43 const std::vector<std::string>& test_names, | 45 const std::vector<std::string>& test_names, |
44 const base::FilePath& output_file) { | 46 const base::FilePath& output_file) { |
45 CommandLine new_cmd_line(*CommandLine::ForCurrentProcess()); | 47 CommandLine new_cmd_line(*CommandLine::ForCurrentProcess()); |
46 | 48 |
47 new_cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); | 49 new_cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); |
48 new_cmd_line.AppendSwitchASCII(kGTestFilterFlag, JoinString(test_names, ":")); | 50 new_cmd_line.AppendSwitchASCII(kGTestFilterFlag, JoinString(test_names, ":")); |
49 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); | 51 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); |
50 new_cmd_line.AppendSwitch(kBraveNewTestLauncherFlag); | 52 new_cmd_line.AppendSwitch(kBraveNewTestLauncherFlag); |
51 | 53 |
52 return new_cmd_line; | 54 return new_cmd_line; |
53 } | 55 } |
54 | 56 |
55 class UnitTestLauncherDelegate : public TestLauncherDelegate { | 57 class UnitTestLauncherDelegate : public TestLauncherDelegate { |
56 public: | 58 public: |
57 explicit UnitTestLauncherDelegate(size_t jobs) : parallel_launcher_(jobs) { | 59 UnitTestLauncherDelegate(size_t jobs, size_t batch_limit) |
| 60 : parallel_launcher_(jobs), |
| 61 batch_limit_(batch_limit) { |
58 } | 62 } |
59 | 63 |
60 virtual ~UnitTestLauncherDelegate() { | 64 virtual ~UnitTestLauncherDelegate() { |
61 DCHECK(thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
62 } | 66 } |
63 | 67 |
64 private: | 68 private: |
65 struct TestLaunchInfo { | 69 struct TestLaunchInfo { |
66 std::string GetFullName() const { | 70 std::string GetFullName() const { |
67 return test_case_name + "." + test_name; | 71 return test_case_name + "." + test_name; |
(...skipping 17 matching lines...) Expand all Loading... |
85 const TestResultCallback& callback) OVERRIDE { | 89 const TestResultCallback& callback) OVERRIDE { |
86 DCHECK(thread_checker_.CalledOnValidThread()); | 90 DCHECK(thread_checker_.CalledOnValidThread()); |
87 | 91 |
88 TestLaunchInfo launch_info; | 92 TestLaunchInfo launch_info; |
89 launch_info.test_case_name = test_case->name(); | 93 launch_info.test_case_name = test_case->name(); |
90 launch_info.test_name = test_info->name(); | 94 launch_info.test_name = test_info->name(); |
91 launch_info.callback = callback; | 95 launch_info.callback = callback; |
92 tests_.push_back(launch_info); | 96 tests_.push_back(launch_info); |
93 | 97 |
94 // Run tests in batches no larger than the limit. | 98 // Run tests in batches no larger than the limit. |
95 if (tests_.size() >= kTestBatchLimit) | 99 if (tests_.size() >= batch_limit_) |
96 RunRemainingTests(); | 100 RunRemainingTests(); |
97 } | 101 } |
98 | 102 |
99 virtual void RunRemainingTests() OVERRIDE { | 103 virtual void RunRemainingTests() OVERRIDE { |
100 DCHECK(thread_checker_.CalledOnValidThread()); | 104 DCHECK(thread_checker_.CalledOnValidThread()); |
101 | 105 |
102 if (tests_.empty()) | 106 if (tests_.empty()) |
103 return; | 107 return; |
104 | 108 |
105 // Create a dedicated temporary directory to store the xml result data | 109 // Create a dedicated temporary directory to store the xml result data |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 tests[i].callback.Run(test_result); | 277 tests[i].callback.Run(test_result); |
274 called_any_callback = true; | 278 called_any_callback = true; |
275 } | 279 } |
276 } | 280 } |
277 | 281 |
278 return called_any_callback; | 282 return called_any_callback; |
279 } | 283 } |
280 | 284 |
281 ParallelTestLauncher parallel_launcher_; | 285 ParallelTestLauncher parallel_launcher_; |
282 | 286 |
| 287 // Maximum number of tests to run in a single batch. |
| 288 size_t batch_limit_; |
| 289 |
283 std::vector<TestLaunchInfo> tests_; | 290 std::vector<TestLaunchInfo> tests_; |
284 | 291 |
285 ThreadChecker thread_checker_; | 292 ThreadChecker thread_checker_; |
286 }; | 293 }; |
287 | 294 |
| 295 bool GetSwitchValueAsInt(const std::string& switch_name, int* result) { |
| 296 if (!CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) |
| 297 return true; |
| 298 |
| 299 std::string switch_value = |
| 300 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name); |
| 301 if (!StringToInt(switch_value, result) || *result < 1) { |
| 302 LOG(ERROR) << "Invalid value for " << switch_name << ": " << switch_value; |
| 303 return false; |
| 304 } |
| 305 |
| 306 return true; |
| 307 } |
| 308 |
288 } // namespace | 309 } // namespace |
289 | 310 |
290 int LaunchUnitTests(int argc, | 311 int LaunchUnitTests(int argc, |
291 char** argv, | 312 char** argv, |
292 const RunTestSuiteCallback& run_test_suite) { | 313 const RunTestSuiteCallback& run_test_suite) { |
293 CommandLine::Init(argc, argv); | 314 CommandLine::Init(argc, argv); |
294 if (CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcessTestsFlag) || | 315 if (CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcessTestsFlag) || |
295 !CommandLine::ForCurrentProcess()->HasSwitch(kBraveNewTestLauncherFlag)) { | 316 !CommandLine::ForCurrentProcess()->HasSwitch(kBraveNewTestLauncherFlag)) { |
296 return run_test_suite.Run(); | 317 return run_test_suite.Run(); |
297 } | 318 } |
298 | 319 |
299 base::TimeTicks start_time(base::TimeTicks::Now()); | 320 base::TimeTicks start_time(base::TimeTicks::Now()); |
300 | 321 |
301 fprintf(stdout, | |
302 "Starting tests...\n" | |
303 "IMPORTANT DEBUGGING NOTE: batches of tests are run inside their own \n" | |
304 "process. For debugging a test inside a debugger, use the\n" | |
305 "--gtest_filter=<your_test_name> flag along with \n" | |
306 "--single-process-tests.\n"); | |
307 fflush(stdout); | |
308 | |
309 testing::InitGoogleTest(&argc, argv); | 322 testing::InitGoogleTest(&argc, argv); |
310 TestTimeouts::Initialize(); | 323 TestTimeouts::Initialize(); |
311 | 324 |
| 325 int jobs = SysInfo::NumberOfProcessors(); |
| 326 if (!GetSwitchValueAsInt(switches::kTestLauncherJobs, &jobs)) |
| 327 return 1; |
| 328 |
| 329 int batch_limit = kDefaultTestBatchLimit; |
| 330 if (!GetSwitchValueAsInt(switches::kTestLauncherBatchLimit, &batch_limit)) |
| 331 return 1; |
| 332 |
| 333 fprintf(stdout, |
| 334 "Starting tests (using %d parallel jobs)...\n" |
| 335 "IMPORTANT DEBUGGING NOTE: batches of tests are run inside their\n" |
| 336 "own process. For debugging a test inside a debugger, use the\n" |
| 337 "--gtest_filter=<your_test_name> flag along with\n" |
| 338 "--single-process-tests.\n", jobs); |
| 339 fflush(stdout); |
| 340 |
312 MessageLoop message_loop; | 341 MessageLoop message_loop; |
313 | 342 |
314 // TODO(phajdan.jr): Provide an option to adjust jobs, default to number | 343 base::UnitTestLauncherDelegate delegate(jobs, batch_limit); |
315 // of CPU cores. | |
316 base::UnitTestLauncherDelegate delegate(4); | |
317 int exit_code = base::LaunchTests(&delegate, argc, argv); | 344 int exit_code = base::LaunchTests(&delegate, argc, argv); |
318 | 345 |
319 fprintf(stdout, | 346 fprintf(stdout, |
320 "Tests took %" PRId64 " seconds.\n", | 347 "Tests took %" PRId64 " seconds.\n", |
321 (base::TimeTicks::Now() - start_time).InSeconds()); | 348 (base::TimeTicks::Now() - start_time).InSeconds()); |
322 fflush(stdout); | 349 fflush(stdout); |
323 | 350 |
324 return exit_code; | 351 return exit_code; |
325 } | 352 } |
326 | 353 |
327 } // namespace base | 354 } // namespace base |
OLD | NEW |