Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed ports.free() bug when passed just a number, role wasn't getting…

… unallocated
  • Loading branch information...
commit 36f7b0498f66d57842de6a2c05dcf59267306e16 1 parent 80fa7e7
Mike Brevoort authored

Showing 2 changed files with 41 additions and 1 deletion. Show diff stats Hide diff stats

  1. +3 1 index.js
  2. +38 0 test/free_port.js
4 index.js
@@ -302,7 +302,9 @@ exports.createServer = function (opts) {
302 302 Object.keys(roles).forEach(function (role) {
303 303 var rs = roles[role];
304 304 roles[role] = rs.filter(function (r) {
305   - var x = r.port === port && r.host === host && r._id === id;
  305 + // if no id passed into free, don't consider it
  306 + var idCondition = (!id || r._id === id);
  307 + var x = r.port === port && r.host === host && idCondition;
306 308 if (x) {
307 309 var ix = allocated.indexOf(r);
308 310 if (ix >= 0) allocated.splice(ix, 1);
38 test/free_port.js
... ... @@ -0,0 +1,38 @@
  1 +var test = require('tap').test;
  2 +var seaport = require('../');
  3 +
  4 +// same exact test as free.js but passing ports.free a number parameter
  5 +test('free', function (t) {
  6 + t.plan(6);
  7 + var port = Math.floor(Math.random() * 5e4 + 1e4);
  8 + var server = seaport.createServer();
  9 + server.listen(port);
  10 +
  11 + var ports = seaport.connect('localhost', port);
  12 + var gotPort, gotRec;
  13 + server.on('free', function (rec) {
  14 + t.same(rec, gotRec);
  15 + t.same(server.allocated[gotRec.host], []);
  16 + ports.close();
  17 + });
  18 +
  19 + ports.allocate('http', function (p) {
  20 + t.ok(p >= 10000 && p < 65536);
  21 + gotPort = p;
  22 +
  23 + process.nextTick(function () {
  24 + ports.query('http', function (ps) {
  25 + t.equal(ps.length, 1);
  26 + t.equal(ps[0].host, '127.0.0.1');
  27 + t.equal(ps[0].port, gotPort);
  28 +
  29 + gotRec = ps[0];
  30 + ports.free(ps[0].port);
  31 + });
  32 + });
  33 + });
  34 +
  35 + t.on('end', function () {
  36 + server.close();
  37 + });
  38 +});

0 comments on commit 36f7b04

Please sign in to comment.
Something went wrong with that request. Please try again.