OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/process_watcher.h" | 5 #include "chrome/common/process_watcher.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #include <sys/wait.h> | 8 #include <sys/wait.h> |
9 | 9 |
10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
| 11 #include "base/multiprocess_test.h" |
11 #include "base/process_util.h" | 12 #include "base/process_util.h" |
12 #include "base/test/multiprocess_test.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/multiprocess_func_list.h" | |
15 | 14 |
16 class ProcessWatcherTest : public base::MultiProcessTest { | 15 class ProcessWatcherTest : public MultiProcessTest { |
17 }; | 16 }; |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 bool IsProcessDead(base::ProcessHandle child) { | 20 bool IsProcessDead(base::ProcessHandle child) { |
22 // waitpid() will actually reap the process which is exactly NOT what we | 21 // waitpid() will actually reap the process which is exactly NOT what we |
23 // want to test for. The good thing is that if it can't find the process | 22 // want to test for. The good thing is that if it can't find the process |
24 // we'll get a nice value for errno which we can test for. | 23 // we'll get a nice value for errno which we can test for. |
25 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); | 24 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); |
26 return result == -1 && errno == ECHILD; | 25 return result == -1 && errno == ECHILD; |
27 } | 26 } |
28 | 27 |
29 } // namespace | 28 } // namespace |
30 | 29 |
31 TEST_F(ProcessWatcherTest, DelayedTermination) { | 30 TEST_F(ProcessWatcherTest, DelayedTermination) { |
32 base::ProcessHandle child_process = | 31 base::ProcessHandle child_process = |
33 SpawnChild("process_watcher_test_never_die", false); | 32 SpawnChild("process_watcher_test_never_die"); |
34 ProcessWatcher::EnsureProcessTerminated(child_process); | 33 ProcessWatcher::EnsureProcessTerminated(child_process); |
35 base::WaitForSingleProcess(child_process, 5000); | 34 base::WaitForSingleProcess(child_process, 5000); |
36 | 35 |
37 // Check that process was really killed. | 36 // Check that process was really killed. |
38 EXPECT_TRUE(IsProcessDead(child_process)); | 37 EXPECT_TRUE(IsProcessDead(child_process)); |
39 base::CloseProcessHandle(child_process); | 38 base::CloseProcessHandle(child_process); |
40 } | 39 } |
41 | 40 |
42 MULTIPROCESS_TEST_MAIN(process_watcher_test_never_die) { | 41 MULTIPROCESS_TEST_MAIN(process_watcher_test_never_die) { |
43 while (1) { | 42 while (1) { |
44 sleep(500); | 43 sleep(500); |
45 } | 44 } |
46 return 0; | 45 return 0; |
47 } | 46 } |
48 | 47 |
49 TEST_F(ProcessWatcherTest, ImmediateTermination) { | 48 TEST_F(ProcessWatcherTest, ImmediateTermination) { |
50 base::ProcessHandle child_process = | 49 base::ProcessHandle child_process = |
51 SpawnChild("process_watcher_test_die_immediately", false); | 50 SpawnChild("process_watcher_test_die_immediately"); |
52 // Give it time to die. | 51 // Give it time to die. |
53 sleep(2); | 52 sleep(2); |
54 ProcessWatcher::EnsureProcessTerminated(child_process); | 53 ProcessWatcher::EnsureProcessTerminated(child_process); |
55 | 54 |
56 // Check that process was really killed. | 55 // Check that process was really killed. |
57 EXPECT_TRUE(IsProcessDead(child_process)); | 56 EXPECT_TRUE(IsProcessDead(child_process)); |
58 base::CloseProcessHandle(child_process); | 57 base::CloseProcessHandle(child_process); |
59 } | 58 } |
60 | 59 |
61 MULTIPROCESS_TEST_MAIN(process_watcher_test_die_immediately) { | 60 MULTIPROCESS_TEST_MAIN(process_watcher_test_die_immediately) { |
62 return 0; | 61 return 0; |
63 } | 62 } |
64 | 63 |
65 #endif // OS_POSIX | 64 #endif // OS_POSIX |
OLD | NEW |