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

Side by Side Diff: mojo/public/cpp/system/tests/watcher_unittest.cc

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 years, 9 months 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "mojo/public/cpp/system/watcher.h" 5 #include "mojo/public/cpp/system/watcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 30 matching lines...) Expand all
41 41
42 DISALLOW_COPY_AND_ASSIGN(WatcherTest); 42 DISALLOW_COPY_AND_ASSIGN(WatcherTest);
43 }; 43 };
44 44
45 TEST_F(WatcherTest, WatchBasic) { 45 TEST_F(WatcherTest, WatchBasic) {
46 ScopedMessagePipeHandle a, b; 46 ScopedMessagePipeHandle a, b;
47 CreateMessagePipe(nullptr, &a, &b); 47 CreateMessagePipe(nullptr, &a, &b);
48 48
49 bool notified = false; 49 bool notified = false;
50 base::RunLoop run_loop; 50 base::RunLoop run_loop;
51 Watcher b_watcher(FROM_HERE); 51 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
52 EXPECT_EQ(MOJO_RESULT_OK, 52 EXPECT_EQ(MOJO_RESULT_OK,
53 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 53 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
54 OnReady([&] (MojoResult result) { 54 OnReady([&] (MojoResult result) {
55 EXPECT_EQ(MOJO_RESULT_OK, result); 55 EXPECT_EQ(MOJO_RESULT_OK, result);
56 notified = true; 56 notified = true;
57 run_loop.Quit(); 57 run_loop.Quit();
58 }))); 58 })));
59 EXPECT_TRUE(b_watcher.IsWatching()); 59 EXPECT_TRUE(b_watcher.IsWatching());
60 60
61 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 61 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
62 MOJO_WRITE_MESSAGE_FLAG_NONE)); 62 MOJO_WRITE_MESSAGE_FLAG_NONE));
63 run_loop.Run(); 63 run_loop.Run();
64 EXPECT_TRUE(notified); 64 EXPECT_TRUE(notified);
65 65
66 b_watcher.Cancel(); 66 b_watcher.Cancel();
67 } 67 }
68 68
69 TEST_F(WatcherTest, WatchUnsatisfiable) { 69 TEST_F(WatcherTest, WatchUnsatisfiable) {
70 ScopedMessagePipeHandle a, b; 70 ScopedMessagePipeHandle a, b;
71 CreateMessagePipe(nullptr, &a, &b); 71 CreateMessagePipe(nullptr, &a, &b);
72 a.reset(); 72 a.reset();
73 73
74 Watcher b_watcher(FROM_HERE); 74 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
75 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 75 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
76 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 76 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
77 NotReached())); 77 NotReached()));
78 EXPECT_FALSE(b_watcher.IsWatching()); 78 EXPECT_FALSE(b_watcher.IsWatching());
79 } 79 }
80 80
81 TEST_F(WatcherTest, WatchInvalidHandle) { 81 TEST_F(WatcherTest, WatchInvalidHandle) {
82 ScopedMessagePipeHandle a, b; 82 ScopedMessagePipeHandle a, b;
83 CreateMessagePipe(nullptr, &a, &b); 83 CreateMessagePipe(nullptr, &a, &b);
84 a.reset(); 84 a.reset();
85 b.reset(); 85 b.reset();
86 86
87 Watcher b_watcher(FROM_HERE); 87 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
88 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 88 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
89 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 89 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
90 NotReached())); 90 NotReached()));
91 EXPECT_FALSE(b_watcher.IsWatching()); 91 EXPECT_FALSE(b_watcher.IsWatching());
92 } 92 }
93 93
94 TEST_F(WatcherTest, Cancel) { 94 TEST_F(WatcherTest, Cancel) {
95 ScopedMessagePipeHandle a, b; 95 ScopedMessagePipeHandle a, b;
96 CreateMessagePipe(nullptr, &a, &b); 96 CreateMessagePipe(nullptr, &a, &b);
97 97
98 base::RunLoop run_loop; 98 base::RunLoop run_loop;
99 Watcher b_watcher(FROM_HERE); 99 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
100 EXPECT_EQ(MOJO_RESULT_OK, 100 EXPECT_EQ(MOJO_RESULT_OK,
101 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 101 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
102 NotReached())); 102 NotReached()));
103 EXPECT_TRUE(b_watcher.IsWatching()); 103 EXPECT_TRUE(b_watcher.IsWatching());
104 b_watcher.Cancel(); 104 b_watcher.Cancel();
105 EXPECT_FALSE(b_watcher.IsWatching()); 105 EXPECT_FALSE(b_watcher.IsWatching());
106 106
107 // This should never trigger the watcher. 107 // This should never trigger the watcher.
108 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 108 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
109 MOJO_WRITE_MESSAGE_FLAG_NONE)); 109 MOJO_WRITE_MESSAGE_FLAG_NONE));
110 110
111 base::ThreadTaskRunnerHandle::Get()->PostTask( 111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE, run_loop.QuitClosure()); 112 FROM_HERE, run_loop.QuitClosure());
113 run_loop.Run(); 113 run_loop.Run();
114 } 114 }
115 115
116 TEST_F(WatcherTest, CancelOnClose) { 116 TEST_F(WatcherTest, CancelOnClose) {
117 ScopedMessagePipeHandle a, b; 117 ScopedMessagePipeHandle a, b;
118 CreateMessagePipe(nullptr, &a, &b); 118 CreateMessagePipe(nullptr, &a, &b);
119 119
120 base::RunLoop run_loop; 120 base::RunLoop run_loop;
121 Watcher b_watcher(FROM_HERE); 121 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
122 EXPECT_EQ(MOJO_RESULT_OK, 122 EXPECT_EQ(MOJO_RESULT_OK,
123 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 123 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
124 OnReady([&] (MojoResult result) { 124 OnReady([&] (MojoResult result) {
125 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 125 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
126 run_loop.Quit(); 126 run_loop.Quit();
127 }))); 127 })));
128 EXPECT_TRUE(b_watcher.IsWatching()); 128 EXPECT_TRUE(b_watcher.IsWatching());
129 129
130 // This should trigger the watcher above. 130 // This should trigger the watcher above.
131 b.reset(); 131 b.reset();
132 132
133 run_loop.Run(); 133 run_loop.Run();
134 134
135 EXPECT_FALSE(b_watcher.IsWatching()); 135 EXPECT_FALSE(b_watcher.IsWatching());
136 } 136 }
137 137
138 TEST_F(WatcherTest, CancelOnDestruction) { 138 TEST_F(WatcherTest, CancelOnDestruction) {
139 ScopedMessagePipeHandle a, b; 139 ScopedMessagePipeHandle a, b;
140 CreateMessagePipe(nullptr, &a, &b); 140 CreateMessagePipe(nullptr, &a, &b);
141 base::RunLoop run_loop; 141 base::RunLoop run_loop;
142 { 142 {
143 Watcher b_watcher(FROM_HERE); 143 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
144 EXPECT_EQ(MOJO_RESULT_OK, 144 EXPECT_EQ(MOJO_RESULT_OK,
145 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 145 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
146 NotReached())); 146 NotReached()));
147 EXPECT_TRUE(b_watcher.IsWatching()); 147 EXPECT_TRUE(b_watcher.IsWatching());
148 148
149 // |b_watcher| should be cancelled when it goes out of scope. 149 // |b_watcher| should be cancelled when it goes out of scope.
150 } 150 }
151 151
152 // This should never trigger the watcher above. 152 // This should never trigger the watcher above.
153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
154 MOJO_WRITE_MESSAGE_FLAG_NONE)); 154 MOJO_WRITE_MESSAGE_FLAG_NONE));
155 base::ThreadTaskRunnerHandle::Get()->PostTask( 155 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, run_loop.QuitClosure()); 156 FROM_HERE, run_loop.QuitClosure());
157 run_loop.Run(); 157 run_loop.Run();
158 } 158 }
159 159
160 TEST_F(WatcherTest, CloseAndCancel) { 160 TEST_F(WatcherTest, CloseAndCancel) {
161 ScopedMessagePipeHandle a, b; 161 ScopedMessagePipeHandle a, b;
162 CreateMessagePipe(nullptr, &a, &b); 162 CreateMessagePipe(nullptr, &a, &b);
163 163
164 Watcher b_watcher(FROM_HERE); 164 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
165 EXPECT_EQ(MOJO_RESULT_OK, 165 EXPECT_EQ(MOJO_RESULT_OK,
166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
167 OnReady([](MojoResult result) { FAIL(); }))); 167 OnReady([](MojoResult result) { FAIL(); })));
168 EXPECT_TRUE(b_watcher.IsWatching()); 168 EXPECT_TRUE(b_watcher.IsWatching());
169 169
170 // This should trigger the watcher above... 170 // This should trigger the watcher above...
171 b.reset(); 171 b.reset();
172 // ...but the watcher is cancelled first. 172 // ...but the watcher is cancelled first.
173 b_watcher.Cancel(); 173 b_watcher.Cancel();
174 174
175 EXPECT_FALSE(b_watcher.IsWatching()); 175 EXPECT_FALSE(b_watcher.IsWatching());
176 176
177 base::RunLoop().RunUntilIdle(); 177 base::RunLoop().RunUntilIdle();
178 } 178 }
179 179
180 TEST_F(WatcherTest, UnarmedCancel) {
181 ScopedMessagePipeHandle a, b;
182 CreateMessagePipe(nullptr, &a, &b);
183
184 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
185 base::RunLoop loop;
186 EXPECT_EQ(MOJO_RESULT_OK,
187 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
188 base::Bind(
189 [](base::RunLoop* loop, MojoResult result) {
190 EXPECT_EQ(result, MOJO_RESULT_CANCELLED);
191 loop->Quit();
192 },
193 &loop)));
194
195 // This message write will not wake up the watcher since the watcher isn't
196 // armed. Instead, the cancellation will dispatch due to the reset below.
197 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
198 MOJO_WRITE_MESSAGE_FLAG_NONE));
199 b.reset();
200 loop.Run();
201 }
202
203 TEST_F(WatcherTest, ManualArming) {
204 ScopedMessagePipeHandle a, b;
205 CreateMessagePipe(nullptr, &a, &b);
206
207 Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
208 base::RunLoop loop;
209 EXPECT_EQ(MOJO_RESULT_OK,
210 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
211 base::Bind(
212 [](base::RunLoop* loop, MojoResult result) {
213 EXPECT_EQ(result, MOJO_RESULT_OK);
214 loop->Quit();
215 },
216 &loop)));
217 EXPECT_EQ(MOJO_RESULT_OK, b_watcher.Arm());
218
219 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
220 MOJO_WRITE_MESSAGE_FLAG_NONE));
221 loop.Run();
222 }
223
224 TEST_F(WatcherTest, ManualArmOrNotifyWhileSignaled) {
225 ScopedMessagePipeHandle a, b;
226 CreateMessagePipe(nullptr, &a, &b);
227
228 base::RunLoop loop1;
229 Watcher b_watcher1(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
230 bool notified1 = false;
231 EXPECT_EQ(MOJO_RESULT_OK,
232 b_watcher1.Start(
233 b.get(), MOJO_HANDLE_SIGNAL_READABLE,
234 base::Bind(
235 [](base::RunLoop* loop, bool* notified, MojoResult result) {
236 EXPECT_EQ(result, MOJO_RESULT_OK);
237 *notified = true;
238 loop->Quit();
239 },
240 &loop1, &notified1)));
241
242 base::RunLoop loop2;
243 Watcher b_watcher2(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
244 bool notified2 = false;
245 EXPECT_EQ(MOJO_RESULT_OK,
246 b_watcher2.Start(
247 b.get(), MOJO_HANDLE_SIGNAL_READABLE,
248 base::Bind(
249 [](base::RunLoop* loop, bool* notified, MojoResult result) {
250 EXPECT_EQ(result, MOJO_RESULT_OK);
251 *notified = true;
252 loop->Quit();
253 },
254 &loop2, &notified2)));
255
256 // First ensure that |b| is readable.
257 EXPECT_EQ(MOJO_RESULT_OK, b_watcher1.Arm());
258 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
259 MOJO_WRITE_MESSAGE_FLAG_NONE));
260 loop1.Run();
261
262 EXPECT_TRUE(notified1);
263 EXPECT_FALSE(notified2);
264 notified1 = false;
265
266 // Now verify that ArmOrNotify results in a notification.
267 b_watcher2.ArmOrNotify();
268 loop2.Run();
269
270 EXPECT_FALSE(notified1);
271 EXPECT_TRUE(notified2);
272 }
273
180 } // namespace 274 } // namespace
181 } // namespace mojo 275 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698