Skip to content
Browse files

[fix api test] Update haibu-carapace and associated tests for hook.io…

…@0.6.x.
  • Loading branch information...
1 parent f48c26d commit fdd00517689436d890f9574c171a63e660e5e697 @indexzero indexzero committed Aug 2, 2011
Showing with 63 additions and 53 deletions.
  1. +16 −6 lib/carapace.js
  2. +22 −23 lib/net.js
  3. +17 −16 test/helper/macros.js
  4. +3 −3 test/simple/use-run-test.js
  5. +5 −5 test/spawn/local-run-test.js
View
22 lib/carapace.js
@@ -72,10 +72,13 @@ carapace.listen = function listen (options, callback) {
conn.on('close', function () {
evref.ref();
- carapace.emit('carapace::connection::close', carapace.id);
+ carapace.emit('carapace::connection::close', { id: carapace.id });
});
- carapace.emit('carapace::connection::open', carapace.id, conn);
+ carapace.emit('carapace::connection::open', {
+ id: carapace.id,
+ connection: conn
+ });
});
_listen.call(this, options, function (err) {
@@ -113,10 +116,17 @@ carapace.use = function (plugins, callback) {
// this requires absolute path or node_module
//
require(plugin)(carapace);
- carapace.emit('carapace::plugin::loaded', carapace.id, plugin);
+ carapace.emit('carapace::plugin::loaded', {
+ id: carapace.id,
+ plugin: plugin
+ });
}
catch (ex) {
- carapace.emit('carapace::plugin::error', carapace.id, plugin, ex);
+ carapace.emit('carapace::plugin::error', {
+ id: carapace.id,
+ plugin: plugin,
+ error: ex
+ });
}
});
@@ -157,7 +167,7 @@ carapace.run = function (script, argv, override, callback) {
if (error) {
return callback
? callback(error)
- : carapace.emit('carapace::error', carapace.id, error);
+ : carapace.emit('carapace::error', { id: carapace.id, error: error });
}
//
@@ -190,7 +200,7 @@ carapace.run = function (script, argv, override, callback) {
carapace._module = process.mainModule;
carapace.running = true;
- carapace.emit('carapace::running', carapace.id);
+ carapace.emit('carapace::running', { id: carapace.id });
if (callback) {
callback();
View
45 lib/net.js
@@ -34,6 +34,18 @@ function nextPort (port) {
};
//
+// Helper function from node.js core for
+// working with `dummyFD`
+//
+function getDummyFD () {
+ if (!dummyFD) {
+ try { dummyFD = socket('tcp') }
+ catch (e) { dummyFD = null }
+ }
+}
+
+
+//
// Internal mapping of server instances to
// ports that have been mapped via `.listen()`.
//
@@ -53,18 +65,12 @@ net.Server.prototype._doListen = function () {
// Ensure we have a dummy fd for EMFILE conditions.
getDummyFD();
-
+
try {
- //
- // Don't allow listening on the hook-port, hook may be restarting
- //
- if(desired === carapace['hook-port']) {
- return this._doListen.call(this, nextPort(desired), arguments[1]);
- }
binding.bind(self.fd, arguments[0], arguments[1]);
}
catch (err) {
- if (err.code !== 'EADDRINUSE') {
+ if (err.code !== 'EADDRINUSE' || desired === carapace['hook-port']) {
self.close();
return self.emit('error', err);
}
@@ -95,25 +101,18 @@ net.Server.prototype._doListen = function () {
//
// Remark: How does this work with multi-server programs?
//
- carapace.servers[desired] = self;
- carapace.emit('*::carapace::port', carapace.id, desired, actual);
+ carapace.servers[desired] = self;
+ carapace.emit('carapace::port', {
+ id: carapace.id,
+ desired: desired,
+ port: actual
+ });
}
catch (err) {
self.close();
- self.emit('error', err);
- return;
+ return self.emit('error', err);
}
self._startWatcher();
});
-};
-
-function getDummyFD () {
- if (!dummyFD) {
- try {
- dummyFD = socket('tcp');
- } catch (e) {
- dummyFD = null;
- }
- }
-}
+};
View
33 test/helper/macros.js
@@ -71,13 +71,13 @@ macros.assertUse = function (plugins, vows) {
macros.assertRun = function (script, argv, vows) {
var context = {
topic: function () {
- carapace.on('carapace::running', this.callback.bind(this, null));
+ carapace.on('carapace::running', this.callback.bind(carapace, null));
carapace.run(script, argv || []);
},
- "should fire the `carapace::running` event": function (_, name) {
- assert.equal(name, 'carapace::running');
+ "should fire the `carapace::running` event": function () {
+ assert.equal(carapace.event, 'carapace::running');
},
- "should rewrite process.argv transparently": function (_, name) {
+ "should rewrite process.argv transparently": function () {
assert.equal(process.argv[1], script);
}
};
@@ -114,22 +114,23 @@ macros.assertParentSpawn = function (PORT, script, argv, cwd, vows) {
var that = this,
child = spawn(carapaceBin, ['--hook-port', PORT].concat(argv));
- carapace.on('*::port', function onPort (source, ev, port) {
- if (port && port !== carapace['hook-port']) {
- that.port = port;
- that.callback(null, source, ev, port, child);
- carapace.removeListener('*::port', onPort);
+ carapace.on('*::carapace::port', function onPort (info) {
+ if (info.port && info.port !== carapace['hook-port']) {
+ that.port = info.port;
+ info.event = this.event
+ that.callback(null, info, child);
+ carapace.un('*::carapace::port', onPort);
}
});
},
- "should emit the `*::port` event": {
- topic: function (source, ev, port, child) {
- this.callback(null, source, ev, port, child);
+ "should emit the `*::carapace::port` event": {
+ topic: function (info, child) {
+ this.callback(null, info, child);
},
- "with the correct port": function (err, source, event, port, child) {
- assert.equal(event, '*::port');
- assert.equal(port, this.port);
- assert.notEqual(port, carapace['hook-port'])
+ "with the correct port": function (_, info, child) {
+ assert.isTrue(!!~info.event.indexOf('carapace::port'));
+ assert.equal(info.port, this.port);
+ assert.notEqual(info.port, carapace['hook-port'])
},
"should correctly start the HTTP server": {
topic: function (_,_,_,child) {
View
6 test/simple/use-run-test.js
@@ -18,12 +18,12 @@ vows.describe('carapace/simple/use-plugins').addBatch({
"load up chdir, chroot, heartbeat plugins" : helper.assertUse(['chdir', 'chroot', 'heartbeat'], {
"and running the heartbeat plugin" : {
topic : function () {
- carapace.on('carapace::heartbeat', this.callback.bind(null,null));
+ carapace.on('carapace::heartbeat', this.callback.bind(carapace, null));
carapace.heartbeat();
},
"should see a carapace::heartbeat event" : function (_, event, data) {
- assert.isString(event);
- assert.equal(event, 'carapace::heartbeat');
+ assert.isString(carapace.event);
+ assert.equal(carapace.event, 'carapace::heartbeat');
}
}
})
View
10 test/spawn/local-run-test.js
@@ -25,13 +25,13 @@ vows.describe('carapace/spawn/local').addBatch({
"should set the correct exports on carapace._module": function (_, _) {
assert.equal(carapace._module.exports.port, 1337);
},
- "should emit the `*::port` event": {
+ "should emit the `carapace::port` event": {
topic: function () {
- carapace.on('*::port', this.callback.bind(this, null));
+ carapace.on('carapace::port', this.callback.bind(carapace, null));
},
- "with the correct port": function (err, event, port) {
- assert.equal(event, '*::port');
- assert.equal(port, 1337);
+ "with the correct port": function (err, info) {
+ assert.equal(carapace.event, 'carapace::port');
+ assert.equal(info.port, 1337);
},
"should correctly start the HTTP server": {
topic: function () {

0 comments on commit fdd0051

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