Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/browser/extensions/api/socket/udp_socket_unittest.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/extensions/api/socket/udp_socket.h" 5 #include "chrome/browser/extensions/api/socket/udp_socket.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 21 matching lines...) Expand all
32 } 32 }
33 33
34 static const char test_message[] = "$$TESTMESSAGETESTMESSAGETESTMESSAGETEST$$"; 34 static const char test_message[] = "$$TESTMESSAGETESTMESSAGETESTMESSAGETEST$$";
35 static const int test_message_length = ARRAYSIZE_UNSAFE(test_message); 35 static const int test_message_length = ARRAYSIZE_UNSAFE(test_message);
36 36
37 static void OnSendCompleted(int result) { 37 static void OnSendCompleted(int result) {
38 EXPECT_EQ(test_message_length, result); 38 EXPECT_EQ(test_message_length, result);
39 } 39 }
40 40
41 TEST(UDPSocketUnitTest, TestUDPSocketRecvFrom) { 41 TEST(UDPSocketUnitTest, TestUDPSocketRecvFrom) {
42 MessageLoopForIO io_loop; // For RecvFrom to do its threaded work. 42 base::MessageLoopForIO io_loop; // For RecvFrom to do its threaded work.
43 UDPSocket socket("abcdefghijklmnopqrst"); 43 UDPSocket socket("abcdefghijklmnopqrst");
44 44
45 // Confirm that we can call two RecvFroms in quick succession without 45 // Confirm that we can call two RecvFroms in quick succession without
46 // triggering crbug.com/146606. 46 // triggering crbug.com/146606.
47 socket.Connect("127.0.0.1", 40000, base::Bind(&OnConnected)); 47 socket.Connect("127.0.0.1", 40000, base::Bind(&OnConnected));
48 socket.RecvFrom(4096, base::Bind(&OnCompleted)); 48 socket.RecvFrom(4096, base::Bind(&OnCompleted));
49 socket.RecvFrom(4096, base::Bind(&OnCompleted)); 49 socket.RecvFrom(4096, base::Bind(&OnCompleted));
50 } 50 }
51 51
52 TEST(UDPSocketUnitTest, TestUDPMulticastJoinGroup) { 52 TEST(UDPSocketUnitTest, TestUDPMulticastJoinGroup) {
(...skipping 21 matching lines...) Expand all
74 } 74 }
75 75
76 TEST(UDPSocketUnitTest, TestUDPMulticastLoopbackMode) { 76 TEST(UDPSocketUnitTest, TestUDPMulticastLoopbackMode) {
77 const char* kGroup = "237.132.100.17"; 77 const char* kGroup = "237.132.100.17";
78 UDPSocket socket("abcdefghijklmnopqrst"); 78 UDPSocket socket("abcdefghijklmnopqrst");
79 EXPECT_EQ(0, socket.SetMulticastLoopbackMode(false)); 79 EXPECT_EQ(0, socket.SetMulticastLoopbackMode(false));
80 socket.Connect(kGroup, 13333, base::Bind(&OnConnected)); 80 socket.Connect(kGroup, 13333, base::Bind(&OnConnected));
81 } 81 }
82 82
83 static void QuitMessageLoop() { 83 static void QuitMessageLoop() {
84 MessageLoopForIO::current()->QuitNow(); 84 base::MessageLoopForIO::current()->QuitNow();
85 } 85 }
86 86
87 // Send a test multicast packet every second. 87 // Send a test multicast packet every second.
88 // Once the target socket received the packet, the message loop will exit. 88 // Once the target socket received the packet, the message loop will exit.
89 static void SendMulticastPacket(UDPSocket* src, int result) { 89 static void SendMulticastPacket(UDPSocket* src, int result) {
90 if (result == 0) { 90 if (result == 0) {
91 scoped_refptr<net::IOBuffer> data = new net::WrappedIOBuffer(test_message); 91 scoped_refptr<net::IOBuffer> data = new net::WrappedIOBuffer(test_message);
92 src->Write(data, test_message_length, base::Bind(&OnSendCompleted)); 92 src->Write(data, test_message_length, base::Bind(&OnSendCompleted));
93 MessageLoopForIO::current()->PostDelayedTask(FROM_HERE, 93 base::MessageLoopForIO::current()->PostDelayedTask(FROM_HERE,
94 base::Bind(&SendMulticastPacket, src, result), 94 base::Bind(&SendMulticastPacket, src, result),
95 base::TimeDelta::FromSeconds(1)); 95 base::TimeDelta::FromSeconds(1));
96 } else { 96 } else {
97 QuitMessageLoop(); 97 QuitMessageLoop();
98 FAIL() << "Failed to connect to multicast address. Error code: " << result; 98 FAIL() << "Failed to connect to multicast address. Error code: " << result;
99 } 99 }
100 } 100 }
101 101
102 static void OnMulticastReadCompleted(bool *packet_received, 102 static void OnMulticastReadCompleted(bool *packet_received,
103 int count, 103 int count,
104 scoped_refptr<net::IOBuffer> io_buffer) { 104 scoped_refptr<net::IOBuffer> io_buffer) {
105 EXPECT_EQ(test_message_length, count); 105 EXPECT_EQ(test_message_length, count);
106 EXPECT_EQ(0, strncmp(io_buffer->data(), test_message, test_message_length)); 106 EXPECT_EQ(0, strncmp(io_buffer->data(), test_message, test_message_length));
107 *packet_received = true; 107 *packet_received = true;
108 QuitMessageLoop(); 108 QuitMessageLoop();
109 } 109 }
110 110
111 TEST(UDPSocketUnitTest, TestUDPMulticastRecv) { 111 TEST(UDPSocketUnitTest, TestUDPMulticastRecv) {
112 const int kPort = 9999; 112 const int kPort = 9999;
113 const char* const kGroup = "237.132.100.17"; 113 const char* const kGroup = "237.132.100.17";
114 bool packet_received = false; 114 bool packet_received = false;
115 MessageLoopForIO io_loop; // For Read to do its threaded work. 115 base::MessageLoopForIO io_loop; // For Read to do its threaded work.
116 UDPSocket dest("abcdefghijklmnopqrst"); 116 UDPSocket dest("abcdefghijklmnopqrst");
117 UDPSocket src("abcdefghijklmnopqrst"); 117 UDPSocket src("abcdefghijklmnopqrst");
118 118
119 // Receiver 119 // Receiver
120 EXPECT_EQ(0, dest.Bind("0.0.0.0", kPort)); 120 EXPECT_EQ(0, dest.Bind("0.0.0.0", kPort));
121 EXPECT_EQ(0, dest.JoinGroup(kGroup)); 121 EXPECT_EQ(0, dest.JoinGroup(kGroup));
122 dest.Read(1024, base::Bind(&OnMulticastReadCompleted, &packet_received)); 122 dest.Read(1024, base::Bind(&OnMulticastReadCompleted, &packet_received));
123 123
124 // Sender 124 // Sender
125 EXPECT_EQ(0, src.SetMulticastTimeToLive(0)); 125 EXPECT_EQ(0, src.SetMulticastTimeToLive(0));
126 src.Connect(kGroup, kPort, base::Bind(&SendMulticastPacket, &src)); 126 src.Connect(kGroup, kPort, base::Bind(&SendMulticastPacket, &src));
127 127
128 // If not received within the test action timeout, quit the message loop. 128 // If not received within the test action timeout, quit the message loop.
129 io_loop.PostDelayedTask(FROM_HERE, 129 io_loop.PostDelayedTask(FROM_HERE,
130 base::Bind(&QuitMessageLoop), 130 base::Bind(&QuitMessageLoop),
131 TestTimeouts::action_timeout()); 131 TestTimeouts::action_timeout());
132 132
133 io_loop.Run(); 133 io_loop.Run();
134 134
135 EXPECT_TRUE(packet_received) << "Failed to receive from multicast address"; 135 EXPECT_TRUE(packet_received) << "Failed to receive from multicast address";
136 } 136 }
137 137
138 } // namespace extensions 138 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698