OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "media/audio/async_socket_io_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace { |
| 11 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler"; |
| 12 const size_t kAsyncSocketIoTestStringLength = |
| 13 arraysize(kAsyncSocketIoTestString); |
| 14 |
| 15 class TestSocketReader { |
| 16 public: |
| 17 TestSocketReader(base::CancelableSyncSocket* socket, bool quit_on_read) |
| 18 : socket_(socket), buffer_(), quit_on_read_(quit_on_read) { |
| 19 io_handler.Initialize(socket_->handle()); |
| 20 } |
| 21 ~TestSocketReader() {} |
| 22 |
| 23 bool IssueRead() { |
| 24 return io_handler.Read(&buffer_[0], sizeof(buffer_), |
| 25 base::Bind(&TestSocketReader::OnRead, |
| 26 base::Unretained(this))); |
| 27 } |
| 28 |
| 29 const char* buffer() const { return &buffer_[0]; } |
| 30 |
| 31 private: |
| 32 void OnRead(int bytes_read) { |
| 33 EXPECT_GT(bytes_read, 0); |
| 34 if (quit_on_read_) |
| 35 MessageLoop::current()->Quit(); |
| 36 } |
| 37 |
| 38 media::AsyncSocketIoHandler io_handler; |
| 39 base::CancelableSyncSocket* socket_; // Ownership lies outside the class. |
| 40 char buffer_[kAsyncSocketIoTestStringLength]; |
| 41 bool quit_on_read_; |
| 42 }; |
| 43 |
| 44 } // end namespace. |
| 45 |
| 46 // Tests doing a pending read from a socket and use an IO handler to get |
| 47 // notified of data. |
| 48 TEST(AsyncSocketIoHandlerTest, AsynchronousReadWithMessageLoop) { |
| 49 MessageLoopForIO loop; |
| 50 |
| 51 base::CancelableSyncSocket pair[2]; |
| 52 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 53 |
| 54 TestSocketReader reader(&pair[0], true); |
| 55 EXPECT_TRUE(reader.IssueRead()); |
| 56 |
| 57 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); |
| 58 MessageLoop::current()->Run(); |
| 59 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); |
| 60 } |
| 61 |
| 62 // Tests doing a read from a socket when we know that there is data in the |
| 63 // socket. Here we want to make sure that any async 'can read' notifications |
| 64 // won't trip us off and that the synchronous case works as well. |
| 65 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) { |
| 66 MessageLoopForIO loop; |
| 67 |
| 68 base::CancelableSyncSocket pair[2]; |
| 69 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 70 |
| 71 TestSocketReader reader(&pair[0], false); |
| 72 |
| 73 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); |
| 74 MessageLoop::current()->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
| 75 base::TimeDelta::FromMilliseconds(100)); |
| 76 MessageLoop::current()->Run(); |
| 77 |
| 78 EXPECT_TRUE(reader.IssueRead()); |
| 79 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); |
| 80 } |
OLD | NEW |