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_pump_libevent.h" | 5 #include "base/message_pump_libevent.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "base/eintr_wrapper.h" | |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 #if defined(USE_SYSTEM_LIBEVENT) | 14 #if defined(USE_SYSTEM_LIBEVENT) |
14 #include <event.h> | 15 #include <event.h> |
15 #else | 16 #else |
16 #include "third_party/libevent/event.h" | 17 #include "third_party/libevent/event.h" |
17 #endif | 18 #endif |
18 | 19 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 | 57 |
57 // base:MessagePumpLibevent::Watcher interface | 58 // base:MessagePumpLibevent::Watcher interface |
58 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {} | 59 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {} |
59 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} | 60 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
60 }; | 61 }; |
61 | 62 |
62 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 63 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
63 | 64 |
64 // Test to make sure that we catch calling WatchFileDescriptor off of the | 65 // Test to make sure that we catch calling WatchFileDescriptor off of the |
65 // wrong thread. | 66 // wrong thread. |
66 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { | 67 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) { |
jar (doing other things)
2012/07/09 21:12:23
Where is TestWatchingFromBadThread defined?
Shouqun Liu
2012/07/10 05:42:41
This is a death test, I think we don't need to def
jar (doing other things)
2012/07/10 17:02:10
<doh> For some reason I was confusing this test n
| |
67 MessagePumpLibevent::FileDescriptorWatcher watcher; | 68 MessagePumpLibevent::FileDescriptorWatcher watcher; |
68 StupidWatcher delegate; | 69 StupidWatcher delegate; |
69 | 70 |
70 ASSERT_DEATH(io_loop()->WatchFileDescriptor( | 71 ASSERT_DEATH(io_loop()->WatchFileDescriptor( |
71 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | 72 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), |
72 "Check failed: " | 73 "Check failed: " |
73 "watch_file_descriptor_caller_checker_.CalledOnValidThread()"); | 74 "watch_file_descriptor_caller_checker_.CalledOnValidThread()"); |
74 } | 75 } |
75 | 76 |
76 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 77 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
(...skipping 17 matching lines...) Expand all Loading... | |
94 DCHECK(controller_); | 95 DCHECK(controller_); |
95 delete controller_; | 96 delete controller_; |
96 controller_ = NULL; | 97 controller_ = NULL; |
97 } | 98 } |
98 | 99 |
99 private: | 100 private: |
100 MessagePumpLibevent::FileDescriptorWatcher* controller_; | 101 MessagePumpLibevent::FileDescriptorWatcher* controller_; |
101 }; | 102 }; |
102 | 103 |
103 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | 104 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { |
105 int pipefds[2]; | |
106 int err = pipe(pipefds); | |
107 ASSERT_EQ(0, err); | |
108 int fd = pipefds[1]; | |
104 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 109 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
105 MessagePumpLibevent::FileDescriptorWatcher* watcher = | 110 MessagePumpLibevent::FileDescriptorWatcher* watcher = |
106 new MessagePumpLibevent::FileDescriptorWatcher; | 111 new MessagePumpLibevent::FileDescriptorWatcher; |
107 DeleteWatcher delegate(watcher); | 112 DeleteWatcher delegate(watcher); |
108 pump->WatchFileDescriptor( | 113 pump->WatchFileDescriptor( |
109 0, false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | 114 fd, false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); |
110 | 115 |
111 // Spoof a libevent notification. | 116 // Spoof a libevent notification. |
112 OnLibeventNotification(pump, watcher); | 117 OnLibeventNotification(pump, watcher); |
118 if (HANDLE_EINTR(close(pipefds[0])) < 0) | |
119 PLOG(ERROR) << "close"; | |
120 if (HANDLE_EINTR(close(pipefds[1])) < 0) | |
121 PLOG(ERROR) << "close"; | |
113 } | 122 } |
114 | 123 |
115 class StopWatcher : public MessagePumpLibevent::Watcher { | 124 class StopWatcher : public MessagePumpLibevent::Watcher { |
116 public: | 125 public: |
117 explicit StopWatcher( | 126 explicit StopWatcher( |
118 MessagePumpLibevent::FileDescriptorWatcher* controller) | 127 MessagePumpLibevent::FileDescriptorWatcher* controller) |
119 : controller_(controller) { | 128 : controller_(controller) { |
120 DCHECK(controller_); | 129 DCHECK(controller_); |
121 } | 130 } |
122 virtual ~StopWatcher() {} | 131 virtual ~StopWatcher() {} |
123 | 132 |
124 // base:MessagePumpLibevent::Watcher interface | 133 // base:MessagePumpLibevent::Watcher interface |
125 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 134 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
126 NOTREACHED(); | 135 NOTREACHED(); |
127 } | 136 } |
128 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 137 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
129 controller_->StopWatchingFileDescriptor(); | 138 controller_->StopWatchingFileDescriptor(); |
130 } | 139 } |
131 | 140 |
132 private: | 141 private: |
133 MessagePumpLibevent::FileDescriptorWatcher* const controller_; | 142 MessagePumpLibevent::FileDescriptorWatcher* const controller_; |
134 }; | 143 }; |
135 | 144 |
136 TEST_F(MessagePumpLibeventTest, StopWatcher) { | 145 TEST_F(MessagePumpLibeventTest, StopWatcher) { |
146 int pipefds[2]; | |
147 int err = pipe(pipefds); | |
148 ASSERT_EQ(0, err); | |
149 int fd = pipefds[1]; | |
jar (doing other things)
2012/07/09 21:12:23
I hate seeing growth of replicated code...
WDYT
Shouqun Liu
2012/07/10 05:42:41
Yes, agree with you. I have made the following ch
| |
137 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 150 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
138 MessagePumpLibevent::FileDescriptorWatcher watcher; | 151 MessagePumpLibevent::FileDescriptorWatcher watcher; |
139 StopWatcher delegate(&watcher); | 152 StopWatcher delegate(&watcher); |
140 pump->WatchFileDescriptor( | 153 pump->WatchFileDescriptor( |
141 0, false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 154 fd, false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
142 | 155 |
143 // Spoof a libevent notification. | 156 // Spoof a libevent notification. |
144 OnLibeventNotification(pump, &watcher); | 157 OnLibeventNotification(pump, &watcher); |
158 if (HANDLE_EINTR(close(pipefds[0])) < 0) | |
159 PLOG(ERROR) << "close"; | |
160 if (HANDLE_EINTR(close(pipefds[1])) < 0) | |
161 PLOG(ERROR) << "close"; | |
145 } | 162 } |
146 | 163 |
147 } // namespace | 164 } // namespace |
148 | 165 |
149 } // namespace base | 166 } // namespace base |
OLD | NEW |