| 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/test_launcher.h" | 5 #include "base/test/launcher/test_launcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 #include "base/test/test_timeouts.h" | 40 #include "base/test/test_timeouts.h" |
| 41 #include "base/threading/thread.h" | 41 #include "base/threading/thread.h" |
| 42 #include "base/threading/thread_checker.h" | 42 #include "base/threading/thread_checker.h" |
| 43 #include "base/threading/thread_task_runner_handle.h" | 43 #include "base/threading/thread_task_runner_handle.h" |
| 44 #include "base/time/time.h" | 44 #include "base/time/time.h" |
| 45 #include "build/build_config.h" | 45 #include "build/build_config.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 46 #include "testing/gtest/include/gtest/gtest.h" |
| 47 | 47 |
| 48 #if defined(OS_POSIX) | 48 #if defined(OS_POSIX) |
| 49 #include <fcntl.h> | 49 #include <fcntl.h> |
| 50 |
| 51 #include "base/files/file_descriptor_watcher_posix.h" |
| 50 #endif | 52 #endif |
| 51 | 53 |
| 52 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
| 53 #include "base/mac/scoped_nsautorelease_pool.h" | 55 #include "base/mac/scoped_nsautorelease_pool.h" |
| 54 #endif | 56 #endif |
| 55 | 57 |
| 56 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
| 57 #include "base/win/windows_version.h" | 59 #include "base/win/windows_version.h" |
| 58 #endif | 60 #endif |
| 59 | 61 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Send the signal to entire process group. | 147 // Send the signal to entire process group. |
| 146 kill((-1) * (i->first), SIGKILL); | 148 kill((-1) * (i->first), SIGKILL); |
| 147 } | 149 } |
| 148 | 150 |
| 149 fprintf(stdout, "done.\n"); | 151 fprintf(stdout, "done.\n"); |
| 150 fflush(stdout); | 152 fflush(stdout); |
| 151 } | 153 } |
| 152 | 154 |
| 153 // I/O watcher for the reading end of the self-pipe above. | 155 // I/O watcher for the reading end of the self-pipe above. |
| 154 // Terminates any launched child processes and exits the process. | 156 // Terminates any launched child processes and exits the process. |
| 155 class SignalFDWatcher : public MessageLoopForIO::Watcher { | 157 void OnShutdownPipeReadable() { |
| 156 public: | 158 fprintf(stdout, "\nCaught signal. Killing spawned test processes...\n"); |
| 157 SignalFDWatcher() { | 159 fflush(stdout); |
| 158 } | |
| 159 | 160 |
| 160 void OnFileCanReadWithoutBlocking(int fd) override { | 161 KillSpawnedTestProcesses(); |
| 161 fprintf(stdout, "\nCaught signal. Killing spawned test processes...\n"); | |
| 162 fflush(stdout); | |
| 163 | 162 |
| 164 KillSpawnedTestProcesses(); | 163 // The signal would normally kill the process, so exit now. |
| 165 | 164 _exit(1); |
| 166 // The signal would normally kill the process, so exit now. | 165 } |
| 167 _exit(1); | |
| 168 } | |
| 169 | |
| 170 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); } | |
| 171 | |
| 172 private: | |
| 173 DISALLOW_COPY_AND_ASSIGN(SignalFDWatcher); | |
| 174 }; | |
| 175 #endif // defined(OS_POSIX) | 166 #endif // defined(OS_POSIX) |
| 176 | 167 |
| 177 // Parses the environment variable var as an Int32. If it is unset, returns | 168 // Parses the environment variable var as an Int32. If it is unset, returns |
| 178 // true. If it is set, unsets it then converts it to Int32 before | 169 // true. If it is set, unsets it then converts it to Int32 before |
| 179 // returning it in |result|. Returns true on success. | 170 // returning it in |result|. Returns true on success. |
| 180 bool TakeInt32FromEnvironment(const char* const var, int32_t* result) { | 171 bool TakeInt32FromEnvironment(const char* const var, int32_t* result) { |
| 181 std::unique_ptr<Environment> env(Environment::Create()); | 172 std::unique_ptr<Environment> env(Environment::Create()); |
| 182 std::string str_val; | 173 std::string str_val; |
| 183 | 174 |
| 184 if (!env->GetVar(var, &str_val)) | 175 if (!env->GetVar(var, &str_val)) |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 | 514 |
| 524 struct sigaction action; | 515 struct sigaction action; |
| 525 memset(&action, 0, sizeof(action)); | 516 memset(&action, 0, sizeof(action)); |
| 526 sigemptyset(&action.sa_mask); | 517 sigemptyset(&action.sa_mask); |
| 527 action.sa_handler = &ShutdownPipeSignalHandler; | 518 action.sa_handler = &ShutdownPipeSignalHandler; |
| 528 | 519 |
| 529 CHECK_EQ(0, sigaction(SIGINT, &action, NULL)); | 520 CHECK_EQ(0, sigaction(SIGINT, &action, NULL)); |
| 530 CHECK_EQ(0, sigaction(SIGQUIT, &action, NULL)); | 521 CHECK_EQ(0, sigaction(SIGQUIT, &action, NULL)); |
| 531 CHECK_EQ(0, sigaction(SIGTERM, &action, NULL)); | 522 CHECK_EQ(0, sigaction(SIGTERM, &action, NULL)); |
| 532 | 523 |
| 533 MessageLoopForIO::FileDescriptorWatcher controller; | 524 auto controller = base::FileDescriptorWatcher::WatchReadable( |
| 534 SignalFDWatcher watcher; | 525 g_shutdown_pipe[0], base::Bind(&OnShutdownPipeReadable)); |
| 535 | |
| 536 CHECK(MessageLoopForIO::current()->WatchFileDescriptor( | |
| 537 g_shutdown_pipe[0], | |
| 538 true, | |
| 539 MessageLoopForIO::WATCH_READ, | |
| 540 &controller, | |
| 541 &watcher)); | |
| 542 #endif // defined(OS_POSIX) | 526 #endif // defined(OS_POSIX) |
| 543 | 527 |
| 544 // Start the watchdog timer. | 528 // Start the watchdog timer. |
| 545 watchdog_timer_.Reset(); | 529 watchdog_timer_.Reset(); |
| 546 | 530 |
| 547 ThreadTaskRunnerHandle::Get()->PostTask( | 531 ThreadTaskRunnerHandle::Get()->PostTask( |
| 548 FROM_HERE, Bind(&TestLauncher::RunTestIteration, Unretained(this))); | 532 FROM_HERE, Bind(&TestLauncher::RunTestIteration, Unretained(this))); |
| 549 | 533 |
| 550 RunLoop().Run(); | 534 RunLoop().Run(); |
| 551 | 535 |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1156 } | 1140 } |
| 1157 | 1141 |
| 1158 std::string snippet(full_output.substr(run_pos)); | 1142 std::string snippet(full_output.substr(run_pos)); |
| 1159 if (end_pos != std::string::npos) | 1143 if (end_pos != std::string::npos) |
| 1160 snippet = full_output.substr(run_pos, end_pos - run_pos); | 1144 snippet = full_output.substr(run_pos, end_pos - run_pos); |
| 1161 | 1145 |
| 1162 return snippet; | 1146 return snippet; |
| 1163 } | 1147 } |
| 1164 | 1148 |
| 1165 } // namespace base | 1149 } // namespace base |
| OLD | NEW |