| 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 <string> | |
| 6 | |
| 7 #include "mojo/public/c/environment/async_waiter.h" | |
| 8 #include "mojo/public/cpp/environment/environment.h" | |
| 9 #include "mojo/public/cpp/system/core.h" | |
| 10 #include "mojo/public/cpp/system/macros.h" | |
| 11 #include "mojo/public/cpp/test_support/test_utils.h" | |
| 12 #include "mojo/public/cpp/utility/run_loop.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace { | |
| 17 | |
| 18 class TestAsyncWaitCallback { | |
| 19 public: | |
| 20 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {} | |
| 21 ~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 MojoHandleSignals signals, | |
| 43 TestAsyncWaitCallback* callback) { | |
| 44 return Environment::GetDefaultAsyncWaiter()->AsyncWait( | |
| 45 handle.value(), | |
| 46 signals, | |
| 47 MOJO_DEADLINE_INDEFINITE, | |
| 48 &TestAsyncWaitCallback::OnHandleReady, | |
| 49 callback); | |
| 50 } | |
| 51 | |
| 52 void CallCancelWait(MojoAsyncWaitID wait_id) { | |
| 53 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id); | |
| 54 } | |
| 55 | |
| 56 class AsyncWaitTest : public testing::Test { | |
| 57 public: | |
| 58 AsyncWaitTest() {} | |
| 59 | |
| 60 private: | |
| 61 Environment environment_; | |
| 62 RunLoop run_loop_; | |
| 63 | |
| 64 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaitTest); | |
| 65 }; | |
| 66 | |
| 67 // Verifies AsyncWaitCallback is notified when pipe is ready. | |
| 68 TEST_F(AsyncWaitTest, CallbackNotified) { | |
| 69 TestAsyncWaitCallback callback; | |
| 70 MessagePipe test_pipe; | |
| 71 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
| 72 | |
| 73 CallAsyncWait( | |
| 74 test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback); | |
| 75 RunLoop::current()->Run(); | |
| 76 EXPECT_EQ(1, callback.result_count()); | |
| 77 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | |
| 78 } | |
| 79 | |
| 80 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | |
| 81 TEST_F(AsyncWaitTest, TwoCallbacksNotified) { | |
| 82 TestAsyncWaitCallback callback1; | |
| 83 TestAsyncWaitCallback callback2; | |
| 84 MessagePipe test_pipe1; | |
| 85 MessagePipe test_pipe2; | |
| 86 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); | |
| 87 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); | |
| 88 | |
| 89 CallAsyncWait( | |
| 90 test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback1); | |
| 91 CallAsyncWait( | |
| 92 test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback2); | |
| 93 | |
| 94 RunLoop::current()->Run(); | |
| 95 EXPECT_EQ(1, callback1.result_count()); | |
| 96 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | |
| 97 EXPECT_EQ(1, callback2.result_count()); | |
| 98 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | |
| 99 } | |
| 100 | |
| 101 // Verifies cancel works. | |
| 102 TEST_F(AsyncWaitTest, CancelCallback) { | |
| 103 TestAsyncWaitCallback callback; | |
| 104 MessagePipe test_pipe; | |
| 105 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
| 106 | |
| 107 CallCancelWait(CallAsyncWait( | |
| 108 test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback)); | |
| 109 RunLoop::current()->Run(); | |
| 110 EXPECT_EQ(0, callback.result_count()); | |
| 111 } | |
| 112 | |
| 113 } // namespace | |
| 114 } // namespace mojo | |
| OLD | NEW |