forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_loop_io_posix_unittest.cc
417 lines (331 loc) · 13.1 KB
/
message_loop_io_posix_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop/message_loop.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/posix/eintr_wrapper.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
#if !defined(OS_NACL)
namespace {
class MessageLoopForIoPosixTest : public testing::Test {
public:
MessageLoopForIoPosixTest() = default;
// testing::Test interface.
void SetUp() override {
// Create a file descriptor. Doesn't need to be readable or writable,
// as we don't need to actually get any notifications.
// pipe() is just the easiest way to do it.
int pipefds[2];
int err = pipe(pipefds);
ASSERT_EQ(0, err);
read_fd_ = ScopedFD(pipefds[0]);
write_fd_ = ScopedFD(pipefds[1]);
}
void TriggerReadEvent() {
// Write from the other end of the pipe to trigger the event.
char c = '\0';
EXPECT_EQ(1, HANDLE_EINTR(write(write_fd_.get(), &c, 1)));
}
protected:
ScopedFD read_fd_;
ScopedFD write_fd_;
DISALLOW_COPY_AND_ASSIGN(MessageLoopForIoPosixTest);
};
class TestHandler : public MessageLoopForIO::Watcher {
public:
void OnFileCanReadWithoutBlocking(int fd) override {
watcher_to_delete_ = nullptr;
is_readable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
}
void OnFileCanWriteWithoutBlocking(int fd) override {
watcher_to_delete_ = nullptr;
is_writable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
}
bool is_readable_ = false;
bool is_writable_ = false;
// If set then the contained watcher will be deleted on notification.
std::unique_ptr<MessageLoopForIO::FileDescriptorWatcher> watcher_to_delete_;
};
// Watcher that calls specified closures when read/write events occur. Verifies
// that each non-null closure passed to this class is called once and only once.
// Also resets the read event by reading from the FD.
class CallClosureHandler : public MessageLoopForIO::Watcher {
public:
CallClosureHandler(OnceClosure read_closure, OnceClosure write_closure)
: read_closure_(std::move(read_closure)),
write_closure_(std::move(write_closure)) {}
~CallClosureHandler() override {
EXPECT_TRUE(read_closure_.is_null());
EXPECT_TRUE(write_closure_.is_null());
}
void SetReadClosure(OnceClosure read_closure) {
EXPECT_TRUE(read_closure_.is_null());
read_closure_ = std::move(read_closure);
}
void SetWriteClosure(OnceClosure write_closure) {
EXPECT_TRUE(write_closure_.is_null());
write_closure_ = std::move(write_closure);
}
// base:MessagePumpFuchsia::Watcher interface.
void OnFileCanReadWithoutBlocking(int fd) override {
// Empty the pipe buffer to reset the event. Otherwise libevent
// implementation of MessageLoop may call the event handler again even if
// |read_closure_| below quits the RunLoop.
char c;
int result = HANDLE_EINTR(read(fd, &c, 1));
if (result == -1) {
PLOG(ERROR) << "read";
FAIL();
}
EXPECT_EQ(result, 1);
ASSERT_FALSE(read_closure_.is_null());
std::move(read_closure_).Run();
}
void OnFileCanWriteWithoutBlocking(int fd) override {
ASSERT_FALSE(write_closure_.is_null());
std::move(write_closure_).Run();
}
private:
OnceClosure read_closure_;
OnceClosure write_closure_;
};
TEST_F(MessageLoopForIoPosixTest, FileDescriptorWatcherOutlivesMessageLoop) {
// Simulate a MessageLoop that dies before an FileDescriptorWatcher.
// This could happen when people use the Singleton pattern or atexit.
// Arrange for watcher to live longer than message loop.
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
TestHandler handler;
{
MessageLoopForIO message_loop;
message_loop.WatchFileDescriptor(write_fd_.get(), true,
MessageLoopForIO::WATCH_WRITE, &watcher,
&handler);
// Don't run the message loop, just destroy it.
}
ASSERT_FALSE(handler.is_readable_);
ASSERT_FALSE(handler.is_writable_);
}
TEST_F(MessageLoopForIoPosixTest, FileDescriptorWatcherDoubleStop) {
// Verify that it's ok to call StopWatchingFileDescriptor().
// Arrange for message loop to live longer than watcher.
MessageLoopForIO message_loop;
{
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
TestHandler handler;
message_loop.WatchFileDescriptor(write_fd_.get(), true,
MessageLoopForIO::WATCH_WRITE, &watcher,
&handler);
ASSERT_TRUE(watcher.StopWatchingFileDescriptor());
ASSERT_TRUE(watcher.StopWatchingFileDescriptor());
}
}
TEST_F(MessageLoopForIoPosixTest, FileDescriptorWatcherDeleteInCallback) {
// Verify that it is OK to delete the FileDescriptorWatcher from within a
// callback.
MessageLoopForIO message_loop;
TestHandler handler;
handler.watcher_to_delete_ =
std::make_unique<MessageLoopForIO::FileDescriptorWatcher>(FROM_HERE);
message_loop.WatchFileDescriptor(write_fd_.get(), true,
MessageLoopForIO::WATCH_WRITE,
handler.watcher_to_delete_.get(), &handler);
RunLoop().Run();
}
// Verify that basic readable notification works.
TEST_F(MessageLoopForIoPosixTest, WatchReadable) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
TestHandler handler;
// Watch the pipe for readability.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
// The pipe should not be readable when first created.
RunLoop().RunUntilIdle();
ASSERT_FALSE(handler.is_readable_);
ASSERT_FALSE(handler.is_writable_);
TriggerReadEvent();
// We don't want to assume that the read fd becomes readable the
// instant a bytes is written, so Run until quit by an event.
RunLoop().Run();
ASSERT_TRUE(handler.is_readable_);
ASSERT_FALSE(handler.is_writable_);
}
// Verify that watching a file descriptor for writability succeeds.
TEST_F(MessageLoopForIoPosixTest, WatchWritable) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
TestHandler handler;
// Watch the pipe for writability.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
write_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_WRITE,
&watcher, &handler));
// We should not receive a writable notification until we process events.
ASSERT_FALSE(handler.is_readable_);
ASSERT_FALSE(handler.is_writable_);
// The pipe should be writable immediately, but wait for the quit closure
// anyway, to be sure.
RunLoop().Run();
ASSERT_FALSE(handler.is_readable_);
ASSERT_TRUE(handler.is_writable_);
}
// Verify that RunUntilIdle() receives IO notifications.
TEST_F(MessageLoopForIoPosixTest, RunUntilIdle) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
TestHandler handler;
// Watch the pipe for readability.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
// The pipe should not be readable when first created.
RunLoop().RunUntilIdle();
ASSERT_FALSE(handler.is_readable_);
TriggerReadEvent();
while (!handler.is_readable_)
RunLoop().RunUntilIdle();
}
void StopWatching(MessageLoopForIO::FileDescriptorWatcher* controller,
RunLoop* run_loop) {
controller->StopWatchingFileDescriptor();
run_loop->Quit();
}
// Verify that StopWatchingFileDescriptor() works from an event handler.
TEST_F(MessageLoopForIoPosixTest, StopFromHandler) {
MessageLoopForIO message_loop;
RunLoop run_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
CallClosureHandler handler(BindOnce(&StopWatching, &watcher, &run_loop),
OnceClosure());
// Create persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/true, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
TriggerReadEvent();
run_loop.Run();
// Trigger the event again. The event handler should not be called again.
TriggerReadEvent();
RunLoop().RunUntilIdle();
}
// Verify that non-persistent watcher is called only once.
TEST_F(MessageLoopForIoPosixTest, NonPersistentWatcher) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
RunLoop run_loop;
CallClosureHandler handler(run_loop.QuitClosure(), OnceClosure());
// Create a non-persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
TriggerReadEvent();
run_loop.Run();
// Trigger the event again. handler should not be called again.
TriggerReadEvent();
RunLoop().RunUntilIdle();
}
// Verify that persistent watcher is called every time the event is triggered.
TEST_F(MessageLoopForIoPosixTest, PersistentWatcher) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
RunLoop run_loop1;
CallClosureHandler handler(run_loop1.QuitClosure(), OnceClosure());
// Create persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/true, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
TriggerReadEvent();
run_loop1.Run();
RunLoop run_loop2;
handler.SetReadClosure(run_loop2.QuitClosure());
// Trigger the event again. handler should be called now, which will quit
// run_loop2.
TriggerReadEvent();
run_loop2.Run();
}
void StopWatchingAndWatchAgain(
MessageLoopForIO::FileDescriptorWatcher* controller,
int fd,
MessageLoopForIO::Watcher* new_handler,
RunLoop* run_loop) {
controller->StopWatchingFileDescriptor();
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
fd, /*persistent=*/true, MessageLoopForIO::WATCH_READ, controller,
new_handler));
run_loop->Quit();
}
// Verify that a watcher can be stopped and reused from an event handler.
TEST_F(MessageLoopForIoPosixTest, StopAndRestartFromHandler) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
RunLoop run_loop1;
RunLoop run_loop2;
CallClosureHandler handler2(run_loop2.QuitClosure(), OnceClosure());
CallClosureHandler handler1(BindOnce(&StopWatchingAndWatchAgain, &watcher,
read_fd_.get(), &handler2, &run_loop1),
OnceClosure());
// Create persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/true, MessageLoopForIO::WATCH_READ,
&watcher, &handler1));
TriggerReadEvent();
run_loop1.Run();
// Trigger the event again. handler2 should be called now, which will quit
// run_loop2
TriggerReadEvent();
run_loop2.Run();
}
// Verify that the pump properly handles a delayed task after an IO event.
TEST_F(MessageLoopForIoPosixTest, IoEventThenTimer) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
RunLoop timer_run_loop;
message_loop.task_runner()->PostDelayedTask(
FROM_HERE, timer_run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(10));
RunLoop watcher_run_loop;
CallClosureHandler handler(watcher_run_loop.QuitClosure(), OnceClosure());
// Create a non-persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
TriggerReadEvent();
// Normally the IO event will be received before the delayed task is
// executed, so this run loop will first handle the IO event and then quit on
// the timer.
timer_run_loop.Run();
// Run watcher_run_loop in case the IO event wasn't received before the
// delayed task.
watcher_run_loop.Run();
}
// Verify that the pipe can handle an IO event after a delayed task.
TEST_F(MessageLoopForIoPosixTest, TimerThenIoEvent) {
MessageLoopForIO message_loop;
MessageLoopForIO::FileDescriptorWatcher watcher(FROM_HERE);
// Trigger read event from a delayed task.
message_loop.task_runner()->PostDelayedTask(
FROM_HERE,
BindOnce(&MessageLoopForIoPosixTest::TriggerReadEvent, Unretained(this)),
TimeDelta::FromMilliseconds(1));
RunLoop run_loop;
CallClosureHandler handler(run_loop.QuitClosure(), OnceClosure());
// Create a non-persistent watcher.
ASSERT_TRUE(MessageLoopForIO::current()->WatchFileDescriptor(
read_fd_.get(), /*persistent=*/false, MessageLoopForIO::WATCH_READ,
&watcher, &handler));
run_loop.Run();
}
} // namespace
#endif // !defined(OS_NACL)
} // namespace base