Beginnings of synchronizing notifications in the notification database.
[chromium-blink-merge.git] / base / message_loop / message_pump_libevent_unittest.cc
blob65d721727284f35cfdc3ad621c63ddb5fa51e9e7
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 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) {
81 MessagePumpLibevent::FileDescriptorWatcher watcher;
82 StupidWatcher delegate;
84 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
85 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
86 "Check failed: "
87 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)");
90 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) {
91 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
92 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. "
93 "Quit was called outside of Run!");
96 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
98 class BaseWatcher : public MessagePumpLibevent::Watcher {
99 public:
100 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller)
101 : controller_(controller) {
102 DCHECK(controller_);
104 ~BaseWatcher() override {}
106 // base:MessagePumpLibevent::Watcher interface
107 void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); }
109 void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); }
111 protected:
112 MessagePumpLibevent::FileDescriptorWatcher* controller_;
115 class DeleteWatcher : public BaseWatcher {
116 public:
117 explicit DeleteWatcher(
118 MessagePumpLibevent::FileDescriptorWatcher* controller)
119 : BaseWatcher(controller) {}
121 ~DeleteWatcher() override { DCHECK(!controller_); }
123 void OnFileCanWriteWithoutBlocking(int /* fd */) override {
124 DCHECK(controller_);
125 delete controller_;
126 controller_ = NULL;
130 TEST_F(MessagePumpLibeventTest, DeleteWatcher) {
131 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
132 MessagePumpLibevent::FileDescriptorWatcher* watcher =
133 new MessagePumpLibevent::FileDescriptorWatcher;
134 DeleteWatcher delegate(watcher);
135 pump->WatchFileDescriptor(pipefds_[1],
136 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate);
138 // Spoof a libevent notification.
139 OnLibeventNotification(pump.get(), watcher);
142 class StopWatcher : public BaseWatcher {
143 public:
144 explicit StopWatcher(
145 MessagePumpLibevent::FileDescriptorWatcher* controller)
146 : BaseWatcher(controller) {}
148 ~StopWatcher() override {}
150 void OnFileCanWriteWithoutBlocking(int /* fd */) override {
151 controller_->StopWatchingFileDescriptor();
155 TEST_F(MessagePumpLibeventTest, StopWatcher) {
156 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
157 MessagePumpLibevent::FileDescriptorWatcher watcher;
158 StopWatcher delegate(&watcher);
159 pump->WatchFileDescriptor(pipefds_[1],
160 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate);
162 // Spoof a libevent notification.
163 OnLibeventNotification(pump.get(), &watcher);
166 void QuitMessageLoopAndStart(const Closure& quit_closure) {
167 quit_closure.Run();
169 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
170 RunLoop runloop;
171 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure());
172 runloop.Run();
175 class NestedPumpWatcher : public MessagePumpLibevent::Watcher {
176 public:
177 NestedPumpWatcher() {}
178 ~NestedPumpWatcher() override {}
180 void OnFileCanReadWithoutBlocking(int /* fd */) override {
181 RunLoop runloop;
182 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart,
183 runloop.QuitClosure()));
184 runloop.Run();
187 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
190 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) {
191 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
192 MessagePumpLibevent::FileDescriptorWatcher watcher;
193 NestedPumpWatcher delegate;
194 pump->WatchFileDescriptor(pipefds_[1],
195 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate);
197 // Spoof a libevent notification.
198 OnLibeventNotification(pump.get(), &watcher);
201 void FatalClosure() {
202 FAIL() << "Reached fatal closure.";
205 class QuitWatcher : public BaseWatcher {
206 public:
207 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller,
208 RunLoop* run_loop)
209 : BaseWatcher(controller), run_loop_(run_loop) {}
210 ~QuitWatcher() override {}
212 void OnFileCanReadWithoutBlocking(int /* fd */) override {
213 // Post a fatal closure to the MessageLoop before we quit it.
214 MessageLoop::current()->PostTask(FROM_HERE, Bind(&FatalClosure));
216 // Now quit the MessageLoop.
217 run_loop_->Quit();
220 private:
221 RunLoop* run_loop_; // weak
224 void WriteFDWrapper(const int fd,
225 const char* buf,
226 int size,
227 WaitableEvent* event) {
228 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size));
231 // Tests that MessagePumpLibevent quits immediately when it is quit from
232 // libevent's event_base_loop().
233 TEST_F(MessagePumpLibeventTest, QuitWatcher) {
234 // Delete the old MessageLoop so that we can manage our own one here.
235 ui_loop_.reset();
237 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|.
238 MessageLoop loop(make_scoped_ptr(pump));
239 RunLoop run_loop;
240 MessagePumpLibevent::FileDescriptorWatcher controller;
241 QuitWatcher delegate(&controller, &run_loop);
242 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */);
243 WaitableEventWatcher watcher;
245 // Tell the pump to watch the pipe.
246 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ,
247 &controller, &delegate);
249 // Make the IO thread wait for |event| before writing to pipefds[1].
250 const char buf = 0;
251 const WaitableEventWatcher::EventCallback write_fd_task =
252 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1);
253 io_loop()->PostTask(FROM_HERE,
254 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching),
255 Unretained(&watcher), &event, write_fd_task));
257 // Queue |event| to signal on |loop|.
258 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event)));
260 // Now run the MessageLoop.
261 run_loop.Run();
264 } // namespace
266 } // namespace base