Remove APIPermission::kFileSystemWriteDirectory
[chromium-blink-merge.git] / base / message_loop / message_pump_libevent_unittest.cc
blobe911905abd7b489b47254a7195f6315701b422e9
1 // Copyright (c) 2012 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.
5 #include "base/message_loop/message_pump_libevent.h"
7 #include <unistd.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "base/run_loop.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/synchronization/waitable_event_watcher.h"
18 #include "base/threading/thread.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/libevent/event.h"
22 namespace base {
24 class MessagePumpLibeventTest : public testing::Test {
25 protected:
26 MessagePumpLibeventTest()
27 : ui_loop_(new MessageLoop(MessageLoop::TYPE_UI)),
28 io_thread_("MessagePumpLibeventTestIOThread") {}
29 ~MessagePumpLibeventTest() override {}
31 void SetUp() override {
32 Thread::Options options(MessageLoop::TYPE_IO, 0);
33 ASSERT_TRUE(io_thread_.StartWithOptions(options));
34 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
35 int ret = pipe(pipefds_);
36 ASSERT_EQ(0, ret);
39 void TearDown() override {
40 if (IGNORE_EINTR(close(pipefds_[0])) < 0)
41 PLOG(ERROR) << "close";
42 if (IGNORE_EINTR(close(pipefds_[1])) < 0)
43 PLOG(ERROR) << "close";
46 MessageLoopForIO* io_loop() const {
47 return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
50 void OnLibeventNotification(
51 MessagePumpLibevent* pump,
52 MessagePumpLibevent::FileDescriptorWatcher* controller) {
53 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
56 int pipefds_[2];
57 scoped_ptr<MessageLoop> ui_loop_;
59 private:
60 Thread io_thread_;
63 namespace {
65 // Concrete implementation of MessagePumpLibevent::Watcher that does
66 // nothing useful.
67 class StupidWatcher : public MessagePumpLibevent::Watcher {
68 public:
69 ~StupidWatcher() override {}
71 // base:MessagePumpLibevent::Watcher interface
72 void OnFileCanReadWithoutBlocking(int fd) override {}
73 void OnFileCanWriteWithoutBlocking(int fd) override {}
76 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
78 // Test to make sure that we catch calling WatchFileDescriptor off of the
79 // wrong thread.
80 #if defined(OS_CHROMEOS) || defined(OS_LINUX)
81 // Flaky on Chrome OS and Linux: crbug.com/138845.
82 #define MAYBE_TestWatchingFromBadThread DISABLED_TestWatchingFromBadThread
83 #else
84 #define MAYBE_TestWatchingFromBadThread TestWatchingFromBadThread
85 #endif
86 TEST_F(MessagePumpLibeventTest, MAYBE_TestWatchingFromBadThread) {
87 MessagePumpLibevent::FileDescriptorWatcher watcher;
88 StupidWatcher delegate;
90 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
91 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
92 "Check failed: "
93 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)");
96 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) {
97 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
98 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. "
99 "Quit was called outside of Run!");
102 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
104 class BaseWatcher : public MessagePumpLibevent::Watcher {
105 public:
106 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller)
107 : controller_(controller) {
108 DCHECK(controller_);
110 ~BaseWatcher() override {}
112 // base:MessagePumpLibevent::Watcher interface
113 void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); }
115 void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); }
117 protected:
118 MessagePumpLibevent::FileDescriptorWatcher* controller_;
121 class DeleteWatcher : public BaseWatcher {
122 public:
123 explicit DeleteWatcher(
124 MessagePumpLibevent::FileDescriptorWatcher* controller)
125 : BaseWatcher(controller) {}
127 ~DeleteWatcher() override { DCHECK(!controller_); }
129 void OnFileCanWriteWithoutBlocking(int /* fd */) override {
130 DCHECK(controller_);
131 delete controller_;
132 controller_ = NULL;
136 TEST_F(MessagePumpLibeventTest, DeleteWatcher) {
137 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
138 MessagePumpLibevent::FileDescriptorWatcher* watcher =
139 new MessagePumpLibevent::FileDescriptorWatcher;
140 DeleteWatcher delegate(watcher);
141 pump->WatchFileDescriptor(pipefds_[1],
142 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate);
144 // Spoof a libevent notification.
145 OnLibeventNotification(pump.get(), watcher);
148 class StopWatcher : public BaseWatcher {
149 public:
150 explicit StopWatcher(
151 MessagePumpLibevent::FileDescriptorWatcher* controller)
152 : BaseWatcher(controller) {}
154 ~StopWatcher() override {}
156 void OnFileCanWriteWithoutBlocking(int /* fd */) override {
157 controller_->StopWatchingFileDescriptor();
161 TEST_F(MessagePumpLibeventTest, StopWatcher) {
162 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
163 MessagePumpLibevent::FileDescriptorWatcher watcher;
164 StopWatcher delegate(&watcher);
165 pump->WatchFileDescriptor(pipefds_[1],
166 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate);
168 // Spoof a libevent notification.
169 OnLibeventNotification(pump.get(), &watcher);
172 void QuitMessageLoopAndStart(const Closure& quit_closure) {
173 quit_closure.Run();
175 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
176 RunLoop runloop;
177 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure());
178 runloop.Run();
181 class NestedPumpWatcher : public MessagePumpLibevent::Watcher {
182 public:
183 NestedPumpWatcher() {}
184 ~NestedPumpWatcher() override {}
186 void OnFileCanReadWithoutBlocking(int /* fd */) override {
187 RunLoop runloop;
188 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart,
189 runloop.QuitClosure()));
190 runloop.Run();
193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
196 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) {
197 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
198 MessagePumpLibevent::FileDescriptorWatcher watcher;
199 NestedPumpWatcher delegate;
200 pump->WatchFileDescriptor(pipefds_[1],
201 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate);
203 // Spoof a libevent notification.
204 OnLibeventNotification(pump.get(), &watcher);
207 void FatalClosure() {
208 FAIL() << "Reached fatal closure.";
211 class QuitWatcher : public BaseWatcher {
212 public:
213 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller,
214 RunLoop* run_loop)
215 : BaseWatcher(controller), run_loop_(run_loop) {}
216 ~QuitWatcher() override {}
218 void OnFileCanReadWithoutBlocking(int /* fd */) override {
219 // Post a fatal closure to the MessageLoop before we quit it.
220 MessageLoop::current()->PostTask(FROM_HERE, Bind(&FatalClosure));
222 // Now quit the MessageLoop.
223 run_loop_->Quit();
226 private:
227 RunLoop* run_loop_; // weak
230 void WriteFDWrapper(const int fd,
231 const char* buf,
232 int size,
233 WaitableEvent* event) {
234 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size));
237 // Tests that MessagePumpLibevent quits immediately when it is quit from
238 // libevent's event_base_loop().
239 TEST_F(MessagePumpLibeventTest, QuitWatcher) {
240 // Delete the old MessageLoop so that we can manage our own one here.
241 ui_loop_.reset();
243 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|.
244 MessageLoop loop(make_scoped_ptr(pump));
245 RunLoop run_loop;
246 MessagePumpLibevent::FileDescriptorWatcher controller;
247 QuitWatcher delegate(&controller, &run_loop);
248 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */);
249 scoped_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher);
251 // Tell the pump to watch the pipe.
252 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ,
253 &controller, &delegate);
255 // Make the IO thread wait for |event| before writing to pipefds[1].
256 const char buf = 0;
257 const WaitableEventWatcher::EventCallback write_fd_task =
258 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1);
259 io_loop()->PostTask(FROM_HERE,
260 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching),
261 Unretained(watcher.get()), &event, write_fd_task));
263 // Queue |event| to signal on |loop|.
264 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event)));
266 // Now run the MessageLoop.
267 run_loop.Run();
269 // StartWatching can move |watcher| to IO thread. Release on IO thread.
270 io_loop()->PostTask(FROM_HERE, Bind(&WaitableEventWatcher::StopWatching,
271 Owned(watcher.release())));
274 } // namespace
276 } // namespace base