summaryrefslogtreecommitdiff
path: root/mojo/public/cpp/system/tests/simple_watcher_unittest.cc
blob: 5688c42c0f5011331de6451005dae2726c413ba2 (plain)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/system/simple_watcher.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "mojo/public/c/system/types.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace {

template <typename Handler>
void RunResultHandler(Handler f, MojoResult result) {
  f(result);
}

template <typename Handler>
SimpleWatcher::ReadyCallback OnReady(Handler f) {
  return base::BindRepeating(&RunResultHandler<Handler>, f);
}

SimpleWatcher::ReadyCallback NotReached() {
  return OnReady([](MojoResult) { NOTREACHED_IN_MIGRATION(); });
}

class SimpleWatcherTest : public testing::Test {
 public:
  SimpleWatcherTest() {}

  SimpleWatcherTest(const SimpleWatcherTest&) = delete;
  SimpleWatcherTest& operator=(const SimpleWatcherTest&) = delete;

  ~SimpleWatcherTest() override {}

 private:
  base::test::SingleThreadTaskEnvironment task_environment_;
};

TEST_F(SimpleWatcherTest, WatchBasic) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  bool notified = false;
  base::RunLoop run_loop;
  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                            OnReady([&](MojoResult result) {
                              EXPECT_EQ(MOJO_RESULT_OK, result);
                              notified = true;
                              run_loop.Quit();
                            })));
  EXPECT_TRUE(b_watcher.IsWatching());

  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));
  run_loop.Run();
  EXPECT_TRUE(notified);

  b_watcher.Cancel();
}

TEST_F(SimpleWatcherTest, WatchUnsatisfiable) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);
  a.reset();

  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::MANUAL,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(
      MOJO_RESULT_OK,
      b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE, NotReached()));
  EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, b_watcher.Arm());
}

TEST_F(SimpleWatcherTest, WatchFailedPreconditionNoSpam) {
  ScopedDataPipeProducerHandle producer_handle;
  ScopedDataPipeConsumerHandle consumer_handle;
  ASSERT_EQ(CreateDataPipe(nullptr, producer_handle, consumer_handle),
            MOJO_RESULT_OK);
  bool had_failed_precondition = false;

  SimpleWatcher watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC);
  MojoResult rc =
      watcher.Watch(consumer_handle.get(), MOJO_HANDLE_SIGNAL_READABLE,
                    OnReady([&](MojoResult result) {
                      EXPECT_FALSE(had_failed_precondition);
                      switch (result) {
                        case MOJO_RESULT_OK:
                          const void* begin;
                          size_t num_bytes;
                          consumer_handle->BeginReadData(
                              &begin, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
                          consumer_handle->EndReadData(num_bytes);
                          break;
                        case MOJO_RESULT_FAILED_PRECONDITION:
                          had_failed_precondition = true;
                          break;
                      }
                    }));
  EXPECT_EQ(MOJO_RESULT_OK, rc);

  size_t size = 5;
  EXPECT_EQ(MOJO_RESULT_OK, producer_handle->WriteData(
                                "hello", &size, MOJO_WRITE_DATA_FLAG_NONE));
  base::RunLoop().RunUntilIdle();
  producer_handle.reset();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(had_failed_precondition);
}

TEST_F(SimpleWatcherTest, WatchInvalidHandle) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);
  a.reset();
  b.reset();

  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(
      MOJO_RESULT_INVALID_ARGUMENT,
      b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE, NotReached()));
  EXPECT_FALSE(b_watcher.IsWatching());
}

TEST_F(SimpleWatcherTest, Cancel) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  base::RunLoop run_loop;
  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(
      MOJO_RESULT_OK,
      b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE, NotReached()));
  EXPECT_TRUE(b_watcher.IsWatching());
  b_watcher.Cancel();
  EXPECT_FALSE(b_watcher.IsWatching());

  // This should never trigger the watcher.
  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, run_loop.QuitClosure());
  run_loop.Run();
}

TEST_F(SimpleWatcherTest, CancelOnClose) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  base::RunLoop run_loop;
  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                            OnReady([&](MojoResult result) {
                              EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
                              run_loop.Quit();
                            })));
  EXPECT_TRUE(b_watcher.IsWatching());

  // This should trigger the watcher above.
  b.reset();

  run_loop.Run();

  EXPECT_FALSE(b_watcher.IsWatching());
}

TEST_F(SimpleWatcherTest, CancelOnDestruction) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);
  base::RunLoop run_loop;
  {
    SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                            base::SequencedTaskRunner::GetCurrentDefault());
    EXPECT_EQ(
        MOJO_RESULT_OK,
        b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE, NotReached()));
    EXPECT_TRUE(b_watcher.IsWatching());

    // |b_watcher| should be cancelled when it goes out of scope.
  }

  // This should never trigger the watcher above.
  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, run_loop.QuitClosure());
  run_loop.Run();
}

TEST_F(SimpleWatcherTest, CloseAndCancel) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC,
                          base::SequencedTaskRunner::GetCurrentDefault());
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                            OnReady([](MojoResult result) { FAIL(); })));
  EXPECT_TRUE(b_watcher.IsWatching());

  // This should trigger the watcher above...
  b.reset();
  // ...but the watcher is cancelled first.
  b_watcher.Cancel();

  EXPECT_FALSE(b_watcher.IsWatching());

  base::RunLoop().RunUntilIdle();
}

TEST_F(SimpleWatcherTest, UnarmedCancel) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::MANUAL,
                          base::SequencedTaskRunner::GetCurrentDefault());
  base::RunLoop loop;
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                            base::BindRepeating(
                                [](base::RunLoop* loop, MojoResult result) {
                                  EXPECT_EQ(result, MOJO_RESULT_CANCELLED);
                                  loop->Quit();
                                },
                                &loop)));

  // This message write will not wake up the watcher since the watcher isn't
  // armed. Instead, the cancellation will dispatch due to the reset below.
  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));
  b.reset();
  loop.Run();
}

TEST_F(SimpleWatcherTest, ManualArming) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  SimpleWatcher b_watcher(FROM_HERE, SimpleWatcher::ArmingPolicy::MANUAL,
                          base::SequencedTaskRunner::GetCurrentDefault());
  base::RunLoop loop;
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher.Watch(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                            base::BindRepeating(
                                [](base::RunLoop* loop, MojoResult result) {
                                  EXPECT_EQ(result, MOJO_RESULT_OK);
                                  loop->Quit();
                                },
                                &loop)));
  EXPECT_EQ(MOJO_RESULT_OK, b_watcher.Arm());

  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));
  loop.Run();
}

TEST_F(SimpleWatcherTest, ManualArmOrNotifyWhileSignaled) {
  ScopedMessagePipeHandle a, b;
  CreateMessagePipe(nullptr, &a, &b);

  base::RunLoop loop1;
  SimpleWatcher b_watcher1(FROM_HERE, SimpleWatcher::ArmingPolicy::MANUAL);
  bool notified1 = false;
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher1.Watch(
                b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                base::BindRepeating(
                    [](base::RunLoop* loop, bool* notified, MojoResult result) {
                      EXPECT_EQ(result, MOJO_RESULT_OK);
                      *notified = true;
                      loop->Quit();
                    },
                    &loop1, &notified1)));

  base::RunLoop loop2;
  SimpleWatcher b_watcher2(FROM_HERE, SimpleWatcher::ArmingPolicy::MANUAL);
  bool notified2 = false;
  EXPECT_EQ(MOJO_RESULT_OK,
            b_watcher2.Watch(
                b.get(), MOJO_HANDLE_SIGNAL_READABLE,
                base::BindRepeating(
                    [](base::RunLoop* loop, bool* notified, MojoResult result) {
                      EXPECT_EQ(result, MOJO_RESULT_OK);
                      *notified = true;
                      loop->Quit();
                    },
                    &loop2, &notified2)));

  // First ensure that |b| is readable.
  EXPECT_EQ(MOJO_RESULT_OK, b_watcher1.Arm());
  EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
                                            MOJO_WRITE_MESSAGE_FLAG_NONE));
  loop1.Run();

  EXPECT_TRUE(notified1);
  EXPECT_FALSE(notified2);
  notified1 = false;

  // Now verify that ArmOrNotify results in a notification.
  b_watcher2.ArmOrNotify();
  loop2.Run();

  EXPECT_FALSE(notified1);
  EXPECT_TRUE(notified2);
}

}  // namespace
}  // namespace mojo