ports.free() for port only free implementation #12

Merged
merged 1 commit into from Oct 27, 2012
View
@@ -302,7 +302,7 @@ exports.createServer = function (opts) {
Object.keys(roles).forEach(function (role) {
var rs = roles[role];
roles[role] = rs.filter(function (r) {
- var x = r.port === port && r.host === host && r._id === id;
+ var x = r.port === port && r.host === host && (!id || r._id === id);
if (x) {
var ix = allocated.indexOf(r);
if (ix >= 0) allocated.splice(ix, 1);
View
@@ -0,0 +1,37 @@
+var test = require('tap').test;
+var seaport = require('../');
+
+test('free', function (t) {
+ t.plan(6);
+ var port = Math.floor(Math.random() * 5e4 + 1e4);
+ var server = seaport.createServer();
+ server.listen(port);
+
+ var ports = seaport.connect('localhost', port);
+ var gotPort, gotRec;
+ server.on('free', function (rec) {
+ t.same(rec, gotRec);
+ t.same(server.allocated[gotRec.host], []);
+ ports.close();
+ });
+
+ ports.allocate('http', function (p) {
+ t.ok(p >= 10000 && p < 65536);
+ gotPort = p;
+
+ process.nextTick(function () {
+ ports.query('http', function (ps) {
+ t.equal(ps.length, 1);
+ t.equal(ps[0].host, '127.0.0.1');
+ t.equal(ps[0].port, gotPort);
+
+ gotRec = ps[0];
+ ports.free(ps[0].port);
+ });
+ });
+ });
+
+ t.on('end', function () {
+ server.close();
+ });
+});