Merge pull request #57 from braydonf/bug/double-disconnect

Patched double disconnect bug.
This commit is contained in:
Eric Martindale 2015-03-26 14:27:04 -04:00
commit 438638c4ab
2 changed files with 23 additions and 1 deletions

View File

@ -170,7 +170,9 @@ Peer.prototype._addSocketEventHandlers = function() {
Peer.prototype._onError = function(e) {
this.emit('error', e);
this.disconnect();
if (this.status !== Peer.STATUS.DISCONNECTED) {
this.disconnect();
}
};
/**

View File

@ -151,6 +151,26 @@ describe('Peer', function() {
peer.socket.emit('error', error);
});
it('will not disconnect twice on disconnect and error', function(done) {
var peer = new Peer({host: 'localhost'});
var socket = new EventEmitter();
socket.connect = sinon.stub();
socket.destroy = sinon.stub();
peer._getSocket = function() {
return socket;
};
peer.on('error', sinon.stub());
peer.connect();
var called = 0;
peer.on('disconnect', function() {
called++;
called.should.not.be.above(1);
done();
});
peer.disconnect();
peer.socket.emit('error', new Error('fake error'));
});
it('disconnect with max buffer length', function(done) {
var peer = new Peer({host: 'localhost'});
var socket = new EventEmitter();