Skip to content

Commit

Permalink
test: clean up net server try ports test
Browse files Browse the repository at this point in the history
* Replace var's with const and let
* Replace boolean flags with common.mustCall()
* Using stricter comparisons
* Fixed typo in comment

PR-URL: #8458
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
tlhunter authored and Fishrock123 committed Sep 14, 2016
1 parent 5387735 commit 5baa4e0
Showing 1 changed file with 13 additions and 41 deletions.
54 changes: 13 additions & 41 deletions test/parallel/test-net-server-try-ports.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
'use strict';
// This tests binds to one port, then attempts to start a server on that
// This test binds to one port, then attempts to start a server on that
// port. It should be EADDRINUSE but be able to then bind to another port.
require('../common');
var assert = require('assert');
var net = require('net');
const common = require('../common');
const assert = require('assert');
const net = require('net');

var server1listening = false;
var server2listening = false;
var server2eaddrinuse = false;
const server1 = net.Server();

var server1 = net.Server(function(socket) {
socket.destroy();
});
const server2 = net.Server();

var server2 = net.Server(function(socket) {
socket.destroy();
});

var server2errors = 0;
server2.on('error', function(e) {
server2errors++;
console.error('server2 error');

if (e.code == 'EADDRINUSE') {
server2eaddrinuse = true;
}

server2.listen(0, function() {
console.error('server2 listening');
server2listening = true;
server2.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'EADDRINUSE');

server2.listen(0, common.mustCall(function() {
server1.close();
server2.close();
});
});
}));
}));


server1.listen(0, function() {
console.error('server1 listening');
server1listening = true;
server1.listen(0, common.mustCall(function() {
// This should make server2 emit EADDRINUSE
server2.listen(this.address().port);
});


process.on('exit', function() {
assert.equal(1, server2errors);
assert.ok(server2eaddrinuse);
assert.ok(server2listening);
assert.ok(server1listening);
});
}));

0 comments on commit 5baa4e0

Please sign in to comment.