OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/test/test_launcher.h" | 5 #include "content/public/test/test_launcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 292 |
293 // Make it possible for selected tests to request a longer timeout. | 293 // Make it possible for selected tests to request a longer timeout. |
294 // Generally tests should really avoid doing too much, and splitting | 294 // Generally tests should really avoid doing too much, and splitting |
295 // a test instead of using SLOW prefix is strongly preferred. | 295 // a test instead of using SLOW prefix is strongly preferred. |
296 if (test_name.find("SLOW_") != std::string::npos) | 296 if (test_name.find("SLOW_") != std::string::npos) |
297 timeout *= kSlowTestTimeoutMultiplier; | 297 timeout *= kSlowTestTimeoutMultiplier; |
298 | 298 |
299 return timeout; | 299 return timeout; |
300 } | 300 } |
301 | 301 |
302 // Runs test specified by |test_name| in a child process, | 302 int RunTestInternal(const testing::TestCase* test_case, |
303 // and returns the exit code. | 303 const std::string& test_name, |
304 int RunTest(TestLauncherDelegate* launcher_delegate, | 304 CommandLine* command_line, |
305 const std::string& test_name, | 305 base::TimeDelta default_timeout, |
306 base::TimeDelta default_timeout, | 306 bool* was_timeout) { |
307 bool* was_timeout) { | 307 if (test_case) { |
308 if (was_timeout) | 308 std::string pre_test_name = test_name; |
309 *was_timeout = false; | 309 ReplaceFirstSubstringAfterOffset(&pre_test_name, 0, ".", ".PRE_"); |
| 310 for (int i = 0; i < test_case->total_test_count(); ++i) { |
| 311 const testing::TestInfo* test_info = test_case->GetTestInfo(i); |
| 312 std::string cur_test_name = test_info->test_case_name(); |
| 313 cur_test_name.append("."); |
| 314 cur_test_name.append(test_info->name()); |
| 315 if (cur_test_name == pre_test_name) { |
| 316 int exit_code = RunTestInternal(test_case, pre_test_name, command_line, |
| 317 default_timeout, was_timeout); |
| 318 if (exit_code != 0) |
| 319 return exit_code; |
| 320 } |
| 321 } |
| 322 } |
310 | 323 |
311 #if defined(OS_MACOSX) | 324 CommandLine new_cmd_line(*command_line); |
312 // Some of the below method calls will leak objects if there is no | |
313 // autorelease pool in place. | |
314 base::mac::ScopedNSAutoreleasePool pool; | |
315 #endif | |
316 | |
317 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
318 CommandLine new_cmd_line(cmd_line->GetProgram()); | |
319 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); | |
320 | |
321 // Strip out gtest_output flag because otherwise we would overwrite results | |
322 // of the previous test. We will generate the final output file later | |
323 // in RunTests(). | |
324 switches.erase(kGTestOutputFlag); | |
325 | |
326 // Strip out gtest_repeat flag because we can only run one test in the child | |
327 // process (restarting the browser in the same process is illegal after it | |
328 // has been shut down and will actually crash). | |
329 switches.erase(kGTestRepeatFlag); | |
330 | |
331 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); | |
332 iter != switches.end(); ++iter) { | |
333 new_cmd_line.AppendSwitchNative((*iter).first, (*iter).second); | |
334 } | |
335 | 325 |
336 // Always enable disabled tests. This method is not called with disabled | 326 // Always enable disabled tests. This method is not called with disabled |
337 // tests unless this flag was specified to the browser test executable. | 327 // tests unless this flag was specified to the browser test executable. |
338 new_cmd_line.AppendSwitch("gtest_also_run_disabled_tests"); | 328 new_cmd_line.AppendSwitch("gtest_also_run_disabled_tests"); |
339 new_cmd_line.AppendSwitchASCII("gtest_filter", test_name); | 329 new_cmd_line.AppendSwitchASCII("gtest_filter", test_name); |
340 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); | 330 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); |
341 | 331 |
342 // Do not let the child ignore failures. We need to propagate the | |
343 // failure status back to the parent. | |
344 new_cmd_line.AppendSwitch(base::TestSuite::kStrictFailureHandling); | |
345 | |
346 if (!launcher_delegate->AdjustChildProcessCommandLine(&new_cmd_line)) | |
347 return -1; | |
348 | |
349 const char* browser_wrapper = getenv("BROWSER_WRAPPER"); | 332 const char* browser_wrapper = getenv("BROWSER_WRAPPER"); |
350 if (browser_wrapper) { | 333 if (browser_wrapper) { |
351 #if defined(OS_WIN) | 334 #if defined(OS_WIN) |
352 new_cmd_line.PrependWrapper(ASCIIToWide(browser_wrapper)); | 335 new_cmd_line.PrependWrapper(ASCIIToWide(browser_wrapper)); |
353 #elif defined(OS_POSIX) | 336 #elif defined(OS_POSIX) |
354 new_cmd_line.PrependWrapper(browser_wrapper); | 337 new_cmd_line.PrependWrapper(browser_wrapper); |
355 #endif | 338 #endif |
356 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with " | 339 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with " |
357 << browser_wrapper; | 340 << browser_wrapper; |
358 } | 341 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 // cleaned up using JobObjects. | 378 // cleaned up using JobObjects. |
396 base::KillProcessGroup(process_handle); | 379 base::KillProcessGroup(process_handle); |
397 } | 380 } |
398 #endif | 381 #endif |
399 | 382 |
400 base::CloseProcessHandle(process_handle); | 383 base::CloseProcessHandle(process_handle); |
401 | 384 |
402 return exit_code; | 385 return exit_code; |
403 } | 386 } |
404 | 387 |
| 388 // Runs test specified by |test_name| in a child process, |
| 389 // and returns the exit code. |
| 390 int RunTest(TestLauncherDelegate* launcher_delegate, |
| 391 const testing::TestCase* test_case, |
| 392 const std::string& test_name, |
| 393 base::TimeDelta default_timeout, |
| 394 bool* was_timeout) { |
| 395 if (was_timeout) |
| 396 *was_timeout = false; |
| 397 |
| 398 #if defined(OS_MACOSX) |
| 399 // Some of the below method calls will leak objects if there is no |
| 400 // autorelease pool in place. |
| 401 base::mac::ScopedNSAutoreleasePool pool; |
| 402 #endif |
| 403 |
| 404 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 405 CommandLine new_cmd_line(cmd_line->GetProgram()); |
| 406 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); |
| 407 |
| 408 // Strip out gtest_output flag because otherwise we would overwrite results |
| 409 // of the previous test. We will generate the final output file later |
| 410 // in RunTests(). |
| 411 switches.erase(kGTestOutputFlag); |
| 412 |
| 413 // Strip out gtest_repeat flag because we can only run one test in the child |
| 414 // process (restarting the browser in the same process is illegal after it |
| 415 // has been shut down and will actually crash). |
| 416 switches.erase(kGTestRepeatFlag); |
| 417 |
| 418 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); |
| 419 iter != switches.end(); ++iter) { |
| 420 new_cmd_line.AppendSwitchNative((*iter).first, (*iter).second); |
| 421 } |
| 422 |
| 423 // Do not let the child ignore failures. We need to propagate the |
| 424 // failure status back to the parent. |
| 425 new_cmd_line.AppendSwitch(base::TestSuite::kStrictFailureHandling); |
| 426 |
| 427 if (!launcher_delegate->AdjustChildProcessCommandLine(&new_cmd_line)) |
| 428 return -1; |
| 429 |
| 430 return RunTestInternal( |
| 431 test_case, test_name, &new_cmd_line, default_timeout, was_timeout); |
| 432 } |
| 433 |
405 bool RunTests(TestLauncherDelegate* launcher_delegate, | 434 bool RunTests(TestLauncherDelegate* launcher_delegate, |
406 bool should_shard, | 435 bool should_shard, |
407 int total_shards, | 436 int total_shards, |
408 int shard_index) { | 437 int shard_index) { |
409 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 438 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
410 | 439 |
411 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); | 440 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); |
412 | 441 |
413 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); | 442 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
414 | 443 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 continue; | 476 continue; |
448 | 477 |
449 // Skip disabled tests. | 478 // Skip disabled tests. |
450 if (test_name.find("DISABLED") != std::string::npos && | 479 if (test_name.find("DISABLED") != std::string::npos && |
451 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | 480 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { |
452 printer.OnTestEnd(test_info->name(), test_case->name(), | 481 printer.OnTestEnd(test_info->name(), test_case->name(), |
453 false, false, false, 0); | 482 false, false, false, 0); |
454 continue; | 483 continue; |
455 } | 484 } |
456 | 485 |
| 486 if (StartsWithASCII(test_info->name(), "PRE_", true)) |
| 487 continue; |
| 488 |
457 // Skip the test that doesn't match the filter string (if given). | 489 // Skip the test that doesn't match the filter string (if given). |
458 if ((!positive_filter.empty() && | 490 if ((!positive_filter.empty() && |
459 !MatchesFilter(test_name, positive_filter)) || | 491 !MatchesFilter(test_name, positive_filter)) || |
460 MatchesFilter(test_name, negative_filter)) { | 492 MatchesFilter(test_name, negative_filter)) { |
461 printer.OnTestEnd(test_info->name(), test_case->name(), | 493 printer.OnTestEnd(test_info->name(), test_case->name(), |
462 false, false, false, 0); | 494 false, false, false, 0); |
463 continue; | 495 continue; |
464 } | 496 } |
465 | 497 |
466 // Decide if this test should be run. | 498 // Decide if this test should be run. |
467 bool should_run = true; | 499 bool should_run = true; |
468 if (should_shard) { | 500 if (should_shard) { |
469 should_run = ShouldRunTestOnShard(total_shards, shard_index, | 501 should_run = ShouldRunTestOnShard(total_shards, shard_index, |
470 num_runnable_tests); | 502 num_runnable_tests); |
471 } | 503 } |
472 num_runnable_tests += 1; | 504 num_runnable_tests += 1; |
473 // If sharding is enabled and the test should not be run, skip it. | 505 // If sharding is enabled and the test should not be run, skip it. |
474 if (!should_run) { | 506 if (!should_run) { |
475 continue; | 507 continue; |
476 } | 508 } |
477 | 509 |
478 base::Time start_time = base::Time::Now(); | 510 base::Time start_time = base::Time::Now(); |
479 ++test_run_count; | 511 ++test_run_count; |
480 bool was_timeout = false; | 512 bool was_timeout = false; |
481 int exit_code = RunTest(launcher_delegate, | 513 int exit_code = RunTest(launcher_delegate, |
| 514 test_case, |
482 test_name, | 515 test_name, |
483 TestTimeouts::action_max_timeout(), | 516 TestTimeouts::action_max_timeout(), |
484 &was_timeout); | 517 &was_timeout); |
485 if (exit_code == 0) { | 518 if (exit_code == 0) { |
486 // Test passed. | 519 // Test passed. |
487 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, | 520 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, |
488 false, | 521 false, |
489 (base::Time::Now() - start_time).InMillisecondsF()); | 522 (base::Time::Now() - start_time).InMillisecondsF()); |
490 } else { | 523 } else { |
491 failed_tests.push_back(test_name); | 524 failed_tests.push_back(test_name); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 // from disk may be slow on a busy bot, and can easily exceed the default | 667 // from disk may be slow on a busy bot, and can easily exceed the default |
635 // timeout causing flaky test failures. Use an empty test that only starts | 668 // timeout causing flaky test failures. Use an empty test that only starts |
636 // and closes a browser with a long timeout to avoid those problems. | 669 // and closes a browser with a long timeout to avoid those problems. |
637 // NOTE: We don't do this when specifying a filter because this slows down | 670 // NOTE: We don't do this when specifying a filter because this slows down |
638 // the common case of running one test locally, and also on trybots when | 671 // the common case of running one test locally, and also on trybots when |
639 // sharding as this one test runs ~200 times and wastes a few minutes. | 672 // sharding as this one test runs ~200 times and wastes a few minutes. |
640 bool warmup = command_line->HasSwitch(kWarmupFlag); | 673 bool warmup = command_line->HasSwitch(kWarmupFlag); |
641 bool has_filter = command_line->HasSwitch(kGTestFilterFlag); | 674 bool has_filter = command_line->HasSwitch(kGTestFilterFlag); |
642 if (warmup || (!should_shard && !has_filter)) { | 675 if (warmup || (!should_shard && !has_filter)) { |
643 exit_code = RunTest(launcher_delegate, | 676 exit_code = RunTest(launcher_delegate, |
| 677 NULL, |
644 empty_test, | 678 empty_test, |
645 TestTimeouts::large_test_timeout(), | 679 TestTimeouts::large_test_timeout(), |
646 NULL); | 680 NULL); |
647 if (exit_code != 0 || warmup) | 681 if (exit_code != 0 || warmup) |
648 return exit_code; | 682 return exit_code; |
649 } | 683 } |
650 } | 684 } |
651 | 685 |
652 int cycles = 1; | 686 int cycles = 1; |
653 if (command_line->HasSwitch(kGTestRepeatFlag)) { | 687 if (command_line->HasSwitch(kGTestRepeatFlag)) { |
(...skipping 15 matching lines...) Expand all Loading... |
669 cycles--; | 703 cycles--; |
670 } | 704 } |
671 return exit_code; | 705 return exit_code; |
672 } | 706 } |
673 | 707 |
674 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { | 708 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { |
675 return g_launcher_delegate; | 709 return g_launcher_delegate; |
676 } | 710 } |
677 | 711 |
678 } // namespace test_launcher | 712 } // namespace test_launcher |
OLD | NEW |