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

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

Issue 2540903002: Remove MessageLoop destruction observer from mojo::Watcher. (Closed)
Patch Set: . Created 4 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 | « mojo/android/system/watcher_impl.cc ('k') | mojo/public/cpp/system/watcher.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 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"
11 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "mojo/public/c/system/types.h" 15 #include "mojo/public/c/system/types.h"
15 #include "mojo/public/cpp/system/message_pipe.h" 16 #include "mojo/public/cpp/system/message_pipe.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace mojo { 19 namespace mojo {
19 namespace { 20 namespace {
20 21
21 template <typename Handler> 22 template <typename Handler>
22 void RunResultHandler(Handler f, MojoResult result) { f(result); } 23 void RunResultHandler(Handler f, MojoResult result) { f(result); }
23 24
24 template <typename Handler> 25 template <typename Handler>
25 Watcher::ReadyCallback OnReady(Handler f) { 26 Watcher::ReadyCallback OnReady(Handler f) {
26 return base::Bind(&RunResultHandler<Handler>, f); 27 return base::Bind(&RunResultHandler<Handler>, f);
27 } 28 }
28 29
29 Watcher::ReadyCallback NotReached() { 30 Watcher::ReadyCallback NotReached() {
30 return OnReady([] (MojoResult) { NOTREACHED(); }); 31 return OnReady([] (MojoResult) { NOTREACHED(); });
31 } 32 }
32 33
33 class WatcherTest : public testing::Test { 34 class WatcherTest : public testing::Test {
34 public: 35 public:
35 WatcherTest() {} 36 WatcherTest() {}
36 ~WatcherTest() override {} 37 ~WatcherTest() override {}
37 38
38 void SetUp() override { 39 private:
39 message_loop_.reset(new base::MessageLoop); 40 base::MessageLoop message_loop_;
40 }
41 41
42 void TearDown() override { 42 DISALLOW_COPY_AND_ASSIGN(WatcherTest);
43 message_loop_.reset();
44 }
45
46 protected:
47 std::unique_ptr<base::MessageLoop> message_loop_;
48 }; 43 };
49 44
50 TEST_F(WatcherTest, WatchBasic) { 45 TEST_F(WatcherTest, WatchBasic) {
51 ScopedMessagePipeHandle a, b; 46 ScopedMessagePipeHandle a, b;
52 CreateMessagePipe(nullptr, &a, &b); 47 CreateMessagePipe(nullptr, &a, &b);
53 48
54 bool notified = false; 49 bool notified = false;
55 base::RunLoop run_loop; 50 base::RunLoop run_loop;
56 Watcher b_watcher; 51 Watcher b_watcher;
57 EXPECT_EQ(MOJO_RESULT_OK, 52 EXPECT_EQ(MOJO_RESULT_OK,
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 150 }
156 151
157 // This should never trigger the watcher above. 152 // This should never trigger the watcher above.
158 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,
159 MOJO_WRITE_MESSAGE_FLAG_NONE)); 154 MOJO_WRITE_MESSAGE_FLAG_NONE));
160 base::ThreadTaskRunnerHandle::Get()->PostTask( 155 base::ThreadTaskRunnerHandle::Get()->PostTask(
161 FROM_HERE, run_loop.QuitClosure()); 156 FROM_HERE, run_loop.QuitClosure());
162 run_loop.Run(); 157 run_loop.Run();
163 } 158 }
164 159
165 TEST_F(WatcherTest, NotifyOnMessageLoopDestruction) {
166 ScopedMessagePipeHandle a, b;
167 CreateMessagePipe(nullptr, &a, &b);
168
169 bool notified = false;
170 Watcher b_watcher;
171 EXPECT_EQ(MOJO_RESULT_OK,
172 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
173 OnReady([&] (MojoResult result) {
174 EXPECT_EQ(MOJO_RESULT_ABORTED, result);
175 notified = true;
176 })));
177 EXPECT_TRUE(b_watcher.IsWatching());
178
179 message_loop_.reset();
180
181 EXPECT_TRUE(notified);
182
183 EXPECT_TRUE(b_watcher.IsWatching());
184 b_watcher.Cancel();
185 }
186
187 TEST_F(WatcherTest, CloseAndCancel) { 160 TEST_F(WatcherTest, CloseAndCancel) {
188 ScopedMessagePipeHandle a, b; 161 ScopedMessagePipeHandle a, b;
189 CreateMessagePipe(nullptr, &a, &b); 162 CreateMessagePipe(nullptr, &a, &b);
190 163
191 Watcher b_watcher; 164 Watcher b_watcher;
192 EXPECT_EQ(MOJO_RESULT_OK, 165 EXPECT_EQ(MOJO_RESULT_OK,
193 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
194 OnReady([](MojoResult result) { FAIL(); }))); 167 OnReady([](MojoResult result) { FAIL(); })));
195 EXPECT_TRUE(b_watcher.IsWatching()); 168 EXPECT_TRUE(b_watcher.IsWatching());
196 169
197 // This should trigger the watcher above... 170 // This should trigger the watcher above...
198 b.reset(); 171 b.reset();
199 // ...but the watcher is cancelled first. 172 // ...but the watcher is cancelled first.
200 b_watcher.Cancel(); 173 b_watcher.Cancel();
201 174
202 EXPECT_FALSE(b_watcher.IsWatching()); 175 EXPECT_FALSE(b_watcher.IsWatching());
203 176
204 base::RunLoop().RunUntilIdle(); 177 base::RunLoop().RunUntilIdle();
205 } 178 }
206 179
207 } // namespace 180 } // namespace
208 } // namespace mojo 181 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/android/system/watcher_impl.cc ('k') | mojo/public/cpp/system/watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698