OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/process/process.h" |
| 6 |
| 7 #include "base/process/kill.h" |
| 8 #include "base/test/multiprocess_test.h" |
| 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/threading/platform_thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/multiprocess_func_list.h" |
| 13 |
| 14 |
| 15 namespace { |
| 16 |
| 17 #if defined(OS_WIN) |
| 18 const int kExpectedStillRunningExitCode = 0x102; |
| 19 #else |
| 20 const int kExpectedStillRunningExitCode = 0; |
| 21 #endif |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace base { |
| 26 |
| 27 class ProcessTest : public MultiProcessTest { |
| 28 }; |
| 29 |
| 30 TEST_F(ProcessTest, Create) { |
| 31 Process process(SpawnChild("SimpleChildProcess")); |
| 32 ASSERT_TRUE(process.IsValid()); |
| 33 ASSERT_FALSE(process.is_current()); |
| 34 process.Close(); |
| 35 ASSERT_FALSE(process.IsValid()); |
| 36 } |
| 37 |
| 38 TEST_F(ProcessTest, CreateCurrent) { |
| 39 Process process = Process::Current(); |
| 40 ASSERT_TRUE(process.IsValid()); |
| 41 ASSERT_TRUE(process.is_current()); |
| 42 process.Close(); |
| 43 ASSERT_FALSE(process.IsValid()); |
| 44 } |
| 45 |
| 46 TEST_F(ProcessTest, Move) { |
| 47 Process process1(SpawnChild("SimpleChildProcess")); |
| 48 EXPECT_TRUE(process1.IsValid()); |
| 49 |
| 50 Process process2; |
| 51 EXPECT_FALSE(process2.IsValid()); |
| 52 |
| 53 process2 = process1.Pass(); |
| 54 EXPECT_TRUE(process2.IsValid()); |
| 55 EXPECT_FALSE(process1.IsValid()); |
| 56 EXPECT_FALSE(process2.is_current()); |
| 57 |
| 58 Process process3 = Process::Current(); |
| 59 process2 = process3.Pass(); |
| 60 EXPECT_TRUE(process2.is_current()); |
| 61 EXPECT_TRUE(process2.IsValid()); |
| 62 EXPECT_FALSE(process3.IsValid()); |
| 63 } |
| 64 |
| 65 TEST_F(ProcessTest, Duplicate) { |
| 66 Process process1(SpawnChild("SimpleChildProcess")); |
| 67 ASSERT_TRUE(process1.IsValid()); |
| 68 |
| 69 Process process2 = process1.Duplicate(); |
| 70 ASSERT_TRUE(process1.IsValid()); |
| 71 ASSERT_TRUE(process2.IsValid()); |
| 72 EXPECT_EQ(process1.pid(), process2.pid()); |
| 73 EXPECT_FALSE(process1.is_current()); |
| 74 EXPECT_FALSE(process2.is_current()); |
| 75 |
| 76 process1.Close(); |
| 77 ASSERT_TRUE(process2.IsValid()); |
| 78 } |
| 79 |
| 80 TEST_F(ProcessTest, DuplicateCurrent) { |
| 81 Process process1 = Process::Current(); |
| 82 ASSERT_TRUE(process1.IsValid()); |
| 83 |
| 84 Process process2 = process1.Duplicate(); |
| 85 ASSERT_TRUE(process1.IsValid()); |
| 86 ASSERT_TRUE(process2.IsValid()); |
| 87 EXPECT_EQ(process1.pid(), process2.pid()); |
| 88 EXPECT_TRUE(process1.is_current()); |
| 89 EXPECT_TRUE(process2.is_current()); |
| 90 |
| 91 process1.Close(); |
| 92 ASSERT_TRUE(process2.IsValid()); |
| 93 } |
| 94 |
| 95 MULTIPROCESS_TEST_MAIN(SleepyChildProcess) { |
| 96 PlatformThread::Sleep(TestTimeouts::action_max_timeout()); |
| 97 return 0; |
| 98 } |
| 99 |
| 100 TEST_F(ProcessTest, Terminate) { |
| 101 Process process(SpawnChild("SleepyChildProcess")); |
| 102 ASSERT_TRUE(process.IsValid()); |
| 103 |
| 104 const int kDummyExitCode = 42; |
| 105 int exit_code = kDummyExitCode; |
| 106 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING, |
| 107 GetTerminationStatus(process.Handle(), &exit_code)); |
| 108 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code); |
| 109 |
| 110 exit_code = kDummyExitCode; |
| 111 int kExpectedExitCode = 250; |
| 112 process.Terminate(kExpectedExitCode); |
| 113 WaitForSingleProcess(process.Handle(), TestTimeouts::action_max_timeout()); |
| 114 |
| 115 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING, |
| 116 GetTerminationStatus(process.Handle(), &exit_code)); |
| 117 #if !defined(OS_POSIX) |
| 118 // The POSIX implementation actually ignores the exit_code. |
| 119 EXPECT_EQ(kExpectedExitCode, exit_code); |
| 120 #endif |
| 121 } |
| 122 |
| 123 // Ensure that the priority of a process is restored correctly after |
| 124 // backgrounding and restoring. |
| 125 // Note: a platform may not be willing or able to lower the priority of |
| 126 // a process. The calls to SetProcessBackground should be noops then. |
| 127 TEST_F(ProcessTest, SetProcessBackgrounded) { |
| 128 Process process(SpawnChild("SimpleChildProcess")); |
| 129 int old_priority = process.GetPriority(); |
| 130 #if defined(OS_WIN) |
| 131 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 132 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 133 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 134 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 135 #else |
| 136 process.SetProcessBackgrounded(true); |
| 137 process.SetProcessBackgrounded(false); |
| 138 #endif |
| 139 int new_priority = process.GetPriority(); |
| 140 EXPECT_EQ(old_priority, new_priority); |
| 141 } |
| 142 |
| 143 // Same as SetProcessBackgrounded but to this very process. It uses |
| 144 // a different code path at least for Windows. |
| 145 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { |
| 146 Process process = Process::Current(); |
| 147 int old_priority = process.GetPriority(); |
| 148 #if defined(OS_WIN) |
| 149 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 150 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 151 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 152 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 153 #else |
| 154 process.SetProcessBackgrounded(true); |
| 155 process.SetProcessBackgrounded(false); |
| 156 #endif |
| 157 int new_priority = process.GetPriority(); |
| 158 EXPECT_EQ(old_priority, new_priority); |
| 159 } |
| 160 |
| 161 } // namespace base |
OLD | NEW |