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

Side by Side Diff: chrome/test/data/extensions/api_test/socket/api/background.js

Issue 12684008: Multicast socket API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase lkgr Created 7 years, 9 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 // net/tools/testserver/testserver.py is picky about the format of what it 5 // net/tools/testserver/testserver.py is picky about the format of what it
6 // calls its "echo" messages. One might go so far as to mutter to oneself that 6 // calls its "echo" messages. One might go so far as to mutter to oneself that
7 // it isn't an echo server at all. 7 // it isn't an echo server at all.
8 // 8 //
9 // The response is based on the request but obfuscated using a random key. 9 // The response is based on the request but obfuscated using a random key.
10 const request = "0100000005320000005hello"; 10 const request = "0100000005320000005hello";
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 227 }
228 228
229 function onServerSocketCreate(socketInfo) { 229 function onServerSocketCreate(socketInfo) {
230 socketId = socketInfo.socketId; 230 socketId = socketInfo.socketId;
231 socket.listen(socketId, address, port, onListen); 231 socket.listen(socketId, address, port, onListen);
232 } 232 }
233 233
234 socket.create('tcp', {}, onServerSocketCreate); 234 socket.create('tcp', {}, onServerSocketCreate);
235 }; 235 };
236 236
237 function testMulticast() {
238 var kMulticastAddress = "237.132.100." + (Math.random() * 250 >> 0);
scheib 2013/04/02 18:25:17 Random in tests tends to result in flaky tests. Pi
Bei Zhang 2013/04/05 00:38:59 My concern is, as multiple slaves might be running
scheib 2013/04/05 17:41:04 Good concern, I don't know a good solution, consul
239 var kPort = 9000 + (Math.random() * 2000 >>0);
240
241 function arrayBufferToString(arrayBuffer) {
242 // UTF-16LE
243 return String.fromCharCode.apply(String, new Uint16Array(arrayBuffer));
244 }
245
246 function stringToArrayBuffer(string) {
247 // UTF-16LE
248 var buf = new ArrayBuffer(string.length * 2);
249 var bufView = new Uint16Array(buf);
250 for (var i = 0, strLen = string.length; i < strLen; i++) {
251 bufView[i] = string.charCodeAt(i);
252 }
253 return buf;
254 }
255
256 function testSendMessage(message, address, port) {
257 address = address || kMulticastAddress;
258 port = port || kPort;
259 var clientSocketId;
260 socket.create('udp', {}, function (socketInfo) {
261 if (socketInfo) {
262 clientSocketId = socketInfo.socketId;
263 chrome.test.assertTrue(clientSocketId > 0,
264 "Cannot create client udp socket.");
265 socket.bind(clientSocketId, "0.0.0.0", kPort + 2, function (result) {
266 chrome.test.assertTrue(clientSocketId > 0,
267 "Cannot bind client udp socket.");
268 socket.sendTo(clientSocketId,
269 stringToArrayBuffer(message),
270 address,
271 port, function (result) {
272 chrome.test.assertTrue(result.bytesWritten >= 0,
273 "Send to failed. " + JSON.stringify(result));
274 socket.destroy(clientSocketId);
275 });
276 });
277 } else {
278 chrome.test.fail("Cannot create client udp socket");
279 }
280 });
281 }
282
283 function recvBeforeAddMembership(serverSocketId) {
284 var recvTimeout;
285 var ok = false;
286 socket.recvFrom(serverSocketId, 1024, function (result) {
287 if (ok){
288 recvWithMembership(serverSocketId);
289 } else {
290 console.log(JSON.stringify(arrayBufferToString(result.data)));
291 chrome.test.fail("Recieved message before joining the group");
292 }
293 });
294 testSendMessage(request);
295 recvTimeout = setTimeout(function () {
296 ok = true;
297 testSendMessage(request, "127.0.0.1", kPort);
298 }, 2000);
299 }
300
301 function recvWithMembership(serverSocketId) {
302 socket.joinGroup(serverSocketId, kMulticastAddress, function (result) {
303 chrome.test.assertEq(0, result, "Join group failed.");
304 var recvTimeout;
305 socket.recvFrom(serverSocketId, 1024, function (result) {
306 chrome.test.assertTrue(!!result && !!result.data, "Recv failed.");
307 chrome.test.assertEq(request, arrayBufferToString(result.data),
308 "Wrong message!");
309 clearTimeout(recvTimeout);
310 recvWithoutMembership(serverSocketId);
311 });
312 testSendMessage(request);
313 recvTimeout = setTimeout(function () {
314 socket.destroy(serverSocketId);
315 chrome.test.fail("Cannot receive from multicast group.");
316 }, 2000);
317 });
318 }
319
320 function recvWithoutMembership(serverSocketId) {
321 socket.leaveGroup(serverSocketId, kMulticastAddress, function (result) {
322 chrome.test.assertEq(0, result, "leave group failed.");
323 var recvTimeout;
324 socket.recvFrom(serverSocketId, 1024, function (result) {
325 chrome.test.fail("Recieved message after leaving the group");
326 clearTimeout(recvTimeout);
327 });
328 testSendMessage(request);
329 recvTimeout = setTimeout(function () {
330 socket.destroy(serverSocketId);
331 succeeded = true;
332 chrome.test.succeed();
333 }, 2000);
334 });
335 }
336
337 socket.create('udp', {}, function (socketInfo) {
338 var serverSocketId;
339 if (socketInfo) {
340 serverSocketId = socketInfo.socketId;
341 socket.bind(serverSocketId, "0.0.0.0", kPort, function (result) {
342 chrome.test.assertEq(0, result, "Bind failed.");
343 recvBeforeAddMembership(serverSocketId);
344 });
345 } else {
346 chrome.test.fail("Cannot create server udp socket");
347 }
348 });
349 }
350
237 var onMessageReply = function(message) { 351 var onMessageReply = function(message) {
238 var parts = message.split(":"); 352 var parts = message.split(":");
239 test_type = parts[0]; 353 test_type = parts[0];
240 address = parts[1]; 354 address = parts[1];
241 port = parseInt(parts[2]); 355 port = parseInt(parts[2]);
242 console.log("Running tests, protocol " + protocol + ", echo server " + 356 console.log("Running tests, protocol " + protocol + ", echo server " +
243 address + ":" + port); 357 address + ":" + port);
244 if (test_type == 'tcp_server') { 358 if (test_type == 'tcp_server') {
245 chrome.test.runTests([ testSocketListening ]); 359 chrome.test.runTests([ testSocketListening ]);
360 } else if (test_type == 'multicast') {
361 chrome.test.runTests([ testMulticast ]);
246 } else { 362 } else {
247 protocol = test_type; 363 protocol = test_type;
248 chrome.test.runTests([ testSocketCreation, testSending ]); 364 chrome.test.runTests([ testSocketCreation, testSending ]);
249 } 365 }
250 }; 366 };
251 367
252 // Find out which protocol we're supposed to test, and which echo server we 368 // Find out which protocol we're supposed to test, and which echo server we
253 // should be using, then kick off the tests. 369 // should be using, then kick off the tests.
254 chrome.test.sendMessage("info_please", onMessageReply); 370 chrome.test.sendMessage("info_please", onMessageReply);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698