aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/tests/bind_task_runner_unittest.cc
blob: 0c777ec410e8eccebb6f04a58a9f699177de26b4 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/callback.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
#include "mojo/public/cpp/bindings/associated_interface_request.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace test {
namespace {

class TestTaskRunner : public base::SingleThreadTaskRunner {
 public:
  TestTaskRunner()
      : thread_id_(base::PlatformThread::CurrentRef()),
        quit_called_(false),
        task_ready_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
                    base::WaitableEvent::InitialState::NOT_SIGNALED) {}

  bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
                                  const base::Closure& task,
                                  base::TimeDelta delay) override {
    NOTREACHED();
    return false;
  }

  bool PostDelayedTask(const tracked_objects::Location& from_here,
                       const base::Closure& task,
                       base::TimeDelta delay) override {
    {
      base::AutoLock locker(lock_);
      tasks_.push(task);
    }
    task_ready_.Signal();
    return true;
  }
  bool RunsTasksOnCurrentThread() const override {
    return base::PlatformThread::CurrentRef() == thread_id_;
  }

  // Only quits when Quit() is called.
  void Run() {
    DCHECK(RunsTasksOnCurrentThread());
    quit_called_ = false;

    while (true) {
      {
        base::AutoLock locker(lock_);
        while (!tasks_.empty()) {
          auto task = tasks_.front();
          tasks_.pop();

          {
            base::AutoUnlock unlocker(lock_);
            task.Run();
            if (quit_called_)
              return;
          }
        }
      }
      task_ready_.Wait();
    }
  }

  void Quit() {
    DCHECK(RunsTasksOnCurrentThread());
    quit_called_ = true;
  }

  // Waits until one task is ready and runs it.
  void RunOneTask() {
    DCHECK(RunsTasksOnCurrentThread());

    while (true) {
      {
        base::AutoLock locker(lock_);
        if (!tasks_.empty()) {
          auto task = tasks_.front();
          tasks_.pop();

          {
            base::AutoUnlock unlocker(lock_);
            task.Run();
            return;
          }
        }
      }
      task_ready_.Wait();
    }
  }

 private:
  ~TestTaskRunner() override {}

  const base::PlatformThreadRef thread_id_;
  bool quit_called_;
  base::WaitableEvent task_ready_;

  // Protect |tasks_|.
  base::Lock lock_;
  std::queue<base::Closure> tasks_;

  DISALLOW_COPY_AND_ASSIGN(TestTaskRunner);
};

template <typename BindingType, typename RequestType>
class IntegerSenderImpl : public IntegerSender {
 public:
  IntegerSenderImpl(RequestType request,
                    scoped_refptr<base::SingleThreadTaskRunner> runner)
      : binding_(this, std::move(request), std::move(runner)) {}

  ~IntegerSenderImpl() override {}

  using EchoHandler = base::Callback<void(int32_t, const EchoCallback&)>;

  void set_echo_handler(const EchoHandler& handler) { echo_handler_ = handler; }

  void Echo(int32_t value, const EchoCallback& callback) override {
    if (echo_handler_.is_null())
      callback.Run(value);
    else
      echo_handler_.Run(value, callback);
  }
  void Send(int32_t value) override { NOTREACHED(); }

  BindingType* binding() { return &binding_; }

 private:
  BindingType binding_;
  EchoHandler echo_handler_;
};

class IntegerSenderConnectionImpl : public IntegerSenderConnection {
 public:
  using SenderType = IntegerSenderImpl<AssociatedBinding<IntegerSender>,
                                       IntegerSenderAssociatedRequest>;

  explicit IntegerSenderConnectionImpl(
      IntegerSenderConnectionRequest request,
      scoped_refptr<base::SingleThreadTaskRunner> runner,
      scoped_refptr<base::SingleThreadTaskRunner> sender_runner)
      : binding_(this, std::move(request), std::move(runner)),
        sender_runner_(std::move(sender_runner)) {}

  ~IntegerSenderConnectionImpl() override {}

  void set_get_sender_notification(const base::Closure& notification) {
    get_sender_notification_ = notification;
  }
  void GetSender(IntegerSenderAssociatedRequest sender) override {
    sender_impl_.reset(new SenderType(std::move(sender), sender_runner_));
    get_sender_notification_.Run();
  }

  void AsyncGetSender(const AsyncGetSenderCallback& callback) override {
    NOTREACHED();
  }

  Binding<IntegerSenderConnection>* binding() { return &binding_; }

  SenderType* sender_impl() { return sender_impl_.get(); }

 private:
  Binding<IntegerSenderConnection> binding_;
  std::unique_ptr<SenderType> sender_impl_;
  scoped_refptr<base::SingleThreadTaskRunner> sender_runner_;
  base::Closure get_sender_notification_;
};

class BindTaskRunnerTest : public testing::Test {
 protected:
  void SetUp() override {
    binding_task_runner_ = scoped_refptr<TestTaskRunner>(new TestTaskRunner);
    ptr_task_runner_ = scoped_refptr<TestTaskRunner>(new TestTaskRunner);

    auto request = MakeRequest(&ptr_, ptr_task_runner_);
    impl_.reset(new ImplType(std::move(request), binding_task_runner_));
  }

  base::MessageLoop loop_;
  scoped_refptr<TestTaskRunner> binding_task_runner_;
  scoped_refptr<TestTaskRunner> ptr_task_runner_;

  IntegerSenderPtr ptr_;
  using ImplType =
      IntegerSenderImpl<Binding<IntegerSender>, IntegerSenderRequest>;
  std::unique_ptr<ImplType> impl_;
};

class AssociatedBindTaskRunnerTest : public testing::Test {
 protected:
  void SetUp() override {
    connection_binding_task_runner_ =
        scoped_refptr<TestTaskRunner>(new TestTaskRunner);
    connection_ptr_task_runner_ =
        scoped_refptr<TestTaskRunner>(new TestTaskRunner);
    sender_binding_task_runner_ =
        scoped_refptr<TestTaskRunner>(new TestTaskRunner);
    sender_ptr_task_runner_ = scoped_refptr<TestTaskRunner>(new TestTaskRunner);

    auto connection_request =
        MakeRequest(&connection_ptr_, connection_ptr_task_runner_);
    connection_impl_.reset(new IntegerSenderConnectionImpl(
        std::move(connection_request), connection_binding_task_runner_,
        sender_binding_task_runner_));

    connection_impl_->set_get_sender_notification(
        base::Bind(&AssociatedBindTaskRunnerTest::QuitTaskRunner,
                   base::Unretained(this)));

    connection_ptr_->GetSender(
        MakeRequest(&sender_ptr_, sender_ptr_task_runner_));
    connection_binding_task_runner_->Run();
  }

  void QuitTaskRunner() {
    connection_binding_task_runner_->Quit();
  }

  base::MessageLoop loop_;
  scoped_refptr<TestTaskRunner> connection_binding_task_runner_;
  scoped_refptr<TestTaskRunner> connection_ptr_task_runner_;
  scoped_refptr<TestTaskRunner> sender_binding_task_runner_;
  scoped_refptr<TestTaskRunner> sender_ptr_task_runner_;

  IntegerSenderConnectionPtr connection_ptr_;
  std::unique_ptr<IntegerSenderConnectionImpl> connection_impl_;
  IntegerSenderAssociatedPtr sender_ptr_;
};

void DoSetFlagAndQuitTaskRunner(bool* flag,
                                scoped_refptr<TestTaskRunner> task_runner) {
  *flag = true;
  if (task_runner)
    task_runner->Quit();
}

void DoExpectValueSetFlagAndQuitTaskRunner(
    int32_t expected_value,
    bool* flag,
    scoped_refptr<TestTaskRunner> task_runner,
    int32_t value) {
  EXPECT_EQ(expected_value, value);
  DoSetFlagAndQuitTaskRunner(flag, task_runner);
}

void DoExpectValueSetFlagForwardValueAndQuitTaskRunner(
    int32_t expected_value,
    bool* flag,
    scoped_refptr<TestTaskRunner> task_runner,
    int32_t value,
    const IntegerSender::EchoCallback& callback) {
  EXPECT_EQ(expected_value, value);
  *flag = true;
  callback.Run(value);
  task_runner->Quit();
}

base::Closure SetFlagAndQuitTaskRunner(
    bool* flag,
    scoped_refptr<TestTaskRunner> task_runner) {
  return base::Bind(&DoSetFlagAndQuitTaskRunner, flag, task_runner);
}

base::Callback<void(int32_t)> ExpectValueSetFlagAndQuitTaskRunner(
    int32_t expected_value,
    bool* flag,
    scoped_refptr<TestTaskRunner> task_runner) {
  return base::Bind(&DoExpectValueSetFlagAndQuitTaskRunner, expected_value,
                    flag, task_runner);
}

TEST_F(BindTaskRunnerTest, MethodCall) {
  bool echo_called = false;
  impl_->set_echo_handler(
      base::Bind(&DoExpectValueSetFlagForwardValueAndQuitTaskRunner,
                 1024, &echo_called, binding_task_runner_));
  bool echo_replied = false;
  ptr_->Echo(1024, ExpectValueSetFlagAndQuitTaskRunner(1024, &echo_replied,
                                                       ptr_task_runner_));
  binding_task_runner_->Run();
  EXPECT_TRUE(echo_called);
  ptr_task_runner_->Run();
  EXPECT_TRUE(echo_replied);
}

TEST_F(BindTaskRunnerTest, BindingConnectionError) {
  bool connection_error_called = false;
  impl_->binding()->set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&connection_error_called, binding_task_runner_));
  ptr_.reset();
  binding_task_runner_->Run();
  EXPECT_TRUE(connection_error_called);
}

TEST_F(BindTaskRunnerTest, PtrConnectionError) {
  bool connection_error_called = false;
  ptr_.set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&connection_error_called, ptr_task_runner_));
  impl_->binding()->Close();
  ptr_task_runner_->Run();
  EXPECT_TRUE(connection_error_called);
}

void ExpectValueSetFlagAndForward(int32_t expected_value,
                                  bool* flag,
                                  int32_t value,
                                  const IntegerSender::EchoCallback& callback) {
  EXPECT_EQ(expected_value, value);
  *flag = true;
  callback.Run(value);
}

TEST_F(AssociatedBindTaskRunnerTest, MethodCall) {
  bool echo_called = false;
  connection_impl_->sender_impl()->set_echo_handler(
      base::Bind(&ExpectValueSetFlagAndForward, 1024, &echo_called));

  bool echo_replied = false;
  sender_ptr_->Echo(
      1024, ExpectValueSetFlagAndQuitTaskRunner(1024, &echo_replied, nullptr));

  // The Echo request first arrives at the master endpoint's task runner, and
  // then is forwarded to the associated endpoint's task runner.
  connection_binding_task_runner_->RunOneTask();
  sender_binding_task_runner_->RunOneTask();
  EXPECT_TRUE(echo_called);

  // Similarly, the Echo response arrives at the master endpoint's task runner
  // and then is forwarded to the associated endpoint's task runner.
  connection_ptr_task_runner_->RunOneTask();
  sender_ptr_task_runner_->RunOneTask();
  EXPECT_TRUE(echo_replied);
}

TEST_F(AssociatedBindTaskRunnerTest, BindingConnectionError) {
  bool sender_impl_error = false;
  connection_impl_->sender_impl()->binding()->set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&sender_impl_error,
                               sender_binding_task_runner_));
  bool connection_impl_error = false;
  connection_impl_->binding()->set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&connection_impl_error,
                               connection_binding_task_runner_));
  bool sender_ptr_error = false;
  sender_ptr_.set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&sender_ptr_error, sender_ptr_task_runner_));
  connection_ptr_.reset();
  sender_ptr_task_runner_->Run();
  EXPECT_TRUE(sender_ptr_error);
  connection_binding_task_runner_->Run();
  EXPECT_TRUE(connection_impl_error);
  sender_binding_task_runner_->Run();
  EXPECT_TRUE(sender_impl_error);
}

TEST_F(AssociatedBindTaskRunnerTest, PtrConnectionError) {
  bool sender_impl_error = false;
  connection_impl_->sender_impl()->binding()->set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&sender_impl_error,
                               sender_binding_task_runner_));
  bool connection_ptr_error = false;
  connection_ptr_.set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&connection_ptr_error,
                               connection_ptr_task_runner_));
  bool sender_ptr_error = false;
  sender_ptr_.set_connection_error_handler(
      SetFlagAndQuitTaskRunner(&sender_ptr_error, sender_ptr_task_runner_));
  connection_impl_->binding()->Close();
  sender_binding_task_runner_->Run();
  EXPECT_TRUE(sender_impl_error);
  connection_ptr_task_runner_->Run();
  EXPECT_TRUE(connection_ptr_error);
  sender_ptr_task_runner_->Run();
  EXPECT_TRUE(sender_ptr_error);
}

}  // namespace
}  // namespace test
}  // namespace mojo