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
base / message_loop / message_pump_io_ios_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// 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_pump_io_ios.h"
#include <unistd.h>
#include "base/logging.h"
#include "base/message_loop/message_pump_for_io.h"
#include "base/posix/eintr_wrapper.h"
#include "base/test/gtest_util.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class MessagePumpIOSForIOTest : public testing::Test {
public:
MessagePumpIOSForIOTest(const MessagePumpIOSForIOTest&) = delete;
MessagePumpIOSForIOTest& operator=(const MessagePumpIOSForIOTest&) = delete;
protected:
MessagePumpIOSForIOTest() = default;
~MessagePumpIOSForIOTest() override = default;
void SetUp() override {
int ret = pipe(pipefds_);
ASSERT_EQ(0, ret);
ret = pipe(alternate_pipefds_);
ASSERT_EQ(0, ret);
}
void TearDown() override {
if (IGNORE_EINTR(close(pipefds_[0])) < 0)
PLOG(ERROR) << "close";
if (IGNORE_EINTR(close(pipefds_[1])) < 0)
PLOG(ERROR) << "close";
}
void HandleFdIOEvent(MessagePumpForIO::FdWatchController* watcher) {
MessagePumpIOSForIO::HandleFdIOEvent(watcher->fdref_.get(),
kCFFileDescriptorReadCallBack | kCFFileDescriptorWriteCallBack,
watcher);
}
int pipefds_[2];
int alternate_pipefds_[2];
};
namespace {
class BaseWatcher : public MessagePumpIOSForIO::FdWatcher {
public:
BaseWatcher(MessagePumpIOSForIO::FdWatchController* controller)
: controller_(controller) {
DCHECK(controller_);
}
~BaseWatcher() override = default;
// MessagePumpIOSForIO::FdWatcher interface
void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); }
void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); }
protected:
MessagePumpIOSForIO::FdWatchController* controller_;
};
class DeleteWatcher : public BaseWatcher {
public:
explicit DeleteWatcher(MessagePumpIOSForIO::FdWatchController* controller)
: BaseWatcher(controller) {}
~DeleteWatcher() override { DCHECK(!controller_); }
void OnFileCanWriteWithoutBlocking(int /* fd */) override {
DCHECK(controller_);
delete controller_;
controller_ = NULL;
}
};
TEST_F(MessagePumpIOSForIOTest, DeleteWatcher) {
std::unique_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
MessagePumpIOSForIO::FdWatchController* watcher =
new MessagePumpIOSForIO::FdWatchController(FROM_HERE);
DeleteWatcher delegate(watcher);
pump->WatchFileDescriptor(pipefds_[1],
false, MessagePumpIOSForIO::WATCH_READ_WRITE, watcher, &delegate);
// Spoof a callback.
HandleFdIOEvent(watcher);
}
class StopWatcher : public BaseWatcher {
public:
StopWatcher(MessagePumpIOSForIO::FdWatchController* controller,
MessagePumpIOSForIO* pump,
int fd_to_start_watching = -1)
: BaseWatcher(controller),
pump_(pump),
fd_to_start_watching_(fd_to_start_watching) {}
~StopWatcher() override = default;
void OnFileCanWriteWithoutBlocking(int /* fd */) override {
controller_->StopWatchingFileDescriptor();
if (fd_to_start_watching_ >= 0) {
pump_->WatchFileDescriptor(fd_to_start_watching_,
false, MessagePumpIOSForIO::WATCH_READ_WRITE, controller_, this);
}
}
private:
MessagePumpIOSForIO* pump_;
int fd_to_start_watching_;
};
TEST_F(MessagePumpIOSForIOTest, StopWatcher) {
std::unique_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
MessagePumpIOSForIO::FdWatchController watcher(FROM_HERE);
StopWatcher delegate(&watcher, pump.get());
pump->WatchFileDescriptor(pipefds_[1],
false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate);
// Spoof a callback.
HandleFdIOEvent(&watcher);
}
TEST_F(MessagePumpIOSForIOTest, StopWatcherAndWatchSomethingElse) {
std::unique_ptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
MessagePumpIOSForIO::FdWatchController watcher(FROM_HERE);
StopWatcher delegate(&watcher, pump.get(), alternate_pipefds_[1]);
pump->WatchFileDescriptor(pipefds_[1],
false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate);
// Spoof a callback.
HandleFdIOEvent(&watcher);
}
} // namespace
} // namespace base