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/test_launcher.h" | 5 #include "base/test/test_launcher.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.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/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/process_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/test/test_timeouts.h" | 18 #include "base/test/test_timeouts.h" |
18 #include "base/time.h" | 19 #include "base/time.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
22 #include "base/mac/scoped_nsautorelease_pool.h" | 23 #include "base/mac/scoped_nsautorelease_pool.h" |
23 #endif | 24 #endif |
24 | 25 |
25 namespace base { | 26 namespace base { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 const char kGTestOutputFlag[] = "gtest_output"; | 342 const char kGTestOutputFlag[] = "gtest_output"; |
342 | 343 |
343 const char kHelpFlag[] = "help"; | 344 const char kHelpFlag[] = "help"; |
344 | 345 |
345 TestResult::TestResult() { | 346 TestResult::TestResult() { |
346 } | 347 } |
347 | 348 |
348 TestLauncherDelegate::~TestLauncherDelegate() { | 349 TestLauncherDelegate::~TestLauncherDelegate() { |
349 } | 350 } |
350 | 351 |
| 352 int LaunchChildGTestProcess(const CommandLine& command_line, |
| 353 base::TimeDelta timeout, |
| 354 bool* was_timeout) { |
| 355 CommandLine new_command_line(command_line.GetProgram()); |
| 356 CommandLine::SwitchMap switches = command_line.GetSwitches(); |
| 357 |
| 358 // Strip out gtest_output flag because otherwise we would overwrite results |
| 359 // of the other tests. |
| 360 switches.erase(kGTestOutputFlag); |
| 361 |
| 362 // Strip out gtest_repeat flag - this is handled by the launcher process. |
| 363 switches.erase(kGTestRepeatFlag); |
| 364 |
| 365 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); |
| 366 iter != switches.end(); ++iter) { |
| 367 new_command_line.AppendSwitchNative((*iter).first, (*iter).second); |
| 368 } |
| 369 |
| 370 base::ProcessHandle process_handle; |
| 371 base::LaunchOptions options; |
| 372 |
| 373 #if defined(OS_POSIX) |
| 374 // On POSIX, we launch the test in a new process group with pgid equal to |
| 375 // its pid. Any child processes that the test may create will inherit the |
| 376 // same pgid. This way, if the test is abruptly terminated, we can clean up |
| 377 // any orphaned child processes it may have left behind. |
| 378 options.new_process_group = true; |
| 379 #endif |
| 380 |
| 381 if (!base::LaunchProcess(new_command_line, options, &process_handle)) |
| 382 return -1; |
| 383 |
| 384 int exit_code = 0; |
| 385 if (!base::WaitForExitCodeWithTimeout(process_handle, |
| 386 &exit_code, |
| 387 timeout)) { |
| 388 *was_timeout = true; |
| 389 exit_code = -1; // Set a non-zero exit code to signal a failure. |
| 390 |
| 391 // Ensure that the process terminates. |
| 392 base::KillProcess(process_handle, -1, true); |
| 393 } |
| 394 |
| 395 #if defined(OS_POSIX) |
| 396 if (exit_code != 0) { |
| 397 // On POSIX, in case the test does not exit cleanly, either due to a crash |
| 398 // or due to it timing out, we need to clean up any child processes that |
| 399 // it might have created. On Windows, child processes are automatically |
| 400 // cleaned up using JobObjects. |
| 401 base::KillProcessGroup(process_handle); |
| 402 } |
| 403 #endif |
| 404 |
| 405 base::CloseProcessHandle(process_handle); |
| 406 |
| 407 return exit_code; |
| 408 } |
| 409 |
351 int LaunchTests(TestLauncherDelegate* launcher_delegate, | 410 int LaunchTests(TestLauncherDelegate* launcher_delegate, |
352 int argc, | 411 int argc, |
353 char** argv) { | 412 char** argv) { |
354 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 413 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
355 | 414 |
356 int32 total_shards; | 415 int32 total_shards; |
357 int32 shard_index; | 416 int32 shard_index; |
358 InitSharding(&total_shards, &shard_index); | 417 InitSharding(&total_shards, &shard_index); |
359 | 418 |
360 int cycles = 1; | 419 int cycles = 1; |
361 if (command_line->HasSwitch(kGTestRepeatFlag)) | 420 if (command_line->HasSwitch(kGTestRepeatFlag)) |
362 StringToInt(command_line->GetSwitchValueASCII(kGTestRepeatFlag), &cycles); | 421 StringToInt(command_line->GetSwitchValueASCII(kGTestRepeatFlag), &cycles); |
363 | 422 |
364 int exit_code = 0; | 423 int exit_code = 0; |
365 while (cycles != 0) { | 424 while (cycles != 0) { |
366 if (!RunTests(launcher_delegate, total_shards, shard_index)) { | 425 if (!RunTests(launcher_delegate, total_shards, shard_index)) { |
367 exit_code = 1; | 426 exit_code = 1; |
368 break; | 427 break; |
369 } | 428 } |
370 | 429 |
371 // Special value "-1" means "repeat indefinitely". | 430 // Special value "-1" means "repeat indefinitely". |
372 if (cycles != -1) | 431 if (cycles != -1) |
373 cycles--; | 432 cycles--; |
374 } | 433 } |
375 | 434 |
376 return exit_code; | 435 return exit_code; |
377 } | 436 } |
378 | 437 |
379 } // namespace base | 438 } // namespace base |
OLD | NEW |