| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/async_socket_io_handler.h" | 5 #include "base/async_socket_io_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 9 |
| 13 namespace { | 10 namespace { |
| 14 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler"; | 11 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler"; |
| 15 const size_t kAsyncSocketIoTestStringLength = | 12 const size_t kAsyncSocketIoTestStringLength = |
| 16 arraysize(kAsyncSocketIoTestString); | 13 arraysize(kAsyncSocketIoTestString); |
| 17 | 14 |
| 18 class TestSocketReader { | 15 class TestSocketReader { |
| 19 public: | 16 public: |
| 20 // Set |number_of_reads_before_quit| to >0 when you expect a specific number | 17 // Set |number_of_reads_before_quit| to >0 when you expect a specific number |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // won't trip us off and that the synchronous case works as well. | 97 // won't trip us off and that the synchronous case works as well. |
| 101 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) { | 98 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) { |
| 102 base::MessageLoopForIO loop; | 99 base::MessageLoopForIO loop; |
| 103 | 100 |
| 104 base::CancelableSyncSocket pair[2]; | 101 base::CancelableSyncSocket pair[2]; |
| 105 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); | 102 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 106 | 103 |
| 107 TestSocketReader reader(&pair[0], -1, false, false); | 104 TestSocketReader reader(&pair[0], -1, false, false); |
| 108 | 105 |
| 109 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); | 106 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); |
| 110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 107 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 111 FROM_HERE, base::MessageLoop::QuitClosure(), | 108 base::MessageLoop::QuitClosure(), |
| 112 base::TimeDelta::FromMilliseconds(100)); | 109 base::TimeDelta::FromMilliseconds(100)); |
| 113 base::MessageLoop::current()->Run(); | 110 base::MessageLoop::current()->Run(); |
| 114 | 111 |
| 115 EXPECT_TRUE(reader.IssueRead()); | 112 EXPECT_TRUE(reader.IssueRead()); |
| 116 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); | 113 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); |
| 117 // We've now verified that the read happened synchronously, but it's not | 114 // We've now verified that the read happened synchronously, but it's not |
| 118 // guaranteed that the callback has been issued since the callback will be | 115 // guaranteed that the callback has been issued since the callback will be |
| 119 // called asynchronously even though the read may have been done. | 116 // called asynchronously even though the read may have been done. |
| 120 // So we call RunUntilIdle() to allow any event notifications or APC's on | 117 // So we call RunUntilIdle() to allow any event notifications or APC's on |
| 121 // Windows, to execute before checking the count of how many callbacks we've | 118 // Windows, to execute before checking the count of how many callbacks we've |
| 122 // received. | 119 // received. |
| 123 base::MessageLoop::current()->RunUntilIdle(); | 120 base::MessageLoop::current()->RunUntilIdle(); |
| 124 EXPECT_EQ(1, reader.callbacks_received()); | 121 EXPECT_EQ(1, reader.callbacks_received()); |
| 125 } | 122 } |
| 126 | 123 |
| 127 // Calls Read() from within a callback to test that simple read "loops" work. | 124 // Calls Read() from within a callback to test that simple read "loops" work. |
| 128 TEST(AsyncSocketIoHandlerTest, ReadFromCallback) { | 125 TEST(AsyncSocketIoHandlerTest, ReadFromCallback) { |
| 129 base::MessageLoopForIO loop; | 126 base::MessageLoopForIO loop; |
| 130 | 127 |
| 131 base::CancelableSyncSocket pair[2]; | 128 base::CancelableSyncSocket pair[2]; |
| 132 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); | 129 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 133 | 130 |
| 134 const int kReadOperationCount = 10; | 131 const int kReadOperationCount = 10; |
| 135 TestSocketReader reader(&pair[0], kReadOperationCount, true, false); | 132 TestSocketReader reader(&pair[0], kReadOperationCount, true, false); |
| 136 EXPECT_TRUE(reader.IssueRead()); | 133 EXPECT_TRUE(reader.IssueRead()); |
| 137 | 134 |
| 138 // Issue sends on an interval to satisfy the Read() requirements. | 135 // Issue sends on an interval to satisfy the Read() requirements. |
| 139 int64 milliseconds = 0; | 136 int64 milliseconds = 0; |
| 140 for (int i = 0; i < kReadOperationCount; ++i) { | 137 for (int i = 0; i < kReadOperationCount; ++i) { |
| 141 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 138 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 142 FROM_HERE, base::Bind(&SendData, &pair[1], kAsyncSocketIoTestString, | 139 base::Bind(&SendData, &pair[1], kAsyncSocketIoTestString, |
| 143 kAsyncSocketIoTestStringLength), | 140 kAsyncSocketIoTestStringLength), |
| 144 base::TimeDelta::FromMilliseconds(milliseconds)); | 141 base::TimeDelta::FromMilliseconds(milliseconds)); |
| 145 milliseconds += 10; | 142 milliseconds += 10; |
| 146 } | 143 } |
| 147 | 144 |
| 148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 145 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 149 FROM_HERE, base::MessageLoop::QuitClosure(), | 146 base::MessageLoop::QuitClosure(), |
| 150 base::TimeDelta::FromMilliseconds(100 + milliseconds)); | 147 base::TimeDelta::FromMilliseconds(100 + milliseconds)); |
| 151 | 148 |
| 152 base::MessageLoop::current()->Run(); | 149 base::MessageLoop::current()->Run(); |
| 153 EXPECT_EQ(kReadOperationCount, reader.callbacks_received()); | 150 EXPECT_EQ(kReadOperationCount, reader.callbacks_received()); |
| 154 } | 151 } |
| 155 | 152 |
| 156 // Calls Read() then close other end, check that a correct callback is received. | 153 // Calls Read() then close other end, check that a correct callback is received. |
| 157 TEST(AsyncSocketIoHandlerTest, ReadThenClose) { | 154 TEST(AsyncSocketIoHandlerTest, ReadThenClose) { |
| 158 base::MessageLoopForIO loop; | 155 base::MessageLoopForIO loop; |
| 159 | 156 |
| 160 base::CancelableSyncSocket pair[2]; | 157 base::CancelableSyncSocket pair[2]; |
| 161 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); | 158 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
| 162 | 159 |
| 163 const int kReadOperationCount = 1; | 160 const int kReadOperationCount = 1; |
| 164 TestSocketReader reader(&pair[0], kReadOperationCount, false, true); | 161 TestSocketReader reader(&pair[0], kReadOperationCount, false, true); |
| 165 EXPECT_TRUE(reader.IssueRead()); | 162 EXPECT_TRUE(reader.IssueRead()); |
| 166 | 163 |
| 167 pair[1].Close(); | 164 pair[1].Close(); |
| 168 | 165 |
| 169 base::MessageLoop::current()->Run(); | 166 base::MessageLoop::current()->Run(); |
| 170 EXPECT_EQ(kReadOperationCount, reader.callbacks_received()); | 167 EXPECT_EQ(kReadOperationCount, reader.callbacks_received()); |
| 171 } | 168 } |
| OLD | NEW |