OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/message_loop/message_pump_libevent.h" | 5 #include "base/message_loop/message_pump_libevent.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/run_loop.h" |
11 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/libevent/event.h" | 15 #include "third_party/libevent/event.h" |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 | 18 |
17 class MessagePumpLibeventTest : public testing::Test { | 19 class MessagePumpLibeventTest : public testing::Test { |
18 protected: | 20 protected: |
19 MessagePumpLibeventTest() | 21 MessagePumpLibeventTest() |
20 : ui_loop_(MessageLoop::TYPE_UI), | 22 : ui_loop_(MessageLoop::TYPE_UI), |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { | 76 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { |
75 MessagePumpLibevent::FileDescriptorWatcher watcher; | 77 MessagePumpLibevent::FileDescriptorWatcher watcher; |
76 StupidWatcher delegate; | 78 StupidWatcher delegate; |
77 | 79 |
78 ASSERT_DEATH(io_loop()->WatchFileDescriptor( | 80 ASSERT_DEATH(io_loop()->WatchFileDescriptor( |
79 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | 81 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), |
80 "Check failed: " | 82 "Check failed: " |
81 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); | 83 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); |
82 } | 84 } |
83 | 85 |
| 86 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) { |
| 87 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
| 88 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. " |
| 89 "Quit was called outside of Run!"); |
| 90 } |
| 91 |
84 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 92 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
85 | 93 |
86 class BaseWatcher : public MessagePumpLibevent::Watcher { | 94 class BaseWatcher : public MessagePumpLibevent::Watcher { |
87 public: | 95 public: |
88 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) | 96 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) |
89 : controller_(controller) { | 97 : controller_(controller) { |
90 DCHECK(controller_); | 98 DCHECK(controller_); |
91 } | 99 } |
92 virtual ~BaseWatcher() {} | 100 virtual ~BaseWatcher() {} |
93 | 101 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 158 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
151 MessagePumpLibevent::FileDescriptorWatcher watcher; | 159 MessagePumpLibevent::FileDescriptorWatcher watcher; |
152 StopWatcher delegate(&watcher); | 160 StopWatcher delegate(&watcher); |
153 pump->WatchFileDescriptor(pipefds_[1], | 161 pump->WatchFileDescriptor(pipefds_[1], |
154 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 162 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
155 | 163 |
156 // Spoof a libevent notification. | 164 // Spoof a libevent notification. |
157 OnLibeventNotification(pump.get(), &watcher); | 165 OnLibeventNotification(pump.get(), &watcher); |
158 } | 166 } |
159 | 167 |
| 168 void QuitMessageLoopAndStart(const Closure& quit_closure) { |
| 169 quit_closure.Run(); |
| 170 |
| 171 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 172 RunLoop runloop; |
| 173 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure()); |
| 174 runloop.Run(); |
| 175 } |
| 176 |
| 177 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { |
| 178 public: |
| 179 NestedPumpWatcher() {} |
| 180 virtual ~NestedPumpWatcher() {} |
| 181 |
| 182 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
| 183 RunLoop runloop; |
| 184 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, |
| 185 runloop.QuitClosure())); |
| 186 runloop.Run(); |
| 187 } |
| 188 |
| 189 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} |
| 190 }; |
| 191 |
| 192 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { |
| 193 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
| 194 MessagePumpLibevent::FileDescriptorWatcher watcher; |
| 195 NestedPumpWatcher delegate; |
| 196 pump->WatchFileDescriptor(pipefds_[1], |
| 197 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); |
| 198 |
| 199 // Spoof a libevent notification. |
| 200 OnLibeventNotification(pump.get(), &watcher); |
| 201 } |
| 202 |
160 } // namespace | 203 } // namespace |
161 | 204 |
162 } // namespace base | 205 } // namespace base |
OLD | NEW |