Patched double disconnect bug.

- Added a test that simulates a peer abruptly disconnecting with an error
- Put an additional condition to prevent disconnecting on an error if already disconnected
- Closes #56
This commit is contained in:
Braydon Fuller 2015-03-26 13:51:03 -04:00
parent fa06ce01e9
commit f8578ff114
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();