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 "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "base/location.h" | 25 #include "base/location.h" |
26 #include "base/posix/eintr_wrapper.h" | 26 #include "base/posix/eintr_wrapper.h" |
27 #include "base/single_thread_task_runner.h" | 27 #include "base/single_thread_task_runner.h" |
28 #include "base/strings/stringprintf.h" | 28 #include "base/strings/stringprintf.h" |
29 #include "base/synchronization/waitable_event.h" | 29 #include "base/synchronization/waitable_event.h" |
30 #include "base/test/test_timeouts.h" | 30 #include "base/test/test_timeouts.h" |
31 #include "base/test/thread_test_helper.h" | 31 #include "base/test/thread_test_helper.h" |
32 #include "base/threading/thread.h" | 32 #include "base/threading/thread.h" |
33 #include "build/build_config.h" | 33 #include "build/build_config.h" |
34 #include "chrome/common/chrome_constants.h" | 34 #include "chrome/common/chrome_constants.h" |
35 #include "content/public/test/test_browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 36 #include "content/public/test/test_browser_thread_bundle.h" |
36 #include "net/base/network_interfaces.h" | 37 #include "net/base/network_interfaces.h" |
37 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
38 | 39 |
39 using content::BrowserThread; | 40 using content::BrowserThread; |
40 | 41 |
41 namespace { | 42 namespace { |
42 | 43 |
43 class ProcessSingletonPosixTest : public testing::Test { | 44 class ProcessSingletonPosixTest : public testing::Test { |
44 public: | 45 public: |
45 // A ProcessSingleton exposing some protected methods for testing. | 46 // A ProcessSingleton exposing some protected methods for testing. |
(...skipping 15 matching lines...) Expand all Loading... |
61 private: | 62 private: |
62 bool NotificationCallback(const base::CommandLine& command_line, | 63 bool NotificationCallback(const base::CommandLine& command_line, |
63 const base::FilePath& current_directory) { | 64 const base::FilePath& current_directory) { |
64 callback_command_lines_.push_back(command_line.argv()); | 65 callback_command_lines_.push_back(command_line.argv()); |
65 return true; | 66 return true; |
66 } | 67 } |
67 }; | 68 }; |
68 | 69 |
69 ProcessSingletonPosixTest() | 70 ProcessSingletonPosixTest() |
70 : kill_callbacks_(0), | 71 : kill_callbacks_(0), |
71 io_thread_(BrowserThread::IO), | 72 test_browser_thread_bundle_( |
| 73 content::TestBrowserThreadBundle::REAL_IO_THREAD), |
72 wait_event_(base::WaitableEvent::ResetPolicy::MANUAL, | 74 wait_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
73 base::WaitableEvent::InitialState::NOT_SIGNALED), | 75 base::WaitableEvent::InitialState::NOT_SIGNALED), |
74 signal_event_(base::WaitableEvent::ResetPolicy::MANUAL, | 76 signal_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
75 base::WaitableEvent::InitialState::NOT_SIGNALED), | 77 base::WaitableEvent::InitialState::NOT_SIGNALED), |
76 process_singleton_on_thread_(NULL) { | 78 process_singleton_on_thread_(NULL) {} |
77 io_thread_.StartIOThread(); | |
78 } | |
79 | 79 |
80 void SetUp() override { | 80 void SetUp() override { |
81 testing::Test::SetUp(); | 81 testing::Test::SetUp(); |
82 | 82 |
83 ProcessSingleton::DisablePromptForTesting(); | 83 ProcessSingleton::DisablePromptForTesting(); |
84 // Put the lock in a temporary directory. Doesn't need to be a | 84 // Put the lock in a temporary directory. Doesn't need to be a |
85 // full profile to test this code. | 85 // full profile to test this code. |
86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
87 // Use a long directory name to ensure that the socket isn't opened through | 87 // Use a long directory name to ensure that the socket isn't opened through |
88 // the symlink. | 88 // the symlink. |
(...skipping 17 matching lines...) Expand all Loading... |
106 worker_thread_->task_runner()->PostTask( | 106 worker_thread_->task_runner()->PostTask( |
107 FROM_HERE, | 107 FROM_HERE, |
108 base::BindOnce(&ProcessSingletonPosixTest::DestructProcessSingleton, | 108 base::BindOnce(&ProcessSingletonPosixTest::DestructProcessSingleton, |
109 base::Unretained(this))); | 109 base::Unretained(this))); |
110 | 110 |
111 scoped_refptr<base::ThreadTestHelper> helper( | 111 scoped_refptr<base::ThreadTestHelper> helper( |
112 new base::ThreadTestHelper(worker_thread_->task_runner().get())); | 112 new base::ThreadTestHelper(worker_thread_->task_runner().get())); |
113 ASSERT_TRUE(helper->Run()); | 113 ASSERT_TRUE(helper->Run()); |
114 } | 114 } |
115 | 115 |
116 io_thread_.Stop(); | |
117 testing::Test::TearDown(); | 116 testing::Test::TearDown(); |
118 } | 117 } |
119 | 118 |
120 void CreateProcessSingletonOnThread() { | 119 void CreateProcessSingletonOnThread() { |
121 ASSERT_EQ(NULL, worker_thread_.get()); | 120 ASSERT_EQ(NULL, worker_thread_.get()); |
122 worker_thread_.reset(new base::Thread("BlockingThread")); | 121 worker_thread_.reset(new base::Thread("BlockingThread")); |
123 worker_thread_->Start(); | 122 worker_thread_->Start(); |
124 | 123 |
125 worker_thread_->task_runner()->PostTask( | 124 worker_thread_->task_runner()->PostTask( |
126 FROM_HERE, | 125 FROM_HERE, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 250 |
252 void DestructProcessSingleton() { | 251 void DestructProcessSingleton() { |
253 ASSERT_TRUE(process_singleton_on_thread_); | 252 ASSERT_TRUE(process_singleton_on_thread_); |
254 delete process_singleton_on_thread_; | 253 delete process_singleton_on_thread_; |
255 } | 254 } |
256 | 255 |
257 void KillCallback(int pid) { | 256 void KillCallback(int pid) { |
258 kill_callbacks_++; | 257 kill_callbacks_++; |
259 } | 258 } |
260 | 259 |
261 base::MessageLoop message_loop_; | 260 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
262 content::TestBrowserThread io_thread_; | |
263 base::ScopedTempDir temp_dir_; | 261 base::ScopedTempDir temp_dir_; |
264 base::WaitableEvent wait_event_; | 262 base::WaitableEvent wait_event_; |
265 base::WaitableEvent signal_event_; | 263 base::WaitableEvent signal_event_; |
266 | 264 |
267 std::unique_ptr<base::Thread> worker_thread_; | 265 std::unique_ptr<base::Thread> worker_thread_; |
268 TestableProcessSingleton* process_singleton_on_thread_; | 266 TestableProcessSingleton* process_singleton_on_thread_; |
269 }; | 267 }; |
270 | 268 |
271 } // namespace | 269 } // namespace |
272 | 270 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 // Test that if there is an existing lock file, and it's not locked, we replace | 444 // Test that if there is an existing lock file, and it's not locked, we replace |
447 // it. | 445 // it. |
448 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { | 446 TEST_F(ProcessSingletonPosixTest, CreateReplacesOldMacLock) { |
449 std::unique_ptr<TestableProcessSingleton> process_singleton( | 447 std::unique_ptr<TestableProcessSingleton> process_singleton( |
450 CreateProcessSingleton()); | 448 CreateProcessSingleton()); |
451 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); | 449 EXPECT_EQ(0, base::WriteFile(lock_path_, "", 0)); |
452 EXPECT_TRUE(process_singleton->Create()); | 450 EXPECT_TRUE(process_singleton->Create()); |
453 VerifyFiles(); | 451 VerifyFiles(); |
454 } | 452 } |
455 #endif // defined(OS_MACOSX) | 453 #endif // defined(OS_MACOSX) |
OLD | NEW |