Chromium Code Reviews| Index: extensions/test/data/sockets_udp/api/background.js |
| diff --git a/extensions/test/data/sockets_udp/api/background.js b/extensions/test/data/sockets_udp/api/background.js |
| index 8382292050250fea22aa4da9f0ae30bcf2e1fafb..6bc7f483acb928d947e4a0c9790433b0f4f9879c 100644 |
| --- a/extensions/test/data/sockets_udp/api/background.js |
| +++ b/extensions/test/data/sockets_udp/api/background.js |
| @@ -71,6 +71,66 @@ var testSocketCreation = function() { |
| chrome.sockets.udp.create({}, onCreate); |
| }; |
| +var testSocketReusable = function() { |
| + var address = '224.0.0.251'; |
| + var port = 5353; |
| + |
| + var numSocketsCreated = 0; |
| + var numSocketsExpected = 2; |
| + |
| + var socketIds = []; |
| + |
| + function onCreate(createInfo) { |
| + function onBind(result) { |
| + chrome.test.assertEq(0, result, 'Bind failed with error: ' + result); |
| + chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfoAfterBind); |
| + } |
| + |
| + function onGetInfoBeforeBind(info) { |
|
carlosk
2016/09/30 02:16:24
nit: it would be clearer to me if these 3 methods
srsudar
2016/09/30 15:07:17
Done.
|
| + if (info.localAddress || info.localPort) { |
| + chrome.test.fail('Unconnected socket should not have local binding'); |
| + } |
| + |
| + chrome.test.assertEq(true, info.addressReusable, 'Should be reusable'); |
| + |
| + chrome.sockets.udp.bind(createInfo.socketId, address, port, onBind); |
| + } |
| + |
| + function onGetInfoAfterBind(info) { |
| + numSocketsCreated += 1; |
| + chrome.test.assertEq(address, info.localAddress, |
| + 'Bound to wrong address'); |
| + chrome.test.assertEq(port, info.localPort, 'Bound to wrong port'); |
| + chrome.test.assertEq(true, info.addressReusable, 'Should be reusable'); |
| + |
| + if (numSocketsCreated < numSocketsExpected) { |
| + // Bind again to ensure the address is reusable. |
| + createAndBindToSharedAddress(); |
| + } else { |
| + chrome.test.assertEq(numSocketsCreated, numSocketsExpected, |
| + 'Not all sockets created during test'); |
|
carlosk
2016/09/30 02:16:24
Is this trying to check if too many sockets were c
srsudar
2016/09/30 15:07:17
Good point. I was trying to use this call to ensur
|
| + chrome.sockets.udp.close(socketIds[0], function() { |
| + chrome.sockets.udp.close(socketIds[1], function() { |
| + chrome.test.succeed(); |
|
carlosk
2016/09/30 02:16:24
IIUC you created numSocketsExpected to store the a
srsudar
2016/09/30 15:07:17
Done.
|
| + }); |
| + }); |
| + } |
| + } |
| + |
| + chrome.test.assertTrue(createInfo.socketId > 0); |
| + socketIds.push(createInfo.socketId); |
| + |
| + chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfoBeforeBind); |
| + } |
| + |
| + function createAndBindToSharedAddress() { |
| + chrome.sockets.udp.create({ "allowAddressReuse": true }, onCreate); |
| + } |
| + |
| + createAndBindToSharedAddress(); |
| +}; |
| + |
| + |
| /////////////////////////////////////////////////////////////////////////////// |
| // Test socket send/receive |
| // |
| @@ -332,8 +392,8 @@ var onMessageReply = function(message) { |
| chrome.test.runTests([ testMulticast ]); |
| } else { |
| console.log("Running udp tests"); |
| - chrome.test.runTests([ testSocketCreation, testSending, testSetPaused, |
| - testBroadcast ]); |
| + chrome.test.runTests([ testSocketCreation, testSocketReusable, testSending, |
| + testSetPaused, testBroadcast ]); |
| } |
| }; |