| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/shell/handle_watcher.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/test/simple_test_tick_clock.h" | |
| 11 #include "mojo/shell/scoped_message_pipe.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace shell { | |
| 16 namespace test { | |
| 17 | |
| 18 MojoResult WriteToHandle(MojoHandle handle) { | |
| 19 return MojoWriteMessage(handle, NULL, 0, NULL, 0, | |
| 20 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 21 } | |
| 22 | |
| 23 MojoResult ReadFromHandle(MojoHandle handle) { | |
| 24 uint32_t num_bytes = 0; | |
| 25 uint32_t num_handles = 0; | |
| 26 return MojoReadMessage(handle, NULL, &num_bytes, NULL, &num_handles, | |
| 27 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | |
| 28 } | |
| 29 | |
| 30 void RunUntilIdle() { | |
| 31 base::RunLoop run_loop; | |
| 32 run_loop.RunUntilIdle(); | |
| 33 } | |
| 34 | |
| 35 // Helper class to manage the callback and running the message loop waiting for | |
| 36 // message to be received. Typical usage is something like: | |
| 37 // Schedule callback returned from GetCallback(). | |
| 38 // RunUntilGotCallback(); | |
| 39 // EXPECT_TRUE(got_callback()); | |
| 40 // clear_callback(); | |
| 41 class CallbackHelper { | |
| 42 public: | |
| 43 CallbackHelper() | |
| 44 : got_callback_(false), | |
| 45 run_loop_(NULL), | |
| 46 weak_factory_(this) {} | |
| 47 ~CallbackHelper() {} | |
| 48 | |
| 49 // See description above |got_callback_|. | |
| 50 bool got_callback() const { return got_callback_; } | |
| 51 void clear_callback() { got_callback_ = false; } | |
| 52 | |
| 53 // Runs the current MessageLoop until the callback returned from GetCallback() | |
| 54 // is notified. | |
| 55 void RunUntilGotCallback() { | |
| 56 ASSERT_TRUE(run_loop_ == NULL); | |
| 57 base::RunLoop run_loop; | |
| 58 base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop); | |
| 59 run_loop.Run(); | |
| 60 } | |
| 61 | |
| 62 base::Closure GetCallback() { | |
| 63 return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr()); | |
| 64 } | |
| 65 | |
| 66 void Start(HandleWatcher* watcher, MojoHandle handle) { | |
| 67 watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE, | |
| 68 GetCallback()); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 void OnCallback() { | |
| 73 got_callback_ = true; | |
| 74 if (run_loop_) | |
| 75 run_loop_->Quit(); | |
| 76 } | |
| 77 | |
| 78 // Set to true when the callback is called. | |
| 79 bool got_callback_; | |
| 80 | |
| 81 // If non-NULL we're in RunUntilGotCallback(). | |
| 82 base::RunLoop* run_loop_; | |
| 83 | |
| 84 base::WeakPtrFactory<CallbackHelper> weak_factory_; | |
| 85 | |
| 86 private: | |
| 87 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); | |
| 88 }; | |
| 89 | |
| 90 class HandleWatcherTest : public testing::Test { | |
| 91 public: | |
| 92 HandleWatcherTest() {} | |
| 93 virtual ~HandleWatcherTest() { | |
| 94 HandleWatcher::tick_clock_ = NULL; | |
| 95 } | |
| 96 | |
| 97 protected: | |
| 98 void InstallTickClock() { | |
| 99 HandleWatcher::tick_clock_ = &tick_clock_; | |
| 100 } | |
| 101 | |
| 102 base::SimpleTestTickClock tick_clock_; | |
| 103 | |
| 104 private: | |
| 105 DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest); | |
| 106 }; | |
| 107 | |
| 108 // Trivial test case with a single handle to watch. | |
| 109 TEST_F(HandleWatcherTest, SingleHandler) { | |
| 110 ScopedMessagePipe test_pipe; | |
| 111 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe.handle_0()); | |
| 112 CallbackHelper callback_helper; | |
| 113 HandleWatcher watcher; | |
| 114 callback_helper.Start(&watcher, test_pipe.handle_0()); | |
| 115 RunUntilIdle(); | |
| 116 EXPECT_FALSE(callback_helper.got_callback()); | |
| 117 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe.handle_1())); | |
| 118 callback_helper.RunUntilGotCallback(); | |
| 119 EXPECT_TRUE(callback_helper.got_callback()); | |
| 120 } | |
| 121 | |
| 122 // Creates three handles and notfies them in reverse order ensuring each one is | |
| 123 // notified appropriately. | |
| 124 TEST_F(HandleWatcherTest, ThreeHandles) { | |
| 125 ScopedMessagePipe test_pipe1; | |
| 126 ScopedMessagePipe test_pipe2; | |
| 127 ScopedMessagePipe test_pipe3; | |
| 128 CallbackHelper callback_helper1; | |
| 129 CallbackHelper callback_helper2; | |
| 130 CallbackHelper callback_helper3; | |
| 131 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0()); | |
| 132 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0()); | |
| 133 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe3.handle_0()); | |
| 134 | |
| 135 HandleWatcher watcher1; | |
| 136 callback_helper1.Start(&watcher1, test_pipe1.handle_0()); | |
| 137 RunUntilIdle(); | |
| 138 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 139 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 140 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 141 | |
| 142 HandleWatcher watcher2; | |
| 143 callback_helper2.Start(&watcher2, test_pipe2.handle_0()); | |
| 144 RunUntilIdle(); | |
| 145 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 146 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 147 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 148 | |
| 149 HandleWatcher watcher3; | |
| 150 callback_helper3.Start(&watcher3, test_pipe3.handle_0()); | |
| 151 RunUntilIdle(); | |
| 152 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 153 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 154 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 155 | |
| 156 // Write to 3 and make sure it's notified. | |
| 157 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1())); | |
| 158 callback_helper3.RunUntilGotCallback(); | |
| 159 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 160 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 161 EXPECT_TRUE(callback_helper3.got_callback()); | |
| 162 callback_helper3.clear_callback(); | |
| 163 | |
| 164 // Write to 1 and 3. Only 1 should be notified since 3 was is no longer | |
| 165 // running. | |
| 166 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1())); | |
| 167 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe3.handle_1())); | |
| 168 callback_helper1.RunUntilGotCallback(); | |
| 169 EXPECT_TRUE(callback_helper1.got_callback()); | |
| 170 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 171 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 172 callback_helper1.clear_callback(); | |
| 173 | |
| 174 // Write to 1 and 2. Only 2 should be notified (since 1 was already notified). | |
| 175 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1())); | |
| 176 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1())); | |
| 177 callback_helper2.RunUntilGotCallback(); | |
| 178 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 179 EXPECT_TRUE(callback_helper2.got_callback()); | |
| 180 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 181 } | |
| 182 | |
| 183 // Verifies Start() invoked a second time works. | |
| 184 TEST_F(HandleWatcherTest, Restart) { | |
| 185 ScopedMessagePipe test_pipe1; | |
| 186 ScopedMessagePipe test_pipe2; | |
| 187 CallbackHelper callback_helper1; | |
| 188 CallbackHelper callback_helper2; | |
| 189 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0()); | |
| 190 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0()); | |
| 191 | |
| 192 HandleWatcher watcher1; | |
| 193 callback_helper1.Start(&watcher1, test_pipe1.handle_0()); | |
| 194 RunUntilIdle(); | |
| 195 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 196 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 197 | |
| 198 HandleWatcher watcher2; | |
| 199 callback_helper2.Start(&watcher2, test_pipe2.handle_0()); | |
| 200 RunUntilIdle(); | |
| 201 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 202 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 203 | |
| 204 // Write to 1 and make sure it's notified. | |
| 205 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1())); | |
| 206 callback_helper1.RunUntilGotCallback(); | |
| 207 EXPECT_TRUE(callback_helper1.got_callback()); | |
| 208 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 209 callback_helper1.clear_callback(); | |
| 210 EXPECT_EQ(MOJO_RESULT_OK, ReadFromHandle(test_pipe1.handle_0())); | |
| 211 | |
| 212 // Write to 2 and make sure it's notified. | |
| 213 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe2.handle_1())); | |
| 214 callback_helper2.RunUntilGotCallback(); | |
| 215 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 216 EXPECT_TRUE(callback_helper2.got_callback()); | |
| 217 callback_helper2.clear_callback(); | |
| 218 | |
| 219 // Listen on 1 again. | |
| 220 callback_helper1.Start(&watcher1, test_pipe1.handle_0()); | |
| 221 RunUntilIdle(); | |
| 222 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 223 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 224 | |
| 225 // Write to 1 and make sure it's notified. | |
| 226 EXPECT_EQ(MOJO_RESULT_OK, WriteToHandle(test_pipe1.handle_1())); | |
| 227 callback_helper1.RunUntilGotCallback(); | |
| 228 EXPECT_TRUE(callback_helper1.got_callback()); | |
| 229 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 230 } | |
| 231 | |
| 232 // Verifies deadline is honored. | |
| 233 TEST_F(HandleWatcherTest, Deadline) { | |
| 234 InstallTickClock(); | |
| 235 | |
| 236 ScopedMessagePipe test_pipe1; | |
| 237 ScopedMessagePipe test_pipe2; | |
| 238 ScopedMessagePipe test_pipe3; | |
| 239 CallbackHelper callback_helper1; | |
| 240 CallbackHelper callback_helper2; | |
| 241 CallbackHelper callback_helper3; | |
| 242 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe1.handle_0()); | |
| 243 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe2.handle_0()); | |
| 244 ASSERT_NE(MOJO_HANDLE_INVALID, test_pipe3.handle_0()); | |
| 245 | |
| 246 // Add a watcher with an infinite timeout. | |
| 247 HandleWatcher watcher1; | |
| 248 callback_helper1.Start(&watcher1, test_pipe1.handle_0()); | |
| 249 RunUntilIdle(); | |
| 250 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 251 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 252 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 253 | |
| 254 // Add another watcher wth a timeout of 500 microseconds. | |
| 255 HandleWatcher watcher2; | |
| 256 watcher2.Start(test_pipe2.handle_0(), MOJO_WAIT_FLAG_READABLE, 500, | |
| 257 callback_helper2.GetCallback()); | |
| 258 RunUntilIdle(); | |
| 259 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 260 EXPECT_FALSE(callback_helper2.got_callback()); | |
| 261 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 262 | |
| 263 // Advance the clock passed the deadline. We also have to start another | |
| 264 // watcher to wake up the background thread. | |
| 265 tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501)); | |
| 266 | |
| 267 HandleWatcher watcher3; | |
| 268 callback_helper3.Start(&watcher3, test_pipe3.handle_0()); | |
| 269 | |
| 270 callback_helper2.RunUntilGotCallback(); | |
| 271 EXPECT_FALSE(callback_helper1.got_callback()); | |
| 272 EXPECT_TRUE(callback_helper2.got_callback()); | |
| 273 EXPECT_FALSE(callback_helper3.got_callback()); | |
| 274 } | |
| 275 | |
| 276 } // namespace test | |
| 277 } // namespace shell | |
| 278 } // namespace mojo | |
| OLD | NEW |