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