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