From 55c96cf8a8a19ed648cbddadcf7b54623fba65b9 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 28 May 2017 21:00:44 -0700 Subject: [PATCH] test: refactor test-net-server-bind * Use common.mustNotCall() and common.mustCall() as appropriate * Use block scoping * Move assertions out of `exit` handler and into callbacks * Order assert.strictEqual() args per docs * Remove console.log() calls * Move test from `parallel` to `sequential` so `common.PORT` can be used without conflicting with OS-provided ports in other `parallel` tests PR-URL: https://github.com/nodejs/node/pull/13273 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- test/parallel/test-net-server-bind.js | 86 ------------------------- test/sequential/test-net-server-bind.js | 64 ++++++++++++++++++ 2 files changed, 64 insertions(+), 86 deletions(-) delete mode 100644 test/parallel/test-net-server-bind.js create mode 100644 test/sequential/test-net-server-bind.js diff --git a/test/parallel/test-net-server-bind.js b/test/parallel/test-net-server-bind.js deleted file mode 100644 index 2804271f6f19d4..00000000000000 --- a/test/parallel/test-net-server-bind.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const net = require('net'); - - -// With only a callback, server should get a port assigned by the OS - -let address0; -const server0 = net.createServer(function(socket) { }); - -server0.listen(function() { - address0 = server0.address(); - console.log('address0 %j', address0); - server0.close(); -}); - - -// No callback to listen(), assume we can bind in 100 ms - -let address1; -let connectionKey1; -const server1 = net.createServer(function(socket) { }); - -server1.listen(common.PORT); - -setTimeout(function() { - address1 = server1.address(); - connectionKey1 = server1._connectionKey; - console.log('address1 %j', address1); - server1.close(); -}, 100); - - -// Callback to listen() - -let address2; -const server2 = net.createServer(function(socket) { }); - -server2.listen(common.PORT + 1, function() { - address2 = server2.address(); - console.log('address2 %j', address2); - server2.close(); -}); - - -// Backlog argument - -let address3; -const server3 = net.createServer(function(socket) { }); - -server3.listen(common.PORT + 2, '0.0.0.0', 127, function() { - address3 = server3.address(); - console.log('address3 %j', address3); - server3.close(); -}); - - -// Backlog argument without host argument - -let address4; -const server4 = net.createServer(function(socket) { }); - -server4.listen(common.PORT + 3, 127, function() { - address4 = server4.address(); - console.log('address4 %j', address4); - server4.close(); -}); - - -process.on('exit', function() { - assert.ok(address0.port > 100); - assert.strictEqual(common.PORT, address1.port); - - let expectedConnectionKey1; - - if (address1.family === 'IPv6') - expectedConnectionKey1 = `6::::${address1.port}`; - else - expectedConnectionKey1 = `4:0.0.0.0:${address1.port}`; - - assert.strictEqual(connectionKey1, expectedConnectionKey1); - assert.strictEqual(common.PORT + 1, address2.port); - assert.strictEqual(common.PORT + 2, address3.port); - assert.strictEqual(common.PORT + 3, address4.port); -}); diff --git a/test/sequential/test-net-server-bind.js b/test/sequential/test-net-server-bind.js new file mode 100644 index 00000000000000..56216d0ed60619 --- /dev/null +++ b/test/sequential/test-net-server-bind.js @@ -0,0 +1,64 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + + +// With only a callback, server should get a port assigned by the OS +{ + const server = net.createServer(common.mustNotCall()); + + server.listen(common.mustCall(function() { + assert.ok(server.address().port > 100); + server.close(); + })); +} + +// No callback to listen(), assume we can bind in 100 ms +{ + const server = net.createServer(common.mustNotCall()); + + server.listen(common.PORT); + + setTimeout(function() { + const address = server.address(); + assert.strictEqual(address.port, common.PORT); + + if (address.family === 'IPv6') + assert.strictEqual(server._connectionKey, `6::::${address.port}`); + else + assert.strictEqual(server._connectionKey, `4:0.0.0.0:${address.port}`); + + server.close(); + }, 100); +} + +// Callback to listen() +{ + const server = net.createServer(common.mustNotCall()); + + server.listen(common.PORT + 1, common.mustCall(function() { + assert.strictEqual(server.address().port, common.PORT + 1); + server.close(); + })); +} + +// Backlog argument +{ + const server = net.createServer(common.mustNotCall()); + + server.listen(common.PORT + 2, '0.0.0.0', 127, common.mustCall(function() { + assert.strictEqual(server.address().port, common.PORT + 2); + server.close(); + })); +} + +// Backlog argument without host argument +{ + const server = net.createServer(common.mustNotCall()); + + server.listen(common.PORT + 3, 127, common.mustCall(function() { + assert.strictEqual(server.address().port, common.PORT + 3); + server.close(); + })); +}