Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: base/message_pump_io_ios_unittest.cc

Issue 11412101: Provide an iOS message pump for IO implementation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Response to reviews Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/message_pump_io_ios.cc ('k') | base/message_pump_libevent.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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_pump_libevent.h" 5 #include "base/message_pump_io_ios.h"
Mark Mentovai 2012/11/28 14:59:39 Now that I can see this file as a copy of the libe
blundell 2012/11/28 16:56:55 I will think about this, but would prefer to do th
6 6
7 #include <unistd.h> 7 #include <unistd.h>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/posix/eintr_wrapper.h" 10 #include "base/posix/eintr_wrapper.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 #if defined(USE_SYSTEM_LIBEVENT)
15 #include <event.h>
16 #else
17 #include "third_party/libevent/event.h"
18 #endif
19
20 namespace base { 14 namespace base {
21 15
22 class MessagePumpLibeventTest : public testing::Test { 16 class MessagePumpIOSForIOTest : public testing::Test {
23 protected: 17 protected:
24 MessagePumpLibeventTest() 18 MessagePumpIOSForIOTest()
25 : ui_loop_(MessageLoop::TYPE_UI), 19 : ui_loop_(MessageLoop::TYPE_UI),
26 io_thread_("MessagePumpLibeventTestIOThread") {} 20 io_thread_("MessagePumpIOSForIOTestIOThread") {}
27 virtual ~MessagePumpLibeventTest() {} 21 virtual ~MessagePumpIOSForIOTest() {}
28 22
29 virtual void SetUp() OVERRIDE { 23 virtual void SetUp() {
Mark Mentovai 2012/11/28 14:59:39 The libevent test still calls this an OVERRIDE.
blundell 2012/11/28 16:56:55 Done.
30 Thread::Options options(MessageLoop::TYPE_IO, 0); 24 Thread::Options options(MessageLoop::TYPE_IO, 0);
31 ASSERT_TRUE(io_thread_.StartWithOptions(options)); 25 ASSERT_TRUE(io_thread_.StartWithOptions(options));
32 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type()); 26 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
33 int err = pipe(pipefds_); 27 int ret = pipe(pipefds_);
34 ASSERT_EQ(0, err); 28 ASSERT_EQ(0, ret);
35 } 29 }
36 30
37 virtual void TearDown() OVERRIDE { 31 virtual void TearDown() OVERRIDE {
38 if (HANDLE_EINTR(close(pipefds_[0])) < 0) 32 if (HANDLE_EINTR(close(pipefds_[0])) < 0)
39 PLOG(ERROR) << "close"; 33 PLOG(ERROR) << "close";
40 if (HANDLE_EINTR(close(pipefds_[1])) < 0) 34 if (HANDLE_EINTR(close(pipefds_[1])) < 0)
41 PLOG(ERROR) << "close"; 35 PLOG(ERROR) << "close";
42 } 36 }
43 37
44 MessageLoop* ui_loop() { return &ui_loop_; } 38 MessageLoop* ui_loop() { return &ui_loop_; }
45 MessageLoopForIO* io_loop() const { 39 MessageLoopForIO* io_loop() const {
46 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); 40 return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
47 } 41 }
48 42
49 void OnLibeventNotification( 43 void SetWatcherDelegate(MessageLoopForIO::FileDescriptorWatcher* watcher,
50 MessagePumpLibevent* pump, 44 MessageLoopForIO::Watcher* delegate) {
51 MessagePumpLibevent::FileDescriptorWatcher* controller) { 45 watcher->set_watcher(delegate);
52 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
53 } 46 }
54 47
48 void HandleFdIOEvent(MessageLoopForIO::FileDescriptorWatcher* watcher) {
49 MessagePumpIOSForIO::HandleFdIOEvent(watcher->fdref_,
50 kCFFileDescriptorReadCallBack | kCFFileDescriptorWriteCallBack,
51 watcher);
52 }
53
54 int pipefds_[2];
55
56 private:
55 MessageLoop ui_loop_; 57 MessageLoop ui_loop_;
56 Thread io_thread_; 58 Thread io_thread_;
57 int pipefds_[2]; 59
60 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIOTest);
58 }; 61 };
59 62
60 namespace { 63 namespace {
61 64
62 // Concrete implementation of MessagePumpLibevent::Watcher that does 65 // Concrete implementation of MessagePumpIOSForIO::Watcher that does
63 // nothing useful. 66 // nothing useful.
64 class StupidWatcher : public MessagePumpLibevent::Watcher { 67 class StupidWatcher : public MessagePumpIOSForIO::Watcher {
65 public: 68 public:
66 virtual ~StupidWatcher() {} 69 virtual ~StupidWatcher() {}
67 70
68 // base:MessagePumpLibevent::Watcher interface 71 // base:MessagePumpIOSForIO::Watcher interface
69 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {} 72 virtual void OnFileCanReadWithoutBlocking(int fd) {}
70 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} 73 virtual void OnFileCanWriteWithoutBlocking(int fd) {}
71 }; 74 };
72 75
73 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) 76 #if GTEST_HAS_DEATH_TEST
74 77
75 // Test to make sure that we catch calling WatchFileDescriptor off of the 78 // Test to make sure that we catch calling WatchFileDescriptor off of the
76 // wrong thread. 79 // wrong thread.
77 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { 80 TEST_F(MessagePumpIOSForIOTest, TestWatchingFromBadThread) {
78 MessagePumpLibevent::FileDescriptorWatcher watcher; 81 MessagePumpIOSForIO::FileDescriptorWatcher watcher;
79 StupidWatcher delegate; 82 StupidWatcher delegate;
80 83
81 ASSERT_DEATH(io_loop()->WatchFileDescriptor( 84 ASSERT_DEBUG_DEATH(io_loop()->WatchFileDescriptor(
82 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), 85 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
83 "Check failed: " 86 "Check failed: "
84 "watch_file_descriptor_caller_checker_.CalledOnValidThread()"); 87 "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
85 } 88 }
86 89
87 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) 90 #endif // GTEST_HAS_DEATH_TEST
88 91
89 class BaseWatcher : public MessagePumpLibevent::Watcher { 92 class BaseWatcher : public MessagePumpIOSForIO::Watcher {
90 public: 93 public:
91 BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) 94 BaseWatcher(MessagePumpIOSForIO::FileDescriptorWatcher* controller)
92 : controller_(controller) { 95 : controller_(controller) {
93 DCHECK(controller_); 96 DCHECK(controller_);
94 } 97 }
95 virtual ~BaseWatcher() {} 98 virtual ~BaseWatcher() {}
96 99
97 // base:MessagePumpLibevent::Watcher interface 100 // MessagePumpIOSForIO::Watcher interface
98 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { 101 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {
99 NOTREACHED(); 102 NOTREACHED();
100 } 103 }
101 104
102 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { 105 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
103 NOTREACHED(); 106 NOTREACHED();
104 } 107 }
105 108
106 protected: 109 protected:
107 MessagePumpLibevent::FileDescriptorWatcher* controller_; 110 MessagePumpIOSForIO::FileDescriptorWatcher* controller_;
108 }; 111 };
109 112
110 class DeleteWatcher : public BaseWatcher { 113 class DeleteWatcher : public BaseWatcher {
111 public: 114 public:
112 explicit DeleteWatcher( 115 explicit DeleteWatcher(
113 MessagePumpLibevent::FileDescriptorWatcher* controller) 116 MessagePumpIOSForIO::FileDescriptorWatcher* controller)
114 : BaseWatcher(controller) {} 117 : BaseWatcher(controller) {}
115 118
116 virtual ~DeleteWatcher() { 119 virtual ~DeleteWatcher() {
117 DCHECK(!controller_); 120 DCHECK(!controller_);
118 } 121 }
119 122
120 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { 123 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
121 DCHECK(controller_); 124 DCHECK(controller_);
122 delete controller_; 125 delete controller_;
123 controller_ = NULL; 126 controller_ = NULL;
124 } 127 }
125 }; 128 };
126 129
127 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { 130 TEST_F(MessagePumpIOSForIOTest, DeleteWatcher) {
128 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); 131 scoped_refptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
129 MessagePumpLibevent::FileDescriptorWatcher* watcher = 132 MessagePumpIOSForIO::FileDescriptorWatcher* watcher =
130 new MessagePumpLibevent::FileDescriptorWatcher; 133 new MessagePumpIOSForIO::FileDescriptorWatcher;
131 DeleteWatcher delegate(watcher); 134 DeleteWatcher delegate(watcher);
135 SetWatcherDelegate(watcher, &delegate);
136
132 pump->WatchFileDescriptor(pipefds_[1], 137 pump->WatchFileDescriptor(pipefds_[1],
133 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); 138 false, MessagePumpIOSForIO::WATCH_READ_WRITE, watcher, &delegate);
134 139
135 // Spoof a libevent notification. 140 // Spoof a callback.
136 OnLibeventNotification(pump, watcher); 141 HandleFdIOEvent(watcher);
137 } 142 }
138 143
139 class StopWatcher : public BaseWatcher { 144 class StopWatcher : public BaseWatcher {
140 public: 145 public:
141 explicit StopWatcher( 146 explicit StopWatcher(
142 MessagePumpLibevent::FileDescriptorWatcher* controller) 147 MessagePumpIOSForIO::FileDescriptorWatcher* controller)
143 : BaseWatcher(controller) {} 148 : BaseWatcher(controller) {}
144 149
145 virtual ~StopWatcher() {} 150 virtual ~StopWatcher() {}
146 151
147 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { 152 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
148 controller_->StopWatchingFileDescriptor(); 153 controller_->StopWatchingFileDescriptor();
149 } 154 }
150 }; 155 };
151 156
152 TEST_F(MessagePumpLibeventTest, StopWatcher) { 157 TEST_F(MessagePumpIOSForIOTest, StopWatcher) {
153 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); 158 scoped_refptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
154 MessagePumpLibevent::FileDescriptorWatcher watcher; 159 MessagePumpIOSForIO::FileDescriptorWatcher watcher;
155 StopWatcher delegate(&watcher); 160 StopWatcher delegate(&watcher);
161 SetWatcherDelegate(&watcher, &delegate);
162
156 pump->WatchFileDescriptor(pipefds_[1], 163 pump->WatchFileDescriptor(pipefds_[1],
157 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); 164 false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate);
158 165
159 // Spoof a libevent notification. 166 // Spoof a callback.
160 OnLibeventNotification(pump, &watcher); 167 HandleFdIOEvent(&watcher);
161 } 168 }
162 169
163 } // namespace 170 } // namespace
164 171
165 } // namespace base 172 } // namespace base
OLDNEW
« no previous file with comments | « base/message_pump_io_ios.cc ('k') | base/message_pump_libevent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698