handle_watcher_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
10f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
20f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
30f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// found in the LICENSE file.
40f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "mojo/common/handle_watcher.h"
60f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/auto_reset.h"
100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/bind.h"
110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/run_loop.h"
120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/test/simple_test_tick_clock.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/common/time_helper.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "mojo/public/system/core_cpp.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/public/tests/test_utils.h"
160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace mojo {
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace common {
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace test {
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RunUntilIdle() {
230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::RunLoop run_loop;
240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  run_loop.RunUntilIdle();
250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DeleteWatcherAndForwardResult(
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    HandleWatcher* watcher,
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    base::Callback<void(MojoResult)> next_callback,
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MojoResult result) {
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  delete watcher;
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  next_callback.Run(result);
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Helper class to manage the callback and running the message loop waiting for
360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// message to be received. Typical usage is something like:
370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//   Schedule callback returned from GetCallback().
380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//   RunUntilGotCallback();
390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//   EXPECT_TRUE(got_callback());
400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)//   clear_callback();
410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class CallbackHelper {
420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public:
430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper()
440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      : got_callback_(false),
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        run_loop_(NULL),
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        weak_factory_(this) {}
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ~CallbackHelper() {}
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // See description above |got_callback_|.
500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool got_callback() const { return got_callback_; }
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void clear_callback() { got_callback_ = false; }
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Runs the current MessageLoop until the callback returned from GetCallback()
540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // is notified.
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void RunUntilGotCallback() {
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    ASSERT_TRUE(run_loop_ == NULL);
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::RunLoop run_loop;
580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop);
590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    run_loop.Run();
600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::Callback<void(MojoResult)> GetCallback() {
630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr());
640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Start(HandleWatcher* watcher, const MessagePipeHandle& handle) {
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    StartWithCallback(watcher, handle, GetCallback());
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void StartWithCallback(HandleWatcher* watcher,
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                         const MessagePipeHandle& handle,
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                         const base::Callback<void(MojoResult)>& callback) {
730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE,
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                   callback);
750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void OnCallback(MojoResult result) {
790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    got_callback_ = true;
800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (run_loop_)
810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      run_loop_->Quit();
820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Set to true when the callback is called.
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool got_callback_;
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // If non-NULL we're in RunUntilGotCallback().
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::RunLoop* run_loop_;
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::WeakPtrFactory<CallbackHelper> weak_factory_;
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CallbackHelper);
940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)};
950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class HandleWatcherTest : public testing::Test {
970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public:
980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcherTest() {}
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual ~HandleWatcherTest() {
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    test::SetTickClockForTest(NULL);
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) protected:
1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void InstallTickClock() {
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    test::SetTickClockForTest(&tick_clock_);
1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::SimpleTestTickClock tick_clock_;
1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::MessageLoop message_loop_;
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest);
1140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)};
1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Trivial test case with a single handle to watch.
1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, SingleHandler) {
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe;
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe.handle0.is_valid());
1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper;
1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher;
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper.Start(&watcher, test_pipe.handle0.get());
1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
1240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper.got_callback());
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(),
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper.RunUntilGotCallback();
1280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper.got_callback());
1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Creates three handles and notfies them in reverse order ensuring each one is
1320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// notified appropriately.
1330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, ThreeHandles) {
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe1;
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe2;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe3;
1370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper1;
1380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper2;
1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper3;
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe1.handle0.is_valid());
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe2.handle0.is_valid());
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe3.handle0.is_valid());
1430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher1;
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
1460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
1500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher2;
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
1540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
1550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
1560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
1570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher3;
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
1620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
1630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 3 and make sure it's notified.
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper3.RunUntilGotCallback();
1690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper3.got_callback());
1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper3.clear_callback();
1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 1 and 3. Only 1 should be notified since 3 was is no longer
1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // running.
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper1.RunUntilGotCallback();
1810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper1.got_callback());
1820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
1830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
1840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper1.clear_callback();
1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 1 and 2. Only 2 should be notified (since 1 was already notified).
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper2.RunUntilGotCallback();
1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper2.got_callback());
1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Verifies Start() invoked a second time works.
1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, Restart) {
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe1;
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe2;
2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper1;
2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper2;
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe1.handle0.is_valid());
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe2.handle0.is_valid());
2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher1;
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher2;
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
2140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 1 and make sure it's notified.
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper1.RunUntilGotCallback();
2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper1.got_callback());
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper1.clear_callback();
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get()));
2260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 2 and make sure it's notified.
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
2300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper2.RunUntilGotCallback();
2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper2.got_callback());
2330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper2.clear_callback();
2340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Listen on 1 again.
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
2370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
2380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Write to 1 and make sure it's notified.
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
2440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper1.RunUntilGotCallback();
2450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper1.got_callback());
2460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Verifies deadline is honored.
2500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, Deadline) {
2510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  InstallTickClock();
2520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe1;
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe2;
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe3;
2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper1;
2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper2;
2580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CallbackHelper callback_helper3;
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe1.handle0.is_valid());
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe2.handle0.is_valid());
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_pipe3.handle0.is_valid());
2620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Add a watcher with an infinite timeout.
2640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher1;
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
2660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
2670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
2700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Add another watcher wth a timeout of 500 microseconds.
2720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher2;
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  watcher2.Start(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, 500,
2740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                 callback_helper2.GetCallback());
2750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  RunUntilIdle();
2760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper2.got_callback());
2780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
2790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Advance the clock passed the deadline. We also have to start another
2810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // watcher to wake up the background thread.
2820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501));
2830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  HandleWatcher watcher3;
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
2860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  callback_helper2.RunUntilGotCallback();
2880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper1.got_callback());
2890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(callback_helper2.got_callback());
2900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(callback_helper3.got_callback());
2910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
293f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(HandleWatcherTest, DeleteInCallback) {
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MessagePipe test_pipe;
295f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CallbackHelper callback_helper;
296f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
297f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  HandleWatcher* watcher = new HandleWatcher();
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(),
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                    base::Bind(&DeleteWatcherAndForwardResult,
300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                               watcher,
301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                               callback_helper.GetCallback()));
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(),
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           std::string()));
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  callback_helper.RunUntilGotCallback();
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(callback_helper.got_callback());
306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace test
309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace common
3100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace mojo
311