| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/public/environment/default_async_waiter.h" | |
| 6 | |
| 7 #include "mojo/public/environment/environment.h" | |
| 8 #include "mojo/public/system/core_cpp.h" | |
| 9 #include "mojo/public/system/macros.h" | |
| 10 #include "mojo/public/tests/test_support.h" | |
| 11 #include "mojo/public/utility/run_loop.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace { | |
| 16 | |
| 17 class TestAsyncWaitCallback { | |
| 18 public: | |
| 19 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { | |
| 20 } | |
| 21 virtual ~TestAsyncWaitCallback() {} | |
| 22 | |
| 23 int result_count() const { return result_count_; } | |
| 24 | |
| 25 MojoResult last_result() const { return last_result_; } | |
| 26 | |
| 27 // MojoAsyncWaitCallback: | |
| 28 static void OnHandleReady(void* closure, MojoResult result) { | |
| 29 TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure); | |
| 30 self->result_count_++; | |
| 31 self->last_result_ = result; | |
| 32 } | |
| 33 | |
| 34 private: | |
| 35 int result_count_; | |
| 36 MojoResult last_result_; | |
| 37 | |
| 38 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | |
| 39 }; | |
| 40 | |
| 41 MojoAsyncWaitID CallAsyncWait(const Handle& handle, | |
| 42 MojoWaitFlags flags, | |
| 43 TestAsyncWaitCallback* callback) { | |
| 44 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); | |
| 45 return waiter->AsyncWait(waiter, | |
| 46 handle.value(), | |
| 47 flags, | |
| 48 MOJO_DEADLINE_INDEFINITE, | |
| 49 &TestAsyncWaitCallback::OnHandleReady, | |
| 50 callback); | |
| 51 } | |
| 52 | |
| 53 void CallCancelWait(MojoAsyncWaitID wait_id) { | |
| 54 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); | |
| 55 waiter->CancelWait(waiter, wait_id); | |
| 56 } | |
| 57 | |
| 58 class AsyncWaiterTest : public testing::Test { | |
| 59 public: | |
| 60 AsyncWaiterTest() {} | |
| 61 | |
| 62 private: | |
| 63 Environment environment_; | |
| 64 RunLoop run_loop_; | |
| 65 | |
| 66 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); | |
| 67 }; | |
| 68 | |
| 69 // Verifies AsyncWaitCallback is notified when pipe is ready. | |
| 70 TEST_F(AsyncWaiterTest, CallbackNotified) { | |
| 71 TestAsyncWaitCallback callback; | |
| 72 MessagePipe test_pipe; | |
| 73 EXPECT_EQ(MOJO_RESULT_OK, | |
| 74 test::WriteEmptyMessage(test_pipe.handle1.get())); | |
| 75 | |
| 76 CallAsyncWait(test_pipe.handle0.get(), | |
| 77 MOJO_WAIT_FLAG_READABLE, | |
| 78 &callback); | |
| 79 RunLoop::current()->Run(); | |
| 80 EXPECT_EQ(1, callback.result_count()); | |
| 81 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | |
| 82 } | |
| 83 | |
| 84 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | |
| 85 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { | |
| 86 TestAsyncWaitCallback callback1; | |
| 87 TestAsyncWaitCallback callback2; | |
| 88 MessagePipe test_pipe1; | |
| 89 MessagePipe test_pipe2; | |
| 90 EXPECT_EQ(MOJO_RESULT_OK, | |
| 91 test::WriteEmptyMessage(test_pipe1.handle1.get())); | |
| 92 EXPECT_EQ(MOJO_RESULT_OK, | |
| 93 test::WriteEmptyMessage(test_pipe2.handle1.get())); | |
| 94 | |
| 95 CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1); | |
| 96 CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2); | |
| 97 | |
| 98 RunLoop::current()->Run(); | |
| 99 EXPECT_EQ(1, callback1.result_count()); | |
| 100 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | |
| 101 EXPECT_EQ(1, callback2.result_count()); | |
| 102 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | |
| 103 } | |
| 104 | |
| 105 // Verifies cancel works. | |
| 106 TEST_F(AsyncWaiterTest, CancelCallback) { | |
| 107 TestAsyncWaitCallback callback; | |
| 108 MessagePipe test_pipe; | |
| 109 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | |
| 110 | |
| 111 CallCancelWait( | |
| 112 CallAsyncWait(test_pipe.handle0.get(), | |
| 113 MOJO_WAIT_FLAG_READABLE, | |
| 114 &callback)); | |
| 115 RunLoop::current()->Run(); | |
| 116 EXPECT_EQ(0, callback.result_count()); | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| 120 } // namespace mojo | |
| OLD | NEW |