Permalink
Browse files

also test assume metadata

  • Loading branch information...
substack committed Mar 6, 2012
1 parent a95c862 commit bfcb1186b63b8d3d502f1fd3bbc75f7c6dcdf6e0
Showing with 28 additions and 16 deletions.
  1. +23 −12 index.js
  2. +5 −4 test/meta.js
View
@@ -153,8 +153,23 @@ exports.createServer = function (opts) {
};
self.assume = function (roleVer, port, cb) {
+ var params = {};
+ if (typeof port === 'object') {
+ params = port;
+ port = params.port;
+ }
+ else if (typeof roleVer === 'object') {
+ params = roleVer;
+ roleVer = params.role;
+
+ if (typeof port === 'function') {
+ cb = port;
+ port = params.port;
+ }
+ }
+
var role = roleVer.split('@')[0];
- var version = roleVer.split('@')[1];
+ var version = params.version || roleVer.split('@')[1];
var ix = ports[addr].indexOf(port);
if (ix >= 0) ports[addr].splice(ix, 1);
@@ -164,18 +179,14 @@ exports.createServer = function (opts) {
roles[role] = (roles[role] || []).filter(function (r) {
return r.port !== port;
});
- roles[role].push({
- host : addr,
- port : port,
- version : version,
- });
- server.emit('assume', {
- role : role,
- host : addr,
- port : port,
- version : version,
- });
+ params.host = params.host || addr;
+ params.port = port;
+ params.role = role;
+ params.version = version;
+ roles[role].push(params);
+
+ server.emit('assume', params);
if (cb) cb();
};
View
@@ -2,7 +2,7 @@ var test = require('tap').test;
var seaport = require('../');
test('allocate with metadata', function (t) {
- t.plan(8);
+ t.plan(12);
var port = Math.floor(Math.random() * 5e5 + 1e5);
var server = seaport.createServer();
@@ -23,13 +23,14 @@ test('allocate with metadata', function (t) {
server.on('free', function () {
ports = seaport.connect('localhost', port);
- ports.assume('http', gotPort);
+ ports.assume('http', { port : gotPort, foo : 'bar' });
});
server.on('assume', function (alloc) {
t.equal(alloc.port, gotPort);
- t.equal(alloc.beep, 'boop');
- t.equal(alloc.host, '127.1.2.3');
+ t.equal(alloc.foo, 'bar');
+ t.ok(alloc.beep === undefined);
+ t.equal(alloc.host, '127.0.0.1');
ports.close();
server.close();

0 comments on commit bfcb118

Please sign in to comment.