[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / base / message_pump_libevent_unittest.cc
blobe48a9c53090429921294408e2c4ea2efe7d15084
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_pump_libevent.h"
7 #include <unistd.h>
9 #include "base/message_loop.h"
10 #include "base/posix/eintr_wrapper.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(USE_SYSTEM_LIBEVENT)
15 #include <event.h>
16 #else
17 #include "third_party/libevent/event.h"
18 #endif
20 namespace base {
22 class MessagePumpLibeventTest : public testing::Test {
23 protected:
24 MessagePumpLibeventTest()
25 : ui_loop_(MessageLoop::TYPE_UI),
26 io_thread_("MessagePumpLibeventTestIOThread") {}
27 virtual ~MessagePumpLibeventTest() {}
29 virtual void SetUp() OVERRIDE {
30 Thread::Options options(MessageLoop::TYPE_IO, 0);
31 ASSERT_TRUE(io_thread_.StartWithOptions(options));
32 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
33 int ret = pipe(pipefds_);
34 ASSERT_EQ(0, ret);
37 virtual void TearDown() OVERRIDE {
38 if (HANDLE_EINTR(close(pipefds_[0])) < 0)
39 PLOG(ERROR) << "close";
40 if (HANDLE_EINTR(close(pipefds_[1])) < 0)
41 PLOG(ERROR) << "close";
44 MessageLoop* ui_loop() { return &ui_loop_; }
45 MessageLoopForIO* io_loop() const {
46 return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
49 void OnLibeventNotification(
50 MessagePumpLibevent* pump,
51 MessagePumpLibevent::FileDescriptorWatcher* controller) {
52 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
55 int pipefds_[2];
57 private:
58 MessageLoop ui_loop_;
59 Thread io_thread_;
62 namespace {
64 // Concrete implementation of MessagePumpLibevent::Watcher that does
65 // nothing useful.
66 class StupidWatcher : public MessagePumpLibevent::Watcher {
67 public:
68 virtual ~StupidWatcher() {}
70 // base:MessagePumpLibevent::Watcher interface
71 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {}
72 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
75 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
77 // Test to make sure that we catch calling WatchFileDescriptor off of the
78 // wrong thread.
79 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) {
80 MessagePumpLibevent::FileDescriptorWatcher watcher;
81 StupidWatcher delegate;
83 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
84 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
85 "Check failed: "
86 "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
89 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
91 class BaseWatcher : public MessagePumpLibevent::Watcher {
92 public:
93 BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller)
94 : controller_(controller) {
95 DCHECK(controller_);
97 virtual ~BaseWatcher() {}
99 // base:MessagePumpLibevent::Watcher interface
100 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {
101 NOTREACHED();
104 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
105 NOTREACHED();
108 protected:
109 MessagePumpLibevent::FileDescriptorWatcher* controller_;
112 class DeleteWatcher : public BaseWatcher {
113 public:
114 explicit DeleteWatcher(
115 MessagePumpLibevent::FileDescriptorWatcher* controller)
116 : BaseWatcher(controller) {}
118 virtual ~DeleteWatcher() {
119 DCHECK(!controller_);
122 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
123 DCHECK(controller_);
124 delete controller_;
125 controller_ = NULL;
129 TEST_F(MessagePumpLibeventTest, DeleteWatcher) {
130 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
131 MessagePumpLibevent::FileDescriptorWatcher* watcher =
132 new MessagePumpLibevent::FileDescriptorWatcher;
133 DeleteWatcher delegate(watcher);
134 pump->WatchFileDescriptor(pipefds_[1],
135 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate);
137 // Spoof a libevent notification.
138 OnLibeventNotification(pump, watcher);
141 class StopWatcher : public BaseWatcher {
142 public:
143 explicit StopWatcher(
144 MessagePumpLibevent::FileDescriptorWatcher* controller)
145 : BaseWatcher(controller) {}
147 virtual ~StopWatcher() {}
149 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
150 controller_->StopWatchingFileDescriptor();
154 TEST_F(MessagePumpLibeventTest, StopWatcher) {
155 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
156 MessagePumpLibevent::FileDescriptorWatcher watcher;
157 StopWatcher delegate(&watcher);
158 pump->WatchFileDescriptor(pipefds_[1],
159 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate);
161 // Spoof a libevent notification.
162 OnLibeventNotification(pump, &watcher);
165 } // namespace
167 } // namespace base