handle_watcher_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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) 70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/auto_reset.h" 80f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/bind.h" 90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/run_loop.h" 100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/test/simple_test_tick_clock.h" 11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "mojo/public/system/core_cpp.h" 120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace mojo { 15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace common { 160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace test { 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)struct MessagePipe { 19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe() { CreateMessagePipe(&handle_0, &handle_1); } 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ~MessagePipe() {} 21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ScopedMessagePipeHandle handle_0; 23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ScopedMessagePipeHandle handle_1; 24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(MessagePipe); 26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}; 27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)MojoResult WriteToHandle(const MessagePipeHandle& handle) { 29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return WriteMessageRaw(handle, NULL, 0, NULL, 0, 30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MOJO_WRITE_MESSAGE_FLAG_NONE); 310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)MojoResult ReadFromHandle(const MessagePipeHandle& handle) { 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return ReadMessageRaw(handle, NULL, NULL, NULL, NULL, 35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void RunUntilIdle() { 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop run_loop; 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop.RunUntilIdle(); 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DeleteWatcherAndForwardResult( 44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HandleWatcher* watcher, 45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Callback<void(MojoResult)> next_callback, 46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MojoResult result) { 47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) delete watcher; 48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) next_callback.Run(result); 49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Helper class to manage the callback and running the message loop waiting for 520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// message to be received. Typical usage is something like: 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Schedule callback returned from GetCallback(). 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// RunUntilGotCallback(); 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// EXPECT_TRUE(got_callback()); 560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// clear_callback(); 570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class CallbackHelper { 580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public: 590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper() 600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) : got_callback_(false), 610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop_(NULL), 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) weak_factory_(this) {} 630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ~CallbackHelper() {} 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // See description above |got_callback_|. 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) bool got_callback() const { return got_callback_; } 670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void clear_callback() { got_callback_ = false; } 680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Runs the current MessageLoop until the callback returned from GetCallback() 700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // is notified. 710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void RunUntilGotCallback() { 720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ASSERT_TRUE(run_loop_ == NULL); 730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop run_loop; 740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop); 750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop.Run(); 760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Callback<void(MojoResult)> GetCallback() { 790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr()); 800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void Start(HandleWatcher* watcher, const MessagePipeHandle& handle) { 83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) StartWithCallback(watcher, handle, GetCallback()); 84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void StartWithCallback(HandleWatcher* watcher, 87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const MessagePipeHandle& handle, 88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::Callback<void(MojoResult)>& callback) { 890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE, 90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback); 910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void OnCallback(MojoResult result) { 950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) got_callback_ = true; 960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (run_loop_) 970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) run_loop_->Quit(); 980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Set to true when the callback is called. 1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) bool got_callback_; 1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // If non-NULL we're in RunUntilGotCallback(). 1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::RunLoop* run_loop_; 1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::WeakPtrFactory<CallbackHelper> weak_factory_; 1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(CallbackHelper); 1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}; 1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class HandleWatcherTest : public testing::Test { 1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public: 1140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcherTest() {} 1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual ~HandleWatcherTest() { 1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher::tick_clock_ = NULL; 1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) protected: 1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) void InstallTickClock() { 1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher::tick_clock_ = &tick_clock_; 1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::SimpleTestTickClock tick_clock_; 1250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::MessageLoop message_loop_; 128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); 1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}; 1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Trivial test case with a single handle to watch. 1330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, SingleHandler) { 134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe; 135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe.handle_0.is_valid()); 1360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper; 1370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher; 138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.Start(&watcher, test_pipe.handle_0); 1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper.got_callback()); 141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe.handle_1)); 1420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper.RunUntilGotCallback(); 1430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper.got_callback()); 1440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 1450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Creates three handles and notfies them in reverse order ensuring each one is 1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// notified appropriately. 1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, ThreeHandles) { 149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe1; 150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe2; 151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe3; 1520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 1540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper3; 155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe1.handle_0.is_valid()); 156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe2.handle_0.is_valid()); 157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe3.handle_0.is_valid()); 1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle_0); 1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher2; 167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper2.Start(&watcher2, test_pipe2.handle_0); 1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher3; 174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper3.Start(&watcher3, test_pipe3.handle_0); 1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 1760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 3 and make sure it's notified. 181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1)); 1820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper3.RunUntilGotCallback(); 1830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 1840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper3.got_callback()); 1860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper3.clear_callback(); 1870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and 3. Only 1 should be notified since 3 was is no longer 1890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // running. 190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1)); 191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1)); 1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.clear_callback(); 1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and 2. Only 2 should be notified (since 1 was already notified). 199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1)); 200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1)); 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) { 209f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe1; 210f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe2; 2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 213f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe1.handle_0.is_valid()); 214f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe2.handle_0.is_valid()); 2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 217f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle_0); 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; 223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper2.Start(&watcher2, test_pipe2.handle_0); 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. 229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1)); 2300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.clear_callback(); 234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, ReadFromHandle(test_pipe1.handle_0)); 2350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 2 and make sure it's notified. 237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1)); 2380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.RunUntilGotCallback(); 2390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper2.got_callback()); 2410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.clear_callback(); 2420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Listen on 1 again. 244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle_0); 2450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Write to 1 and make sure it's notified. 250f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1)); 2510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper1.RunUntilGotCallback(); 2520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper1.got_callback()); 2530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 2550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Verifies deadline is honored. 2570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(HandleWatcherTest, Deadline) { 2580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) InstallTickClock(); 2590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe1; 261f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe2; 262f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe3; 2630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper1; 2640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper2; 2650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) CallbackHelper callback_helper3; 266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe1.handle_0.is_valid()); 267f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe2.handle_0.is_valid()); 268f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ASSERT_TRUE(test_pipe3.handle_0.is_valid()); 2690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Add a watcher with an infinite timeout. 2710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher1; 272f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper1.Start(&watcher1, test_pipe1.handle_0); 2730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Add another watcher wth a timeout of 500 microseconds. 2790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher2; 280f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) watcher2.Start(test_pipe2.handle_0, MOJO_WAIT_FLAG_READABLE, 500, 2810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.GetCallback()); 2820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) RunUntilIdle(); 2830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper2.got_callback()); 2850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Advance the clock passed the deadline. We also have to start another 2880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // watcher to wake up the background thread. 2890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); 2900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) HandleWatcher watcher3; 292f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper3.Start(&watcher3, test_pipe3.handle_0); 2930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) callback_helper2.RunUntilGotCallback(); 2950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper1.got_callback()); 2960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_TRUE(callback_helper2.got_callback()); 2970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_FALSE(callback_helper3.got_callback()); 2980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 2990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(HandleWatcherTest, DeleteInCallback) { 301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) MessagePipe test_pipe; 302f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CallbackHelper callback_helper; 303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HandleWatcher* watcher = new HandleWatcher(); 305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.StartWithCallback(watcher, test_pipe.handle_1, 306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Bind(&DeleteWatcherAndForwardResult, 307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) watcher, 308f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.GetCallback())); 309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) WriteToHandle(test_pipe.handle_0); 310f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callback_helper.RunUntilGotCallback(); 311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) EXPECT_TRUE(callback_helper.got_callback()); 312f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 313f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 3140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace test 315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} // namespace common 3160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace mojo 317