test: fix test-dgram-broadcast-multi-process

The test failed when a router replies IPADDR_BROADCAST.
Fixed it by specifying only one address to bind a socket.
pull/24503/head
Shigeki Ohtsu 2012-06-18 14:16:16 +09:00 committed by Ben Noordhuis
parent 60ff789618
commit e7e34ddb66
1 changed files with 20 additions and 2 deletions

View File

@ -23,7 +23,7 @@ var common = require('../common'),
assert = require('assert'),
dgram = require('dgram'),
util = require('util'),
assert = require('assert'),
networkInterfaces = require('os').networkInterfaces(),
Buffer = require('buffer').Buffer,
fork = require('child_process').fork,
LOCAL_BROADCAST_HOST = '255.255.255.255',
@ -35,6 +35,19 @@ var common = require('../common'),
new Buffer('Fourth message to send')
];
// take the first non-internal interface as the address for binding
get_bindAddress: for (var name in networkInterfaces) {
var interfaces = networkInterfaces[name];
for(var i = 0; i < interfaces.length; i++) {
var localInterface = interfaces[i];
if (!localInterface.internal && localInterface.family === 'IPv4') {
var bindAddress = localInterface.address;
break get_bindAddress;
}
}
}
assert.ok(bindAddress);
if (process.argv[2] !== 'child') {
var workers = {},
listeners = 3,
@ -146,7 +159,9 @@ if (process.argv[2] !== 'child') {
var sendSocket = dgram.createSocket('udp4');
sendSocket.bind(common.PORT);
// bind the address explicitly for sending
// INADDR_BROADCAST to only one interface
sendSocket.bind(common.PORT, bindAddress);
sendSocket.setBroadcast(true);
sendSocket.on('close', function() {
@ -187,6 +202,9 @@ if (process.argv[2] === 'child') {
var listenSocket = dgram.createSocket('udp4');
listenSocket.on('message', function(buf, rinfo) {
// receive udp messages only sent from parent
if (rinfo.address !== bindAddress) return;
console.error('[CHILD] %s received %s from %j',
process.pid,
util.inspect(buf.toString()),