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, |
marja
2012/08/06 14:38:34
Could the test machinery enhancements go to a sepa
Charlie Reis
2012/08/06 23:28:11
Looks like it's already committed in https://chrom
awong
2012/08/07 00:38:48
Sorry...yes, this is actually in another CL that h
| |
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 std::string pre_test_name = test_name; |
308 if (was_timeout) | 308 ReplaceFirstSubstringAfterOffset(&pre_test_name, 0, ".", ".PRE_"); |
309 *was_timeout = false; | |
310 | 309 |
311 #if defined(OS_MACOSX) | 310 if (test_case) { |
312 // Some of the below method calls will leak objects if there is no | 311 for (int i = 0; i < test_case->total_test_count(); ++i) { |
313 // autorelease pool in place. | 312 const testing::TestInfo* test_info = test_case->GetTestInfo(i); |
314 base::mac::ScopedNSAutoreleasePool pool; | 313 std::string test_name = test_info->test_case_name(); |
315 #endif | 314 test_name.append("."); |
315 test_name.append(test_info->name()); | |
316 if (test_name == pre_test_name) { | |
317 int exit_code = RunTestInternal(test_case, pre_test_name, command_line, | |
318 default_timeout, was_timeout); | |
319 if (exit_code != 0) | |
320 return exit_code; | |
321 } | |
322 } | |
323 } | |
316 | 324 |
317 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 325 CommandLine new_cmd_line(*command_line); |
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 | 326 |
336 // Always enable disabled tests. This method is not called with disabled | 327 // Always enable disabled tests. This method is not called with disabled |
337 // tests unless this flag was specified to the browser test executable. | 328 // tests unless this flag was specified to the browser test executable. |
338 new_cmd_line.AppendSwitch("gtest_also_run_disabled_tests"); | 329 new_cmd_line.AppendSwitch("gtest_also_run_disabled_tests"); |
339 new_cmd_line.AppendSwitchASCII("gtest_filter", test_name); | 330 new_cmd_line.AppendSwitchASCII("gtest_filter", test_name); |
340 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); | 331 new_cmd_line.AppendSwitch(kSingleProcessTestsFlag); |
341 | 332 |
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"); | 333 const char* browser_wrapper = getenv("BROWSER_WRAPPER"); |
350 if (browser_wrapper) { | 334 if (browser_wrapper) { |
351 #if defined(OS_WIN) | 335 #if defined(OS_WIN) |
352 new_cmd_line.PrependWrapper(ASCIIToWide(browser_wrapper)); | 336 new_cmd_line.PrependWrapper(ASCIIToWide(browser_wrapper)); |
353 #elif defined(OS_POSIX) | 337 #elif defined(OS_POSIX) |
354 new_cmd_line.PrependWrapper(browser_wrapper); | 338 new_cmd_line.PrependWrapper(browser_wrapper); |
355 #endif | 339 #endif |
356 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with " | 340 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with " |
357 << browser_wrapper; | 341 << browser_wrapper; |
358 } | 342 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 // cleaned up using JobObjects. | 379 // cleaned up using JobObjects. |
396 base::KillProcessGroup(process_handle); | 380 base::KillProcessGroup(process_handle); |
397 } | 381 } |
398 #endif | 382 #endif |
399 | 383 |
400 base::CloseProcessHandle(process_handle); | 384 base::CloseProcessHandle(process_handle); |
401 | 385 |
402 return exit_code; | 386 return exit_code; |
403 } | 387 } |
404 | 388 |
389 // Runs test specified by |test_name| in a child process, | |
390 // and returns the exit code. | |
391 int RunTest(TestLauncherDelegate* launcher_delegate, | |
392 const testing::TestCase* test_case, | |
393 const std::string& test_name, | |
394 base::TimeDelta default_timeout, | |
395 bool* was_timeout) { | |
396 if (was_timeout) | |
397 *was_timeout = false; | |
398 | |
399 #if defined(OS_MACOSX) | |
400 // Some of the below method calls will leak objects if there is no | |
401 // autorelease pool in place. | |
402 base::mac::ScopedNSAutoreleasePool pool; | |
403 #endif | |
404 | |
405 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
406 CommandLine new_cmd_line(cmd_line->GetProgram()); | |
407 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); | |
408 | |
409 // Strip out gtest_output flag because otherwise we would overwrite results | |
410 // of the previous test. We will generate the final output file later | |
411 // in RunTests(). | |
412 switches.erase(kGTestOutputFlag); | |
413 | |
414 // Strip out gtest_repeat flag because we can only run one test in the child | |
415 // process (restarting the browser in the same process is illegal after it | |
416 // has been shut down and will actually crash). | |
417 switches.erase(kGTestRepeatFlag); | |
418 | |
419 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); | |
420 iter != switches.end(); ++iter) { | |
421 new_cmd_line.AppendSwitchNative((*iter).first, (*iter).second); | |
422 } | |
423 | |
424 // Do not let the child ignore failures. We need to propagate the | |
425 // failure status back to the parent. | |
426 new_cmd_line.AppendSwitch(base::TestSuite::kStrictFailureHandling); | |
427 | |
428 if (!launcher_delegate->AdjustChildProcessCommandLine(&new_cmd_line)) | |
429 return -1; | |
430 | |
431 return RunTestInternal( | |
432 test_case, test_name, &new_cmd_line, default_timeout, was_timeout); | |
433 } | |
434 | |
405 bool RunTests(TestLauncherDelegate* launcher_delegate, | 435 bool RunTests(TestLauncherDelegate* launcher_delegate, |
406 bool should_shard, | 436 bool should_shard, |
407 int total_shards, | 437 int total_shards, |
408 int shard_index) { | 438 int shard_index) { |
409 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 439 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
410 | 440 |
411 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); | 441 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); |
412 | 442 |
413 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); | 443 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
414 | 444 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 continue; | 477 continue; |
448 | 478 |
449 // Skip disabled tests. | 479 // Skip disabled tests. |
450 if (test_name.find("DISABLED") != std::string::npos && | 480 if (test_name.find("DISABLED") != std::string::npos && |
451 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | 481 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { |
452 printer.OnTestEnd(test_info->name(), test_case->name(), | 482 printer.OnTestEnd(test_info->name(), test_case->name(), |
453 false, false, false, 0); | 483 false, false, false, 0); |
454 continue; | 484 continue; |
455 } | 485 } |
456 | 486 |
487 if (StartsWithASCII(test_info->name(), "PRE_", true)) | |
488 continue; | |
489 | |
457 // Skip the test that doesn't match the filter string (if given). | 490 // Skip the test that doesn't match the filter string (if given). |
458 if ((!positive_filter.empty() && | 491 if ((!positive_filter.empty() && |
459 !MatchesFilter(test_name, positive_filter)) || | 492 !MatchesFilter(test_name, positive_filter)) || |
460 MatchesFilter(test_name, negative_filter)) { | 493 MatchesFilter(test_name, negative_filter)) { |
461 printer.OnTestEnd(test_info->name(), test_case->name(), | 494 printer.OnTestEnd(test_info->name(), test_case->name(), |
462 false, false, false, 0); | 495 false, false, false, 0); |
463 continue; | 496 continue; |
464 } | 497 } |
465 | 498 |
466 // Decide if this test should be run. | 499 // Decide if this test should be run. |
467 bool should_run = true; | 500 bool should_run = true; |
468 if (should_shard) { | 501 if (should_shard) { |
469 should_run = ShouldRunTestOnShard(total_shards, shard_index, | 502 should_run = ShouldRunTestOnShard(total_shards, shard_index, |
470 num_runnable_tests); | 503 num_runnable_tests); |
471 } | 504 } |
472 num_runnable_tests += 1; | 505 num_runnable_tests += 1; |
473 // If sharding is enabled and the test should not be run, skip it. | 506 // If sharding is enabled and the test should not be run, skip it. |
474 if (!should_run) { | 507 if (!should_run) { |
475 continue; | 508 continue; |
476 } | 509 } |
477 | 510 |
478 base::Time start_time = base::Time::Now(); | 511 base::Time start_time = base::Time::Now(); |
479 ++test_run_count; | 512 ++test_run_count; |
480 bool was_timeout = false; | 513 bool was_timeout = false; |
481 int exit_code = RunTest(launcher_delegate, | 514 int exit_code = RunTest(launcher_delegate, |
515 test_case, | |
482 test_name, | 516 test_name, |
483 TestTimeouts::action_max_timeout(), | 517 TestTimeouts::action_max_timeout(), |
484 &was_timeout); | 518 &was_timeout); |
485 if (exit_code == 0) { | 519 if (exit_code == 0) { |
486 // Test passed. | 520 // Test passed. |
487 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, | 521 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, |
488 false, | 522 false, |
489 (base::Time::Now() - start_time).InMillisecondsF()); | 523 (base::Time::Now() - start_time).InMillisecondsF()); |
490 } else { | 524 } else { |
491 failed_tests.push_back(test_name); | 525 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 | 668 // 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 | 669 // 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. | 670 // 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 | 671 // 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 | 672 // 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. | 673 // sharding as this one test runs ~200 times and wastes a few minutes. |
640 bool warmup = command_line->HasSwitch(kWarmupFlag); | 674 bool warmup = command_line->HasSwitch(kWarmupFlag); |
641 bool has_filter = command_line->HasSwitch(kGTestFilterFlag); | 675 bool has_filter = command_line->HasSwitch(kGTestFilterFlag); |
642 if (warmup || (!should_shard && !has_filter)) { | 676 if (warmup || (!should_shard && !has_filter)) { |
643 exit_code = RunTest(launcher_delegate, | 677 exit_code = RunTest(launcher_delegate, |
678 NULL, | |
644 empty_test, | 679 empty_test, |
645 TestTimeouts::large_test_timeout(), | 680 TestTimeouts::large_test_timeout(), |
646 NULL); | 681 NULL); |
647 if (exit_code != 0 || warmup) | 682 if (exit_code != 0 || warmup) |
648 return exit_code; | 683 return exit_code; |
649 } | 684 } |
650 } | 685 } |
651 | 686 |
652 int cycles = 1; | 687 int cycles = 1; |
653 if (command_line->HasSwitch(kGTestRepeatFlag)) { | 688 if (command_line->HasSwitch(kGTestRepeatFlag)) { |
(...skipping 15 matching lines...) Expand all Loading... | |
669 cycles--; | 704 cycles--; |
670 } | 705 } |
671 return exit_code; | 706 return exit_code; |
672 } | 707 } |
673 | 708 |
674 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { | 709 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { |
675 return g_launcher_delegate; | 710 return g_launcher_delegate; |
676 } | 711 } |
677 | 712 |
678 } // namespace test_launcher | 713 } // namespace test_launcher |
OLD | NEW |