OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/at_exit.h" | 5 #include "base/at_exit.h" |
6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/test/multiprocess_test.h" |
| 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/threading/thread.h" |
8 #include "chrome/common/chrome_version_info.h" | 11 #include "chrome/common/chrome_version_info.h" |
9 #include "chrome/common/service_process_util.h" | 12 #include "chrome/common/service_process_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/multiprocess_func_list.h" |
11 | 14 |
| 15 namespace { |
| 16 |
| 17 bool g_good_shutdown = false; |
| 18 |
| 19 void ShutdownTask(MessageLoop* loop) { |
| 20 // Quit the main message loop. |
| 21 ASSERT_FALSE(g_good_shutdown); |
| 22 g_good_shutdown = true; |
| 23 loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 24 } |
| 25 |
| 26 } // namespace |
12 | 27 |
13 TEST(ServiceProcessUtilTest, ScopedVersionedName) { | 28 TEST(ServiceProcessUtilTest, ScopedVersionedName) { |
14 std::string test_str = "test"; | 29 std::string test_str = "test"; |
15 std::string scoped_name = GetServiceProcessScopedVersionedName(test_str); | 30 std::string scoped_name = GetServiceProcessScopedVersionedName(test_str); |
16 chrome::VersionInfo version_info; | 31 chrome::VersionInfo version_info; |
17 DCHECK(version_info.is_valid()); | 32 DCHECK(version_info.is_valid()); |
18 EXPECT_TRUE(EndsWith(scoped_name, test_str, true)); | 33 EXPECT_TRUE(EndsWith(scoped_name, test_str, true)); |
19 EXPECT_NE(std::string::npos, scoped_name.find(version_info.Version())); | 34 EXPECT_NE(std::string::npos, scoped_name.find(version_info.Version())); |
20 } | 35 } |
21 | 36 |
22 class ServiceProcessStateTest : public testing::Test { | 37 class ServiceProcessStateTest : public base::MultiProcessTest { |
| 38 public: |
| 39 ServiceProcessStateTest(); |
| 40 ~ServiceProcessStateTest(); |
| 41 virtual void SetUp(); |
| 42 MessageLoop* IOMessageLoop() { return io_thread_.message_loop(); } |
| 43 void LaunchAndWait(const std::string& name); |
| 44 |
23 private: | 45 private: |
24 // This is used to release the ServiceProcessState singleton after each test. | 46 // This is used to release the ServiceProcessState singleton after each test. |
25 base::ShadowingAtExitManager at_exit_manager_; | 47 base::ShadowingAtExitManager at_exit_manager_; |
| 48 base::Thread io_thread_; |
26 }; | 49 }; |
27 | 50 |
28 #if defined(OS_WIN) | 51 ServiceProcessStateTest::ServiceProcessStateTest() |
29 // Singleton-ness is only implemented on Windows. | 52 : io_thread_("ServiceProcessStateTestThread") { |
30 // TODO(sanjeev): Rewrite this test to spawn a new process and test using the | 53 } |
31 // ServiceProcessState singleton across processes. | 54 |
32 /* | 55 ServiceProcessStateTest::~ServiceProcessStateTest() { |
| 56 } |
| 57 |
| 58 void ServiceProcessStateTest::SetUp() { |
| 59 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 60 ASSERT_TRUE(io_thread_.StartWithOptions(options)); |
| 61 } |
| 62 |
| 63 void ServiceProcessStateTest::LaunchAndWait(const std::string& name) { |
| 64 base::ProcessHandle handle = SpawnChild(name, false); |
| 65 ASSERT_TRUE(handle); |
| 66 int exit_code = 0; |
| 67 ASSERT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 68 ASSERT_EQ(exit_code, 0); |
| 69 } |
| 70 |
33 TEST_F(ServiceProcessStateTest, Singleton) { | 71 TEST_F(ServiceProcessStateTest, Singleton) { |
34 ServiceProcessState state; | 72 ServiceProcessState* state = ServiceProcessState::GetInstance(); |
35 EXPECT_TRUE(state.Initialize()); | 73 ASSERT_TRUE(state->Initialize()); |
36 // The second instance should fail to Initialize. | 74 LaunchAndWait("ServiceProcessStateTestSingleton"); |
37 ServiceProcessState another_state; | |
38 EXPECT_FALSE(another_state.Initialize()); | |
39 } | 75 } |
40 */ | |
41 #endif // defined(OS_WIN) | |
42 | 76 |
43 TEST_F(ServiceProcessStateTest, ReadyState) { | 77 TEST_F(ServiceProcessStateTest, ReadyState) { |
44 #if defined(OS_WIN) | 78 ASSERT_FALSE(CheckServiceProcessReady()); |
45 // On Posix, we use a lock file on disk to signal readiness. This lock file | |
46 // could be lying around from previous crashes which could cause | |
47 // CheckServiceProcessReady to lie. On Windows, we use a named event so we | |
48 // don't have this issue. Until we have a more stable signalling mechanism on | |
49 // Posix, this check will only execute on Windows. | |
50 EXPECT_FALSE(CheckServiceProcessReady()); | |
51 #endif // defined(OS_WIN) | |
52 ServiceProcessState* state = ServiceProcessState::GetInstance(); | 79 ServiceProcessState* state = ServiceProcessState::GetInstance(); |
53 EXPECT_TRUE(state->Initialize()); | 80 ASSERT_TRUE(state->Initialize()); |
54 state->SignalReady(NULL); | 81 ASSERT_TRUE(state->SignalReady(IOMessageLoop(), NULL)); |
55 EXPECT_TRUE(CheckServiceProcessReady()); | 82 LaunchAndWait("ServiceProcessStateTestReadyTrue"); |
56 state->SignalStopped(); | 83 state->SignalStopped(); |
57 EXPECT_FALSE(CheckServiceProcessReady()); | 84 LaunchAndWait("ServiceProcessStateTestReadyFalse"); |
58 } | 85 } |
59 | 86 |
60 TEST_F(ServiceProcessStateTest, SharedMem) { | 87 TEST_F(ServiceProcessStateTest, SharedMem) { |
| 88 std::string version; |
| 89 base::ProcessId pid; |
61 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
62 // On Posix, named shared memory uses a file on disk. This file | 91 // On Posix, named shared memory uses a file on disk. This file |
63 // could be lying around from previous crashes which could cause | 92 // could be lying around from previous crashes which could cause |
64 // GetServiceProcessPid to lie. On Windows, we use a named event so we | 93 // GetServiceProcessPid to lie. On Windows, we use a named event so we |
65 // don't have this issue. Until we have a more stable shared memory | 94 // don't have this issue. Until we have a more stable shared memory |
66 // implementation on Posix, this check will only execute on Windows. | 95 // implementation on Posix, this check will only execute on Windows. |
67 EXPECT_EQ(0, GetServiceProcessPid()); | 96 ASSERT_FALSE(GetServiceProcessSharedData(&version, &pid)); |
68 #endif // defined(OS_WIN) | 97 #endif // defined(OS_WIN) |
69 ServiceProcessState* state = ServiceProcessState::GetInstance(); | 98 ServiceProcessState* state = ServiceProcessState::GetInstance(); |
70 EXPECT_TRUE(state->Initialize()); | 99 ASSERT_TRUE(state->Initialize()); |
71 EXPECT_EQ(base::GetCurrentProcId(), GetServiceProcessPid()); | 100 ASSERT_TRUE(GetServiceProcessSharedData(&version, &pid)); |
| 101 ASSERT_EQ(base::GetCurrentProcId(), pid); |
72 } | 102 } |
73 | 103 |
| 104 TEST_F(ServiceProcessStateTest, ForceShutdown) { |
| 105 base::ProcessHandle handle = SpawnChild("ServiceProcessStateTestShutdown", |
| 106 true); |
| 107 ASSERT_TRUE(handle); |
| 108 for (int i = 0; !CheckServiceProcessReady() && i < 10; ++i) { |
| 109 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
| 110 } |
| 111 ASSERT_TRUE(CheckServiceProcessReady()); |
| 112 std::string version; |
| 113 base::ProcessId pid; |
| 114 ASSERT_TRUE(GetServiceProcessSharedData(&version, &pid)); |
| 115 ASSERT_TRUE(ForceServiceProcessShutdown(version, pid)); |
| 116 int exit_code = 0; |
| 117 ASSERT_TRUE(base::WaitForExitCodeWithTimeout(handle, |
| 118 &exit_code, TestTimeouts::action_timeout_ms() * 2)); |
| 119 ASSERT_EQ(exit_code, 0); |
| 120 } |
| 121 |
| 122 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestSingleton) { |
| 123 ServiceProcessState* state = ServiceProcessState::GetInstance(); |
| 124 EXPECT_FALSE(state->Initialize()); |
| 125 return 0; |
| 126 } |
| 127 |
| 128 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyTrue) { |
| 129 EXPECT_TRUE(CheckServiceProcessReady()); |
| 130 return 0; |
| 131 } |
| 132 |
| 133 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyFalse) { |
| 134 EXPECT_FALSE(CheckServiceProcessReady()); |
| 135 return 0; |
| 136 } |
| 137 |
| 138 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown) { |
| 139 MessageLoop message_loop; |
| 140 message_loop.set_thread_name("ServiceProcessStateTestShutdownMainThread"); |
| 141 base::Thread io_thread_("ServiceProcessStateTestShutdownIOThread"); |
| 142 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 143 EXPECT_TRUE(io_thread_.StartWithOptions(options)); |
| 144 ServiceProcessState* state = ServiceProcessState::GetInstance(); |
| 145 EXPECT_TRUE(state->Initialize()); |
| 146 EXPECT_TRUE(state->SignalReady(io_thread_.message_loop(), |
| 147 NewRunnableFunction(&ShutdownTask, |
| 148 MessageLoop::current()))); |
| 149 message_loop.PostDelayedTask(FROM_HERE, |
| 150 new MessageLoop::QuitTask(), |
| 151 TestTimeouts::action_max_timeout_ms()); |
| 152 EXPECT_FALSE(g_good_shutdown); |
| 153 message_loop.Run(); |
| 154 EXPECT_TRUE(g_good_shutdown); |
| 155 return 0; |
| 156 } |
| 157 |
OLD | NEW |