From e7e34ddb66f61d123f00a173e6a7b196d567c9d0 Mon Sep 17 00:00:00 2001 From: Shigeki Ohtsu Date: Mon, 18 Jun 2012 14:16:16 +0900 Subject: [PATCH] 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. --- .../test-dgram-broadcast-multi-process.js | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/simple/test-dgram-broadcast-multi-process.js b/test/simple/test-dgram-broadcast-multi-process.js index 7b3aa4d81e5..adbdc41098f 100644 --- a/test/simple/test-dgram-broadcast-multi-process.js +++ b/test/simple/test-dgram-broadcast-multi-process.js @@ -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()),