Update V8 to version 4.7.21.
[chromium-blink-merge.git] / base / async_socket_io_handler_unittest.cc
blob721de9cd72d43c32583e9646cb029bb20c1a29b6
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
3 // found in the LICENSE file.
5 #include "base/async_socket_io_handler.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"
13 namespace {
14 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler";
15 const size_t kAsyncSocketIoTestStringLength =
16 arraysize(kAsyncSocketIoTestString);
18 class TestSocketReader {
19 public:
20 // Set |number_of_reads_before_quit| to >0 when you expect a specific number
21 // of Read operations to complete. Once that number is reached, the current
22 // message loop will be Quit(). Set |number_of_reads_before_quit| to -1 if
23 // callbacks should not be counted.
24 TestSocketReader(base::CancelableSyncSocket* socket,
25 int number_of_reads_before_quit,
26 bool issue_reads_from_callback,
27 bool expect_eof)
28 : socket_(socket), buffer_(),
29 number_of_reads_before_quit_(number_of_reads_before_quit),
30 callbacks_received_(0),
31 issue_reads_from_callback_(issue_reads_from_callback),
32 expect_eof_(expect_eof) {
33 io_handler.Initialize(socket_->handle(),
34 base::Bind(&TestSocketReader::OnRead,
35 base::Unretained(this)));
37 ~TestSocketReader() {}
39 bool IssueRead() {
40 return io_handler.Read(&buffer_[0], sizeof(buffer_));
43 const char* buffer() const { return &buffer_[0]; }
45 int callbacks_received() const { return callbacks_received_; }
47 private:
48 void OnRead(int bytes_read) {
49 if (!expect_eof_) {
50 EXPECT_GT(bytes_read, 0);
51 } else {
52 EXPECT_GE(bytes_read, 0);
54 ++callbacks_received_;
55 if (number_of_reads_before_quit_ == callbacks_received_) {
56 base::MessageLoop::current()->Quit();
57 } else if (issue_reads_from_callback_) {
58 IssueRead();
62 base::AsyncSocketIoHandler io_handler;
63 base::CancelableSyncSocket* socket_; // Ownership lies outside the class.
64 char buffer_[kAsyncSocketIoTestStringLength];
65 int number_of_reads_before_quit_;
66 int callbacks_received_;
67 bool issue_reads_from_callback_;
68 bool expect_eof_;
71 // Workaround to be able to use a base::Closure for sending data.
72 // Send() returns int but a closure must return void.
73 void SendData(base::CancelableSyncSocket* socket,
74 const void* buffer,
75 size_t length) {
76 socket->Send(buffer, length);
79 } // end namespace.
81 // Tests doing a pending read from a socket and use an IO handler to get
82 // notified of data.
83 TEST(AsyncSocketIoHandlerTest, AsynchronousReadWithMessageLoop) {
84 base::MessageLoopForIO loop;
86 base::CancelableSyncSocket pair[2];
87 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
89 TestSocketReader reader(&pair[0], 1, false, false);
90 EXPECT_TRUE(reader.IssueRead());
92 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength);
93 base::MessageLoop::current()->Run();
94 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0);
95 EXPECT_EQ(1, reader.callbacks_received());
98 // Tests doing a read from a socket when we know that there is data in the
99 // socket. Here we want to make sure that any async 'can read' notifications
100 // won't trip us off and that the synchronous case works as well.
101 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) {
102 base::MessageLoopForIO loop;
104 base::CancelableSyncSocket pair[2];
105 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
107 TestSocketReader reader(&pair[0], -1, false, false);
109 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength);
110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
111 FROM_HERE, base::MessageLoop::QuitClosure(),
112 base::TimeDelta::FromMilliseconds(100));
113 base::MessageLoop::current()->Run();
115 EXPECT_TRUE(reader.IssueRead());
116 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0);
117 // 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
119 // called asynchronously even though the read may have been done.
120 // 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
122 // received.
123 base::MessageLoop::current()->RunUntilIdle();
124 EXPECT_EQ(1, reader.callbacks_received());
127 // Calls Read() from within a callback to test that simple read "loops" work.
128 TEST(AsyncSocketIoHandlerTest, ReadFromCallback) {
129 base::MessageLoopForIO loop;
131 base::CancelableSyncSocket pair[2];
132 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
134 const int kReadOperationCount = 10;
135 TestSocketReader reader(&pair[0], kReadOperationCount, true, false);
136 EXPECT_TRUE(reader.IssueRead());
138 // Issue sends on an interval to satisfy the Read() requirements.
139 int64 milliseconds = 0;
140 for (int i = 0; i < kReadOperationCount; ++i) {
141 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
142 FROM_HERE, base::Bind(&SendData, &pair[1], kAsyncSocketIoTestString,
143 kAsyncSocketIoTestStringLength),
144 base::TimeDelta::FromMilliseconds(milliseconds));
145 milliseconds += 10;
148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
149 FROM_HERE, base::MessageLoop::QuitClosure(),
150 base::TimeDelta::FromMilliseconds(100 + milliseconds));
152 base::MessageLoop::current()->Run();
153 EXPECT_EQ(kReadOperationCount, reader.callbacks_received());
156 // Calls Read() then close other end, check that a correct callback is received.
157 TEST(AsyncSocketIoHandlerTest, ReadThenClose) {
158 base::MessageLoopForIO loop;
160 base::CancelableSyncSocket pair[2];
161 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
163 const int kReadOperationCount = 1;
164 TestSocketReader reader(&pair[0], kReadOperationCount, false, true);
165 EXPECT_TRUE(reader.IssueRead());
167 pair[1].Close();
169 base::MessageLoop::current()->Run();
170 EXPECT_EQ(kReadOperationCount, reader.callbacks_received());