Permalink
Browse files

Client forwards errors and shuts down even if not connected

  • Loading branch information...
1 parent e4e26d7 commit 89322e368231cda239e58b73cd400ba1197b8d76 @timoxley committed Feb 9, 2012
Showing with 73 additions and 12 deletions.
  1. +12 −7 lib/client.js
  2. +61 −5 tests/client.js
View
@@ -19,7 +19,7 @@ Client.prototype.connect = function(host, callback) {
this.host = host
this.ready = false
var self = this
-
+ try {
this.up = upnode(function(remote, connection) {
var wrappedAPI = {}
_.each(self._api, function(value, name) {
@@ -48,9 +48,18 @@ Client.prototype.connect = function(host, callback) {
_(this).extend(wrappedAPI)
}).connect(host, function(remote, connection) {
+
self.connection = connection
connection.emit('up', remote);
})
+
+ } catch (err) {
+ self.emit('error', err)
+ }
+ this.up.on('error', function(err) {
+ self.emit('error', err)
+ })
+
this.up.once('remote', function() {
self.up.once('up', function(remote) {
self.ready = true
@@ -72,17 +81,12 @@ Client.prototype.serializeError = function(error) {
Client.prototype.shutdown = function(callback) {
if (typeof callback !== 'function') callback = function(){}
var self = this
- if (!this.ready) {
- this.once('ready', function() {
- self.shutdown(callback)
- })
- return
- }
if (!this.up.closed && self.connection != null) {
self.up.close()
if (!self.up.conn) {
self.emit('shutdown')
+
return callback()
} else {
self.connection.once('end', function() {
@@ -92,6 +96,7 @@ Client.prototype.shutdown = function(callback) {
}
} else {
+ self.up.close()
self.emit('shutdown')
callback()
}
View
@@ -43,6 +43,15 @@ describe('client', function() {
})
describe('shutdown', function() {
+ it('won\'t reconnect after shutdown', function(done) {
+ client = new Client().connect(PORT)
+ client.shutdown(function() {
+ done()
+ })
+ client.on('ready', function(api, connection) {
+ throw new Error('Should not connect after shutdown')
+ })
+ })
it('will close connection on shutdown', function(done) {
client = new Client().connect(PORT)
client.on('ready', function(api, connection) {
@@ -52,13 +61,46 @@ describe('client', function() {
})
})
})
- it('won\'t error if shutdown twice', function(done) {
+ it('will cancel connecting if shutdown', function(done) {
+ server.on('connect', function() {
+ throw new Error('Should not connect')
+ })
+ client = new Client().connect(PORT, function() {
+ throw new Error('Should not fire callback')
+ })
+ client.on('ready', function() {
+ throw new Error('Should not ready')
+ })
+ client.shutdown(function() {
+ done()
+ })
+ })
+ it('will successfully shutdown multiple times after startup', function(done) {
+ server.on('connect', function() {
+ throw new Error('Should not connect')
+ })
+ client = new Client().connect(PORT)
+ client.shutdown(function() {
+ client.shutdown(function() {
+ done()
+ })
+ })
+ })
+ it('won\'t error if shutdown multiple times', function(done) {
client = new Client().connect(PORT)
client.on('ready', function(api, connection) {
- client.shutdown(function() {
- client.shutdown(function() {
- assert.ok(client.up.closed)
- done()
+ client.shutdown(function(err) {
+ assert.ok(!err)
+ client.shutdown(function(err) {
+ assert.ok(!err)
+ client.shutdown(function(err) {
+ assert.ok(!err)
+ client.shutdown(function(err) {
+ assert.ok(!err)
+ assert.ok(client.up.closed)
+ done()
+ })
+ })
})
})
})
@@ -80,6 +122,20 @@ describe('client', function() {
done()
})
})
+ it('will emit informative error if can\'t connect', function(done) {
+ try {
+ client = new Client().connect('garbage', function() {
+ throw new Error('Shouldn\'t be ready.')
+ })
+ } catch(err) {
+ console.log(err.message)
+ assert.ok(/Could\ not\ create/.test(err.message))
+ done()
+ }
+ client.on('ready', function(api, connection) {
+ throw new Error('Shouldn\'t be ready.')
+ })
+ })
})
})

0 comments on commit 89322e3

Please sign in to comment.