Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
test: fix test-dgram-broadcast-multi-process
Browse files Browse the repository at this point in the history
The test failed when a router replies IPADDR_BROADCAST.
Fixed it by specifying only one address to bind a socket.
  • Loading branch information
Shigeki Ohtsu authored and bnoordhuis committed Jun 28, 2012
1 parent 60ff789 commit e7e34dd
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions test/simple/test-dgram-broadcast-multi-process.js
Expand Up @@ -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',
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit e7e34dd

Please sign in to comment.