Skip to content
Browse files

Handle "Not Running" Error when trying to close a server more than on…

…ce. Simply emit 'close' instead (and forward any other errors as error events).
  • Loading branch information...
1 parent 1f24314 commit 419107b3902f656395a74a6e5b7fc3889720095c @timoxley timoxley committed Jan 21, 2012
Showing with 27 additions and 2 deletions.
  1. +12 −2 index.js
  2. +15 −0 test/error.js
View
14 index.js
@@ -270,9 +270,19 @@ dnode.prototype.end = function () {
};
dnode.prototype.close = function () {
- this.server.close();
+ var self = this
+ try {
+ self.server.close();
+ } catch (e) {
+ if (e.message === 'Not running') {
+ self.server.emit('close');
+ } else {
+ // forward as error event
+ self.server.emit('error', e);
+ }
+ }
};
-
+
dnode.connect = function () {
var d = dnode();
return d.connect.apply(d, arguments);
View
15 test/error.js
@@ -85,3 +85,18 @@ test('refused', function (t) {
t.end();
});
});
+
+test('close same server twice shouldn\'t throw errors', function(t) {
+ var port = Math.floor(Math.random() * 40000 + 10000);
+ var server = dnode();
+ server.on('ready', function() {
+ server.once('close', function() {
+ server.once('close', function() {
+ t.end();
+ })
+ server.close();
+ })
+ server.close();
+ });
+ server.listen(port);
+})

0 comments on commit 419107b

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