| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/process/process.h" | 5 #include "base/process/process.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| 11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/multiprocess_func_list.h" | 15 #include "testing/multiprocess_func_list.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 const int kExpectedStillRunningExitCode = 0x102; | 20 const int kExpectedStillRunningExitCode = 0x102; |
| 21 #else | 21 #else |
| 22 const int kExpectedStillRunningExitCode = 0; | 22 const int kExpectedStillRunningExitCode = 0; |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 #if defined(OS_MACOSX) |
| 26 // Fake port provider that returns the calling process's |
| 27 // task port, ignoring its argument. |
| 28 class FakePortProvider : public base::PortProvider { |
| 29 mach_port_t TaskForPid(base::ProcessHandle process) const override { |
| 30 return mach_task_self(); |
| 31 } |
| 32 }; |
| 33 #endif |
| 34 |
| 25 } // namespace | 35 } // namespace |
| 26 | 36 |
| 27 namespace base { | 37 namespace base { |
| 28 | 38 |
| 29 class ProcessTest : public MultiProcessTest { | 39 class ProcessTest : public MultiProcessTest { |
| 30 }; | 40 }; |
| 31 | 41 |
| 32 TEST_F(ProcessTest, Create) { | 42 TEST_F(ProcessTest, Create) { |
| 33 Process process(SpawnChild("SimpleChildProcess")); | 43 Process process(SpawnChild("SimpleChildProcess")); |
| 34 ASSERT_TRUE(process.IsValid()); | 44 ASSERT_TRUE(process.IsValid()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // Note: a platform may not be willing or able to lower the priority of | 181 // Note: a platform may not be willing or able to lower the priority of |
| 172 // a process. The calls to SetProcessBackground should be noops then. | 182 // a process. The calls to SetProcessBackground should be noops then. |
| 173 TEST_F(ProcessTest, SetProcessBackgrounded) { | 183 TEST_F(ProcessTest, SetProcessBackgrounded) { |
| 174 Process process(SpawnChild("SimpleChildProcess")); | 184 Process process(SpawnChild("SimpleChildProcess")); |
| 175 int old_priority = process.GetPriority(); | 185 int old_priority = process.GetPriority(); |
| 176 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
| 177 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 187 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 178 EXPECT_TRUE(process.IsProcessBackgrounded()); | 188 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 179 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 189 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 180 EXPECT_FALSE(process.IsProcessBackgrounded()); | 190 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 191 #elif defined(OS_MACOSX) |
| 192 // On the Mac, backgrounding a process requires a port to that process. |
| 193 // In the browser it's available through the MachBroker class, which is not |
| 194 // part of base. Additionally, there is an indefinite amount of time between |
| 195 // spawning a process and receiving its port. Because this test just checks |
| 196 // the ability to background/foreground a process, we can use the current |
| 197 // process's port instead. |
| 198 FakePortProvider provider; |
| 199 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true)); |
| 200 EXPECT_TRUE(process.IsProcessBackgrounded(&provider)); |
| 201 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false)); |
| 202 EXPECT_FALSE(process.IsProcessBackgrounded(&provider)); |
| 203 |
| 181 #else | 204 #else |
| 182 if (process.CanBackgroundProcesses()) { | 205 if (process.CanBackgroundProcesses()) { |
| 183 process.SetProcessBackgrounded(true); | 206 process.SetProcessBackgrounded(true); |
| 184 process.SetProcessBackgrounded(false); | 207 process.SetProcessBackgrounded(false); |
| 185 } | 208 } |
| 186 #endif | 209 #endif |
| 187 int new_priority = process.GetPriority(); | 210 int new_priority = process.GetPriority(); |
| 188 EXPECT_EQ(old_priority, new_priority); | 211 EXPECT_EQ(old_priority, new_priority); |
| 189 } | 212 } |
| 190 | 213 |
| 191 // Same as SetProcessBackgrounded but to this very process. It uses | 214 // Same as SetProcessBackgrounded but to this very process. It uses |
| 192 // a different code path at least for Windows. | 215 // a different code path at least for Windows. |
| 193 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { | 216 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { |
| 194 Process process = Process::Current(); | 217 Process process = Process::Current(); |
| 195 int old_priority = process.GetPriority(); | 218 int old_priority = process.GetPriority(); |
| 196 #if defined(OS_WIN) | 219 #if defined(OS_WIN) |
| 197 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 220 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 198 EXPECT_TRUE(process.IsProcessBackgrounded()); | 221 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 199 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 222 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 200 EXPECT_FALSE(process.IsProcessBackgrounded()); | 223 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 224 #elif defined(OS_MACOSX) |
| 225 FakePortProvider provider; |
| 226 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true)); |
| 227 EXPECT_TRUE(process.IsProcessBackgrounded(&provider)); |
| 228 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false)); |
| 229 EXPECT_FALSE(process.IsProcessBackgrounded(&provider)); |
| 201 #else | 230 #else |
| 202 process.SetProcessBackgrounded(true); | 231 process.SetProcessBackgrounded(true); |
| 203 process.SetProcessBackgrounded(false); | 232 process.SetProcessBackgrounded(false); |
| 204 #endif | 233 #endif |
| 205 int new_priority = process.GetPriority(); | 234 int new_priority = process.GetPriority(); |
| 206 EXPECT_EQ(old_priority, new_priority); | 235 EXPECT_EQ(old_priority, new_priority); |
| 207 } | 236 } |
| 208 | 237 |
| 209 #if defined(OS_CHROMEOS) | 238 #if defined(OS_CHROMEOS) |
| 210 | 239 |
| 211 // Tests that the function IsProcessBackgroundedCGroup() can parse the contents | 240 // Tests that the function IsProcessBackgroundedCGroup() can parse the contents |
| 212 // of the /proc/<pid>/cgroup file successfully. | 241 // of the /proc/<pid>/cgroup file successfully. |
| 213 TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) { | 242 TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) { |
| 214 const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n"; | 243 const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n"; |
| 215 const char kBackgrounded[] = | 244 const char kBackgrounded[] = |
| 216 "2:freezer:/chrome_renderers/to_be_frozen\n" | 245 "2:freezer:/chrome_renderers/to_be_frozen\n" |
| 217 "1:cpu:/chrome_renderers/background\n"; | 246 "1:cpu:/chrome_renderers/background\n"; |
| 218 | 247 |
| 219 EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded)); | 248 EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded)); |
| 220 EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded)); | 249 EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded)); |
| 221 } | 250 } |
| 222 | 251 |
| 223 #endif // defined(OS_CHROMEOS) | 252 #endif // defined(OS_CHROMEOS) |
| 224 | 253 |
| 225 } // namespace base | 254 } // namespace base |
| OLD | NEW |