Update V8 to version 4.7.58.
[chromium-blink-merge.git] / base / sync_socket_unittest.cc
blobb3aac6b54743123098e9cd5d1f6c92b1eb19257e
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/basictypes.h"
6 // TODO(ellyjones): Remove once http://crbug.com/523296 is fixed.
7 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR
8 #include "base/ios/ios_util.h"
9 #endif
10 #include "base/sync_socket.h"
11 #include "base/threading/simple_thread.h"
12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 const int kReceiveTimeoutInMilliseconds = 750;
19 class HangingReceiveThread : public base::DelegateSimpleThread::Delegate {
20 public:
21 explicit HangingReceiveThread(base::SyncSocket* socket)
22 : socket_(socket),
23 thread_(this, "HangingReceiveThread") {
24 thread_.Start();
27 ~HangingReceiveThread() override {}
29 void Run() override {
30 int data = 0;
31 ASSERT_EQ(socket_->Peek(), 0u);
33 // Use receive with timeout so we don't hang the test harness indefinitely.
34 ASSERT_EQ(0u, socket_->ReceiveWithTimeout(
35 &data, sizeof(data), base::TimeDelta::FromMilliseconds(
36 kReceiveTimeoutInMilliseconds)));
39 void Stop() {
40 thread_.Join();
43 private:
44 base::SyncSocket* socket_;
45 base::DelegateSimpleThread thread_;
47 DISALLOW_COPY_AND_ASSIGN(HangingReceiveThread);
50 // Tests sending data between two SyncSockets. Uses ASSERT() and thus will exit
51 // early upon failure. Callers should use ASSERT_NO_FATAL_FAILURE() if testing
52 // continues after return.
53 void SendReceivePeek(base::SyncSocket* socket_a, base::SyncSocket* socket_b) {
54 int received = 0;
55 const int kSending = 123;
56 COMPILE_ASSERT(sizeof(kSending) == sizeof(received), Invalid_Data_Size);
58 ASSERT_EQ(0u, socket_a->Peek());
59 ASSERT_EQ(0u, socket_b->Peek());
61 // Verify |socket_a| can send to |socket_a| and |socket_a| can Receive from
62 // |socket_a|.
63 ASSERT_EQ(sizeof(kSending), socket_a->Send(&kSending, sizeof(kSending)));
64 ASSERT_EQ(sizeof(kSending), socket_b->Peek());
65 ASSERT_EQ(sizeof(kSending), socket_b->Receive(&received, sizeof(kSending)));
66 ASSERT_EQ(kSending, received);
68 ASSERT_EQ(0u, socket_a->Peek());
69 ASSERT_EQ(0u, socket_b->Peek());
71 // Now verify the reverse.
72 received = 0;
73 ASSERT_EQ(sizeof(kSending), socket_b->Send(&kSending, sizeof(kSending)));
74 ASSERT_EQ(sizeof(kSending), socket_a->Peek());
75 ASSERT_EQ(sizeof(kSending), socket_a->Receive(&received, sizeof(kSending)));
76 ASSERT_EQ(kSending, received);
78 ASSERT_EQ(0u, socket_a->Peek());
79 ASSERT_EQ(0u, socket_b->Peek());
81 ASSERT_TRUE(socket_a->Close());
82 ASSERT_TRUE(socket_b->Close());
85 template <class SocketType>
86 void NormalSendReceivePeek() {
87 SocketType socket_a, socket_b;
88 ASSERT_TRUE(SocketType::CreatePair(&socket_a, &socket_b));
89 SendReceivePeek(&socket_a, &socket_b);
92 } // namespace
94 TEST(SyncSocket, NormalSendReceivePeek) {
95 NormalSendReceivePeek<base::SyncSocket>();
98 TEST(CancelableSyncSocket, NormalSendReceivePeek) {
99 NormalSendReceivePeek<base::CancelableSyncSocket>();
102 TEST(CancelableSyncSocket, CancelReceiveShutdown) {
103 // TODO(ellyjones): This test fails on iOS 7 devices. http://crbug.com/523296
104 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR
105 if (!base::ios::IsRunningOnIOS8OrLater())
106 return;
107 #endif
108 base::CancelableSyncSocket socket_a, socket_b;
109 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&socket_a, &socket_b));
111 base::TimeTicks start = base::TimeTicks::Now();
112 HangingReceiveThread thread(&socket_b);
113 ASSERT_TRUE(socket_b.Shutdown());
114 thread.Stop();
116 // Ensure the receive didn't just timeout.
117 ASSERT_LT((base::TimeTicks::Now() - start).InMilliseconds(),
118 kReceiveTimeoutInMilliseconds);
120 ASSERT_TRUE(socket_a.Close());
121 ASSERT_TRUE(socket_b.Close());