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

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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/public/cpp/system/watcher.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "mojo/public/c/system/types.h"
16 #include "mojo/public/cpp/system/message_pipe.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace mojo {
20 namespace {
21
22 template <typename Handler>
23 void RunResultHandler(Handler f, MojoResult result) { f(result); }
24
25 template <typename Handler>
26 Watcher::ReadyCallback OnReady(Handler f) {
27 return base::Bind(&RunResultHandler<Handler>, f);
28 }
29
30 Watcher::ReadyCallback NotReached() {
31 return OnReady([] (MojoResult) { NOTREACHED(); });
32 }
33
34 class WatcherTest : public testing::Test {
35 public:
36 WatcherTest() {}
37 ~WatcherTest() override {}
38
39 private:
40 base::MessageLoop message_loop_;
41
42 DISALLOW_COPY_AND_ASSIGN(WatcherTest);
43 };
44
45 TEST_F(WatcherTest, WatchBasic) {
46 ScopedMessagePipeHandle a, b;
47 CreateMessagePipe(nullptr, &a, &b);
48
49 bool notified = false;
50 base::RunLoop run_loop;
51 Watcher b_watcher(FROM_HERE);
52 EXPECT_EQ(MOJO_RESULT_OK,
53 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
54 OnReady([&] (MojoResult result) {
55 EXPECT_EQ(MOJO_RESULT_OK, result);
56 notified = true;
57 run_loop.Quit();
58 })));
59 EXPECT_TRUE(b_watcher.IsWatching());
60
61 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
62 MOJO_WRITE_MESSAGE_FLAG_NONE));
63 run_loop.Run();
64 EXPECT_TRUE(notified);
65
66 b_watcher.Cancel();
67 }
68
69 TEST_F(WatcherTest, WatchUnsatisfiable) {
70 ScopedMessagePipeHandle a, b;
71 CreateMessagePipe(nullptr, &a, &b);
72 a.reset();
73
74 Watcher b_watcher(FROM_HERE);
75 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
76 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
77 NotReached()));
78 EXPECT_FALSE(b_watcher.IsWatching());
79 }
80
81 TEST_F(WatcherTest, WatchInvalidHandle) {
82 ScopedMessagePipeHandle a, b;
83 CreateMessagePipe(nullptr, &a, &b);
84 a.reset();
85 b.reset();
86
87 Watcher b_watcher(FROM_HERE);
88 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
89 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
90 NotReached()));
91 EXPECT_FALSE(b_watcher.IsWatching());
92 }
93
94 TEST_F(WatcherTest, Cancel) {
95 ScopedMessagePipeHandle a, b;
96 CreateMessagePipe(nullptr, &a, &b);
97
98 base::RunLoop run_loop;
99 Watcher b_watcher(FROM_HERE);
100 EXPECT_EQ(MOJO_RESULT_OK,
101 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
102 NotReached()));
103 EXPECT_TRUE(b_watcher.IsWatching());
104 b_watcher.Cancel();
105 EXPECT_FALSE(b_watcher.IsWatching());
106
107 // This should never trigger the watcher.
108 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
109 MOJO_WRITE_MESSAGE_FLAG_NONE));
110
111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE, run_loop.QuitClosure());
113 run_loop.Run();
114 }
115
116 TEST_F(WatcherTest, CancelOnClose) {
117 ScopedMessagePipeHandle a, b;
118 CreateMessagePipe(nullptr, &a, &b);
119
120 base::RunLoop run_loop;
121 Watcher b_watcher(FROM_HERE);
122 EXPECT_EQ(MOJO_RESULT_OK,
123 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
124 OnReady([&] (MojoResult result) {
125 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
126 run_loop.Quit();
127 })));
128 EXPECT_TRUE(b_watcher.IsWatching());
129
130 // This should trigger the watcher above.
131 b.reset();
132
133 run_loop.Run();
134
135 EXPECT_FALSE(b_watcher.IsWatching());
136 }
137
138 TEST_F(WatcherTest, CancelOnDestruction) {
139 ScopedMessagePipeHandle a, b;
140 CreateMessagePipe(nullptr, &a, &b);
141 base::RunLoop run_loop;
142 {
143 Watcher b_watcher(FROM_HERE);
144 EXPECT_EQ(MOJO_RESULT_OK,
145 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
146 NotReached()));
147 EXPECT_TRUE(b_watcher.IsWatching());
148
149 // |b_watcher| should be cancelled when it goes out of scope.
150 }
151
152 // This should never trigger the watcher above.
153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
154 MOJO_WRITE_MESSAGE_FLAG_NONE));
155 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, run_loop.QuitClosure());
157 run_loop.Run();
158 }
159
160 TEST_F(WatcherTest, CloseAndCancel) {
161 ScopedMessagePipeHandle a, b;
162 CreateMessagePipe(nullptr, &a, &b);
163
164 Watcher b_watcher(FROM_HERE);
165 EXPECT_EQ(MOJO_RESULT_OK,
166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
167 OnReady([](MojoResult result) { FAIL(); })));
168 EXPECT_TRUE(b_watcher.IsWatching());
169
170 // This should trigger the watcher above...
171 b.reset();
172 // ...but the watcher is cancelled first.
173 b_watcher.Cancel();
174
175 EXPECT_FALSE(b_watcher.IsWatching());
176
177 base::RunLoop().RunUntilIdle();
178 }
179
180 } // namespace
181 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698