Permalink
Browse files

Merge branch 'fix'

  • Loading branch information...
2 parents 01de075 + 94d7528 commit c3b096672c9fbd89c1048946d099fe43edf6929e @substack committed Oct 1, 2012
Showing with 125 additions and 12 deletions.
  1. +3 −2 index.js
  2. +2 −2 package.json
  3. +13 −8 test/hub_down_server_down.js
  4. +93 −0 test/recon.js
  5. +14 −0 test/recon/server.js
View
5 index.js
@@ -44,8 +44,7 @@ Airport.prototype.connect = function (opts, fn) {
ports.get(role, onget);
function onget (ps) {
- ports.removeListener('down', ondown);
-
+ //ports.removeListener('down', ondown);
var s = pick(ps);
if (res) res.destroy();
@@ -61,7 +60,9 @@ Airport.prototype.connect = function (opts, fn) {
queue.forEach(function (cb) { res(cb) });
queue = [];
}
+ if (this._ondown) ports.removeListener('down', this._ondown);
ports.on('down', ondown);
+ this._ondown = ondown;
function connector (service, cb) {
var inst = upnode(cons);
View
4 package.json
@@ -1,7 +1,7 @@
{
"name" : "airport",
"description" : "role-based port management for upnode",
- "version" : "0.4.2",
+ "version" : "0.4.3",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/airport.git"
@@ -27,7 +27,7 @@
"seaport" : "~0.8.0"
},
"devDependencies" : {
- "tap" : "~0.2.6"
+ "tap" : "~0.3.0"
},
"engines" : {
"node" : ">=0.6.0"
View
21 test/hub_down_server_down.js
@@ -3,7 +3,7 @@ var seaport = require('seaport');
var spawn = require('child_process').spawn;
test('hub goes down, server goes down', function (t) {
- t.plan(1);
+ t.plan(2);
var port = Math.floor(Math.random() * 5e4 + 1e4);
@@ -22,29 +22,34 @@ test('hub goes down, server goes down', function (t) {
var data = '';
ps.client.stdout.on('data', function (buf) { data += buf });
+ps.client.stderr.pipe(process.stderr, { end : false });
function checkOutput () {
- t.same(data.split(/\r?\n/), [ 'up', 'down', 'up', '' ]);
+console.dir(data);
+ t.same(data.split(/\r?\n/).slice(-3)[0], 'down');
+ t.same(data.split(/\r?\n/).slice(-2)[0], 'up');
}
setTimeout(function () {
+ ps.hub.kill();
ps.server.kill();
- }, 2 * 1000);
+ }, 1500);
setTimeout(function () {
- ps.hub.kill();
- }, 4 * 1000);
+ ps.hub = sh('hub.js');
+ }, 2.5 * 1000);
setTimeout(function () {
+ ps.hub.kill();
ps.hub = sh('hub.js');
- }, 6 * 1000);
+ }, 4 * 1000);
setTimeout(function () {
ps.server = sh('server.js');
- }, 10 * 1000);
+ }, 4.5 * 1000);
setTimeout(function () {
checkOutput();
- }, 15 * 1000);
+ }, 8 * 1000);
t.on('end', function () {
ps.hub.kill();
View
93 test/recon.js
@@ -0,0 +1,93 @@
+var test = require('tap').test;
+var spawn = require('child_process').spawn;
+var airport = require('../');
+var seaport = require('seaport');
+var port = Math.floor(Math.random() * 5e4 + 1e4);
+
+function createHub () {
+ return spawn(process.execPath, [
+ __dirname + '/../node_modules/.bin/seaport',
+ port
+ ]);
+}
+
+function createServer () {
+ return spawn(process.execPath, [
+ __dirname + '/recon/server.js',
+ port
+ ]);
+}
+
+function runProc (fn) {
+ var ps = fn();
+
+ var ref = {};
+ ref.restart = function (n) {
+ ref.stopped = true;
+ ps.kill('SIGKILL');
+ setTimeout(function () {
+ var ref_ = runProc(fn);
+ ref.restart = ref_.restart;
+ }, n);
+ };
+ return ref;
+}
+
+test('reconnection race', function (t) {
+ t.plan(2);
+
+ var air = airport('localhost', port);
+ var up = air.connect('q');
+ var results = [];
+
+ var iv = setInterval(function () {
+ up(function (remote) {
+ remote.beep(function (s) {
+ console.log(' ' + s);
+ results.push(s);
+ });
+ });
+ }, 100);
+
+ setTimeout(function () {
+ t.ok(results.length > 10 * 2, 'enough initial events');
+ clearInterval(iv);
+ console.log('--- mark ---');
+
+ up(function (remote) {
+ remote.beep(function (s) {
+ t.ok(s);
+ results.push(s);
+ });
+ });
+ }, 10 * 1000);
+
+ var server = runProc(createServer, 1200);
+ var hub = runProc(createHub, 400);
+
+ up(function (remote) {
+ setTimeout(function () {
+ hub.restart(1500)
+ }, 10);
+
+ setTimeout(function () {
+ hub.restart(200);
+ }, 1900);
+
+ setTimeout(function () {
+ server.restart(2500);
+ }, 100);
+
+ setTimeout(function () {
+ server.restart(300);
+ }, 2700);
+
+ setTimeout(function () {
+ server.restart(600);
+ }, 2300);
+ });
+
+ t.on('end', function () {
+ setTimeout(process.exit, 1000);
+ });
+});
View
14 test/recon/server.js
@@ -0,0 +1,14 @@
+var airport = require('../../');
+var air = airport('localhost', Number(process.argv[2]));
+var ix = 0;
+
+return air(function () {
+ this.beep = function (cb) {
+ if (typeof cb === 'function') {
+ var xs = 'boop'.split('');
+ xs[ix] = xs[ix].toUpperCase();
+ ix = (ix + 1) % xs.length;
+ cb(xs.join(''));
+ }
+ };
+}).listen('q');

0 comments on commit c3b0966

Please sign in to comment.