OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/sync_socket.h" | 5 #include "base/sync_socket.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <sstream> |
8 #include <string> | 9 #include <string> |
9 #include <sstream> | |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/location.h" |
| 13 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
14 #include "ipc/ipc_test_base.h" | 15 #include "ipc/ipc_test_base.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
18 #include "base/file_descriptor_posix.h" | 19 #include "base/file_descriptor_posix.h" |
19 #endif | 20 #endif |
20 | 21 |
21 // IPC messages for testing ---------------------------------------------------- | 22 // IPC messages for testing ---------------------------------------------------- |
22 | 23 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 TEST_F(SyncSocketTest, DisconnectTest) { | 221 TEST_F(SyncSocketTest, DisconnectTest) { |
221 base::CancelableSyncSocket pair[2]; | 222 base::CancelableSyncSocket pair[2]; |
222 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); | 223 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
223 | 224 |
224 base::Thread worker("BlockingThread"); | 225 base::Thread worker("BlockingThread"); |
225 worker.Start(); | 226 worker.Start(); |
226 | 227 |
227 // Try to do a blocking read from one of the sockets on the worker thread. | 228 // Try to do a blocking read from one of the sockets on the worker thread. |
228 char buf[0xff]; | 229 char buf[0xff]; |
229 size_t received = 1U; // Initialize to an unexpected value. | 230 size_t received = 1U; // Initialize to an unexpected value. |
230 worker.message_loop()->PostTask(FROM_HERE, | 231 worker.task_runner()->PostTask( |
| 232 FROM_HERE, |
231 base::Bind(&BlockingRead, &pair[0], &buf[0], arraysize(buf), &received)); | 233 base::Bind(&BlockingRead, &pair[0], &buf[0], arraysize(buf), &received)); |
232 | 234 |
233 // Wait for the worker thread to say hello. | 235 // Wait for the worker thread to say hello. |
234 char hello[kHelloStringLength] = {0}; | 236 char hello[kHelloStringLength] = {0}; |
235 pair[1].Receive(&hello[0], sizeof(hello)); | 237 pair[1].Receive(&hello[0], sizeof(hello)); |
236 EXPECT_EQ(0, strcmp(hello, kHelloString)); | 238 EXPECT_EQ(0, strcmp(hello, kHelloString)); |
237 // Give the worker a chance to start Receive(). | 239 // Give the worker a chance to start Receive(). |
238 base::PlatformThread::YieldCurrentThread(); | 240 base::PlatformThread::YieldCurrentThread(); |
239 | 241 |
240 // Now shut down the socket that the thread is issuing a blocking read on | 242 // Now shut down the socket that the thread is issuing a blocking read on |
241 // which should cause Receive to return with an error. | 243 // which should cause Receive to return with an error. |
242 pair[0].Shutdown(); | 244 pair[0].Shutdown(); |
243 | 245 |
244 worker.Stop(); | 246 worker.Stop(); |
245 | 247 |
246 EXPECT_EQ(0U, received); | 248 EXPECT_EQ(0U, received); |
247 } | 249 } |
248 | 250 |
249 // Tests that read is a blocking operation. | 251 // Tests that read is a blocking operation. |
250 TEST_F(SyncSocketTest, BlockingReceiveTest) { | 252 TEST_F(SyncSocketTest, BlockingReceiveTest) { |
251 base::CancelableSyncSocket pair[2]; | 253 base::CancelableSyncSocket pair[2]; |
252 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); | 254 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); |
253 | 255 |
254 base::Thread worker("BlockingThread"); | 256 base::Thread worker("BlockingThread"); |
255 worker.Start(); | 257 worker.Start(); |
256 | 258 |
257 // Try to do a blocking read from one of the sockets on the worker thread. | 259 // Try to do a blocking read from one of the sockets on the worker thread. |
258 char buf[kHelloStringLength] = {0}; | 260 char buf[kHelloStringLength] = {0}; |
259 size_t received = 1U; // Initialize to an unexpected value. | 261 size_t received = 1U; // Initialize to an unexpected value. |
260 worker.message_loop()->PostTask(FROM_HERE, | 262 worker.task_runner()->PostTask(FROM_HERE, |
261 base::Bind(&BlockingRead, &pair[0], &buf[0], | 263 base::Bind(&BlockingRead, &pair[0], &buf[0], |
262 kHelloStringLength, &received)); | 264 kHelloStringLength, &received)); |
263 | 265 |
264 // Wait for the worker thread to say hello. | 266 // Wait for the worker thread to say hello. |
265 char hello[kHelloStringLength] = {0}; | 267 char hello[kHelloStringLength] = {0}; |
266 pair[1].Receive(&hello[0], sizeof(hello)); | 268 pair[1].Receive(&hello[0], sizeof(hello)); |
267 EXPECT_EQ(0, strcmp(hello, kHelloString)); | 269 EXPECT_EQ(0, strcmp(hello, kHelloString)); |
268 // Give the worker a chance to start Receive(). | 270 // Give the worker a chance to start Receive(). |
269 base::PlatformThread::YieldCurrentThread(); | 271 base::PlatformThread::YieldCurrentThread(); |
270 | 272 |
271 // Send a message to the socket on the blocking thead, it should free the | 273 // Send a message to the socket on the blocking thead, it should free the |
272 // socket from Receive(). | 274 // socket from Receive(). |
(...skipping 26 matching lines...) Expand all Loading... |
299 | 301 |
300 // Read from another socket to free some space for a new write. | 302 // Read from another socket to free some space for a new write. |
301 char hello[kHelloStringLength] = {0}; | 303 char hello[kHelloStringLength] = {0}; |
302 pair[1].Receive(&hello[0], sizeof(hello)); | 304 pair[1].Receive(&hello[0], sizeof(hello)); |
303 | 305 |
304 // Should be able to write more data to the buffer now. | 306 // Should be able to write more data to the buffer now. |
305 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); | 307 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); |
306 } | 308 } |
307 | 309 |
308 } // namespace | 310 } // namespace |
OLD | NEW |