| 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/unit_test_launcher.h" | 5 #include "base/test/launcher/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" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 95 |
| 96 new_cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); | 96 new_cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); |
| 97 new_cmd_line.AppendSwitchASCII(kGTestFilterFlag, JoinString(test_names, ":")); | 97 new_cmd_line.AppendSwitchASCII(kGTestFilterFlag, JoinString(test_names, ":")); |
| 98 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); | 98 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); |
| 99 | 99 |
| 100 return new_cmd_line; | 100 return new_cmd_line; |
| 101 } | 101 } |
| 102 | 102 |
| 103 class UnitTestLauncherDelegate : public TestLauncherDelegate { | 103 class UnitTestLauncherDelegate : public TestLauncherDelegate { |
| 104 public: | 104 public: |
| 105 explicit UnitTestLauncherDelegate(size_t batch_limit, bool use_job_objects) | 105 explicit UnitTestLauncherDelegate(size_t batch_limit) |
| 106 : batch_limit_(batch_limit), | 106 : batch_limit_(batch_limit) { |
| 107 use_job_objects_(use_job_objects) { | |
| 108 } | 107 } |
| 109 | 108 |
| 110 virtual ~UnitTestLauncherDelegate() { | 109 virtual ~UnitTestLauncherDelegate() { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 } | 111 } |
| 113 | 112 |
| 114 private: | 113 private: |
| 115 struct GTestCallbackState { | 114 struct GTestCallbackState { |
| 116 TestLauncher* test_launcher; | 115 TestLauncher* test_launcher; |
| 117 std::vector<std::string> test_names; | 116 std::vector<std::string> test_names; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 179 |
| 181 GTestCallbackState callback_state; | 180 GTestCallbackState callback_state; |
| 182 callback_state.test_launcher = test_launcher; | 181 callback_state.test_launcher = test_launcher; |
| 183 callback_state.test_names = current_test_names; | 182 callback_state.test_names = current_test_names; |
| 184 callback_state.output_file = output_file; | 183 callback_state.output_file = output_file; |
| 185 | 184 |
| 186 test_launcher->LaunchChildGTestProcess( | 185 test_launcher->LaunchChildGTestProcess( |
| 187 cmd_line, | 186 cmd_line, |
| 188 std::string(), | 187 std::string(), |
| 189 TestTimeouts::test_launcher_timeout(), | 188 TestTimeouts::test_launcher_timeout(), |
| 190 use_job_objects_, | |
| 191 Bind(&UnitTestLauncherDelegate::SerialGTestCallback, | 189 Bind(&UnitTestLauncherDelegate::SerialGTestCallback, |
| 192 Unretained(this), | 190 Unretained(this), |
| 193 callback_state, | 191 callback_state, |
| 194 new_test_names)); | 192 new_test_names)); |
| 195 } | 193 } |
| 196 | 194 |
| 197 void RunBatch(TestLauncher* test_launcher, | 195 void RunBatch(TestLauncher* test_launcher, |
| 198 const std::vector<std::string>& test_names) { | 196 const std::vector<std::string>& test_names) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 | 198 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 222 | 220 |
| 223 GTestCallbackState callback_state; | 221 GTestCallbackState callback_state; |
| 224 callback_state.test_launcher = test_launcher; | 222 callback_state.test_launcher = test_launcher; |
| 225 callback_state.test_names = test_names; | 223 callback_state.test_names = test_names; |
| 226 callback_state.output_file = output_file; | 224 callback_state.output_file = output_file; |
| 227 | 225 |
| 228 test_launcher->LaunchChildGTestProcess( | 226 test_launcher->LaunchChildGTestProcess( |
| 229 cmd_line, | 227 cmd_line, |
| 230 std::string(), | 228 std::string(), |
| 231 timeout, | 229 timeout, |
| 232 use_job_objects_, | |
| 233 Bind(&UnitTestLauncherDelegate::GTestCallback, | 230 Bind(&UnitTestLauncherDelegate::GTestCallback, |
| 234 Unretained(this), | 231 Unretained(this), |
| 235 callback_state)); | 232 callback_state)); |
| 236 } | 233 } |
| 237 | 234 |
| 238 void GTestCallback(const GTestCallbackState& callback_state, | 235 void GTestCallback(const GTestCallbackState& callback_state, |
| 239 int exit_code, | 236 int exit_code, |
| 240 const TimeDelta& elapsed_time, | 237 const TimeDelta& elapsed_time, |
| 241 bool was_timeout, | 238 bool was_timeout, |
| 242 const std::string& output) { | 239 const std::string& output) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 } | 429 } |
| 433 } | 430 } |
| 434 | 431 |
| 435 return called_any_callback; | 432 return called_any_callback; |
| 436 } | 433 } |
| 437 | 434 |
| 438 ThreadChecker thread_checker_; | 435 ThreadChecker thread_checker_; |
| 439 | 436 |
| 440 // Maximum number of tests to run in a single batch. | 437 // Maximum number of tests to run in a single batch. |
| 441 size_t batch_limit_; | 438 size_t batch_limit_; |
| 442 | |
| 443 // Determines whether we use job objects on Windows. | |
| 444 bool use_job_objects_; | |
| 445 }; | 439 }; |
| 446 | 440 |
| 447 bool GetSwitchValueAsInt(const std::string& switch_name, int* result) { | 441 bool GetSwitchValueAsInt(const std::string& switch_name, int* result) { |
| 448 if (!CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) | 442 if (!CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) |
| 449 return true; | 443 return true; |
| 450 | 444 |
| 451 std::string switch_value = | 445 std::string switch_value = |
| 452 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name); | 446 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name); |
| 453 if (!StringToInt(switch_value, result) || *result < 1) { | 447 if (!StringToInt(switch_value, result) || *result < 1) { |
| 454 LOG(ERROR) << "Invalid value for " << switch_name << ": " << switch_value; | 448 LOG(ERROR) << "Invalid value for " << switch_name << ": " << switch_value; |
| 455 return false; | 449 return false; |
| 456 } | 450 } |
| 457 | 451 |
| 458 return true; | 452 return true; |
| 459 } | 453 } |
| 460 | 454 |
| 461 int LaunchUnitTestsInternal(const RunTestSuiteCallback& run_test_suite, | 455 int LaunchUnitTestsInternal(int argc, |
| 462 int default_jobs, | 456 char** argv, |
| 463 bool use_job_objects, | 457 const RunTestSuiteCallback& run_test_suite, |
| 464 const Closure& gtest_init) { | 458 int default_jobs) { |
| 459 CommandLine::Init(argc, argv); |
| 460 |
| 465 #if defined(OS_ANDROID) | 461 #if defined(OS_ANDROID) |
| 466 // We can't easily fork on Android, just run the test suite directly. | 462 // We can't easily fork on Android, just run the test suite directly. |
| 467 return run_test_suite.Run(); | 463 return run_test_suite.Run(); |
| 468 #else | 464 #else |
| 469 bool force_single_process = false; | 465 bool force_single_process = false; |
| 470 if (CommandLine::ForCurrentProcess()->HasSwitch( | 466 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 471 switches::kTestLauncherDebugLauncher)) { | 467 switches::kTestLauncherDebugLauncher)) { |
| 472 fprintf(stdout, "Forcing test launcher debugging mode.\n"); | 468 fprintf(stdout, "Forcing test launcher debugging mode.\n"); |
| 473 fflush(stdout); | 469 fflush(stdout); |
| 474 } else { | 470 } else { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 499 } | 495 } |
| 500 #endif | 496 #endif |
| 501 | 497 |
| 502 if (CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { | 498 if (CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { |
| 503 PrintUsage(); | 499 PrintUsage(); |
| 504 return 0; | 500 return 0; |
| 505 } | 501 } |
| 506 | 502 |
| 507 base::TimeTicks start_time(base::TimeTicks::Now()); | 503 base::TimeTicks start_time(base::TimeTicks::Now()); |
| 508 | 504 |
| 509 gtest_init.Run(); | 505 testing::InitGoogleTest(&argc, argv); |
| 510 TestTimeouts::Initialize(); | 506 TestTimeouts::Initialize(); |
| 511 | 507 |
| 512 int batch_limit = kDefaultTestBatchLimit; | 508 int batch_limit = kDefaultTestBatchLimit; |
| 513 if (!GetSwitchValueAsInt(switches::kTestLauncherBatchLimit, &batch_limit)) | 509 if (!GetSwitchValueAsInt(switches::kTestLauncherBatchLimit, &batch_limit)) |
| 514 return 1; | 510 return 1; |
| 515 | 511 |
| 516 fprintf(stdout, | 512 fprintf(stdout, |
| 517 "IMPORTANT DEBUGGING NOTE: batches of tests are run inside their\n" | 513 "IMPORTANT DEBUGGING NOTE: batches of tests are run inside their\n" |
| 518 "own process. For debugging a test inside a debugger, use the\n" | 514 "own process. For debugging a test inside a debugger, use the\n" |
| 519 "--gtest_filter=<your_test_name> flag along with\n" | 515 "--gtest_filter=<your_test_name> flag along with\n" |
| 520 "--single-process-tests.\n"); | 516 "--single-process-tests.\n"); |
| 521 fflush(stdout); | 517 fflush(stdout); |
| 522 | 518 |
| 523 MessageLoopForIO message_loop; | 519 MessageLoopForIO message_loop; |
| 524 | 520 |
| 525 UnitTestLauncherDelegate delegate(batch_limit, use_job_objects); | 521 UnitTestLauncherDelegate delegate(batch_limit); |
| 526 base::TestLauncher launcher(&delegate, default_jobs); | 522 base::TestLauncher launcher(&delegate, default_jobs); |
| 527 bool success = launcher.Run(); | 523 bool success = launcher.Run(argc, argv); |
| 528 | 524 |
| 529 fprintf(stdout, | 525 fprintf(stdout, |
| 530 "Tests took %" PRId64 " seconds.\n", | 526 "Tests took %" PRId64 " seconds.\n", |
| 531 (base::TimeTicks::Now() - start_time).InSeconds()); | 527 (base::TimeTicks::Now() - start_time).InSeconds()); |
| 532 fflush(stdout); | 528 fflush(stdout); |
| 533 | 529 |
| 534 return (success ? 0 : 1); | 530 return (success ? 0 : 1); |
| 535 } | 531 } |
| 536 | 532 |
| 537 void InitGoogleTestChar(int* argc, char** argv) { | |
| 538 testing::InitGoogleTest(argc, argv); | |
| 539 } | |
| 540 | |
| 541 #if defined(OS_WIN) | |
| 542 void InitGoogleTestWChar(int* argc, wchar_t** argv) { | |
| 543 testing::InitGoogleTest(argc, argv); | |
| 544 } | |
| 545 #endif // defined(OS_WIN) | |
| 546 | |
| 547 } // namespace | 533 } // namespace |
| 548 | 534 |
| 549 int LaunchUnitTests(int argc, | 535 int LaunchUnitTests(int argc, |
| 550 char** argv, | 536 char** argv, |
| 551 const RunTestSuiteCallback& run_test_suite) { | 537 const RunTestSuiteCallback& run_test_suite) { |
| 552 CommandLine::Init(argc, argv); | |
| 553 return LaunchUnitTestsInternal( | 538 return LaunchUnitTestsInternal( |
| 554 run_test_suite, | 539 argc, argv, run_test_suite, SysInfo::NumberOfProcessors()); |
| 555 SysInfo::NumberOfProcessors(), | |
| 556 true, | |
| 557 Bind(&InitGoogleTestChar, &argc, argv)); | |
| 558 } | 540 } |
| 559 | 541 |
| 560 int LaunchUnitTestsSerially(int argc, | 542 int LaunchUnitTestsSerially(int argc, |
| 561 char** argv, | 543 char** argv, |
| 562 const RunTestSuiteCallback& run_test_suite) { | 544 const RunTestSuiteCallback& run_test_suite) { |
| 563 CommandLine::Init(argc, argv); | 545 return LaunchUnitTestsInternal(argc, argv, run_test_suite, 1); |
| 564 return LaunchUnitTestsInternal( | |
| 565 run_test_suite, | |
| 566 1, | |
| 567 true, | |
| 568 Bind(&InitGoogleTestChar, &argc, argv)); | |
| 569 } | 546 } |
| 570 | 547 |
| 571 #if defined(OS_WIN) | |
| 572 int LaunchUnitTests(int argc, | |
| 573 wchar_t** argv, | |
| 574 bool use_job_objects, | |
| 575 const RunTestSuiteCallback& run_test_suite) { | |
| 576 // Windows CommandLine::Init ignores argv anyway. | |
| 577 CommandLine::Init(argc, NULL); | |
| 578 return LaunchUnitTestsInternal( | |
| 579 run_test_suite, | |
| 580 SysInfo::NumberOfProcessors(), | |
| 581 use_job_objects, | |
| 582 Bind(&InitGoogleTestWChar, &argc, argv)); | |
| 583 } | |
| 584 #endif // defined(OS_WIN) | |
| 585 | |
| 586 } // namespace base | 548 } // namespace base |
| OLD | NEW |