handle_watcher_unittest.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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" 14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "mojo/public/cpp/environment/environment.h" 15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "mojo/public/cpp/system/core.h" 16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "mojo/public/cpp/test_support/test_utils.h" 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace mojo { 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace common { 210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace test { 220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RunUntilIdle() { 240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop run_loop; 250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop.RunUntilIdle(); 260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DeleteWatcherAndForwardResult( 29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HandleWatcher* watcher, 30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Callback<void(MojoResult)> next_callback, 31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MojoResult result) { 32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) delete watcher; 33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) next_callback.Run(result); 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Helper class to manage the callback and running the message loop waiting for 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// message to be received. Typical usage is something like: 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Schedule callback returned from GetCallback(). 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// RunUntilGotCallback(); 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// EXPECT_TRUE(got_callback()); 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// clear_callback(); 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class CallbackHelper { 430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public: 440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper() 450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) : got_callback_(false), 460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop_(NULL), 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) weak_factory_(this) {} 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ~CallbackHelper() {} 490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // See description above |got_callback_|. 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) bool got_callback() const { return got_callback_; } 520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void clear_callback() { got_callback_ = false; } 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Runs the current MessageLoop until the callback returned from GetCallback() 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // is notified. 560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void RunUntilGotCallback() { 570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ASSERT_TRUE(run_loop_ == NULL); 580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop run_loop; 590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop); 600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop.Run(); 610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Callback<void(MojoResult)> GetCallback() { 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr()); 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void Start(HandleWatcher* watcher, const MessagePipeHandle& handle) { 68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) StartWithCallback(watcher, handle, GetCallback()); 69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void StartWithCallback(HandleWatcher* watcher, 72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const MessagePipeHandle& handle, 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::Callback<void(MojoResult)>& callback) { 740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE, 75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback); 760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void OnCallback(MojoResult result) { 800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) got_callback_ = true; 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (run_loop_) 820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop_->Quit(); 830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Set to true when the callback is called. 860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) bool got_callback_; 870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // If non-NULL we're in RunUntilGotCallback(). 890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop* run_loop_; 900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::WeakPtrFactory<CallbackHelper> weak_factory_; 920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(CallbackHelper); 950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}; 960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class HandleWatcherTest : public testing::Test { 980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public: 990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcherTest() {} 1000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual ~HandleWatcherTest() { 101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) test::SetTickClockForTest(NULL); 1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch virtual void SetUp() OVERRIDE { 105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch environment_.reset(new Environment); 106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch } 107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch virtual void TearDown() OVERRIDE { 109e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch environment_.reset(); 110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch } 111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) protected: 1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void InstallTickClock() { 114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) test::SetTickClockForTest(&tick_clock_); 1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::SimpleTestTickClock tick_clock_; 1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::MessageLoop message_loop_; 121e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch scoped_ptr<Environment> environment_; 122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); 1240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}; 1250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Trivial test case with a single handle to watch. 1270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, SingleHandler) { 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe; 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe.handle0.is_valid()); 1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper; 1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher; 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper.Start(&watcher, test_pipe.handle0.get()); 1330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper.got_callback()); 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(), 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 1370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper.RunUntilGotCallback(); 1380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper.got_callback()); 1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 1400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Creates three handles and notfies them in reverse order ensuring each one is 1420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// notified appropriately. 1430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, ThreeHandles) { 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe1; 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe2; 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe3; 1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper3; 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe1.handle0.is_valid()); 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe2.handle0.is_valid()); 1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe3.handle0.is_valid()); 1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); 1560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher2; 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); 1630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher3; 1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); 1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 3 and make sure it's notified. 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper3.RunUntilGotCallback(); 1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper3.got_callback()); 1820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper3.clear_callback(); 1830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and 3. Only 1 should be notified since 3 was is no longer 1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // running. 1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), 1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(), 1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 1900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.clear_callback(); 1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and 2. Only 2 should be notified (since 1 was already notified). 1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), 1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), 2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.RunUntilGotCallback(); 2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper2.got_callback()); 2040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 2060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Verifies Start() invoked a second time works. 2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, Restart) { 2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe1; 2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe2; 2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe1.handle0.is_valid()); 2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe2.handle0.is_valid()); 2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); 2180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher2; 2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper2.Start(&watcher2, test_pipe2.handle0.get()); 2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and make sure it's notified. 2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), 2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 2330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.clear_callback(); 2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get())); 2360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 2 and make sure it's notified. 2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(), 2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 2400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.RunUntilGotCallback(); 2410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper2.got_callback()); 2430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.clear_callback(); 2440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Listen on 1 again. 2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); 2470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and make sure it's notified. 2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(), 2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 2540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 2550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 2580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Verifies deadline is honored. 2600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, Deadline) { 2610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) InstallTickClock(); 2620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe1; 2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe2; 2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe3; 2660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 2670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 2680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper3; 2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe1.handle0.is_valid()); 2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe2.handle0.is_valid()); 2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_TRUE(test_pipe3.handle0.is_valid()); 2720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Add a watcher with an infinite timeout. 2740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle0.get()); 2760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Add another watcher wth a timeout of 500 microseconds. 2820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher2; 2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) watcher2.Start(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, 500, 2840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.GetCallback()); 2850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Advance the clock passed the deadline. We also have to start another 2910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // watcher to wake up the background thread. 2920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); 2930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher3; 2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper3.Start(&watcher3, test_pipe3.handle0.get()); 2960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.RunUntilGotCallback(); 2980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper2.got_callback()); 3000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 3010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 3020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(HandleWatcherTest, DeleteInCallback) { 3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) MessagePipe test_pipe; 305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CallbackHelper callback_helper; 306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HandleWatcher* watcher = new HandleWatcher(); 3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(), 309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Bind(&DeleteWatcherAndForwardResult, 310f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) watcher, 311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.GetCallback())); 3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(), 3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string())); 314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.RunUntilGotCallback(); 315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_TRUE(callback_helper.got_callback()); 316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 3180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace test 319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} // namespace common 3200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace mojo 321