Add 'peerconnect' event from Pool

This commit is contained in:
William Wolf 2015-02-14 17:57:13 -08:00
parent e19734593e
commit e0b58e25d6
2 changed files with 45 additions and 0 deletions

View File

@ -169,6 +169,9 @@ Pool.prototype._connectPeer = function _connectPeer(addr) {
peer.on('disconnect', function peerDisconnect() {
self.emit('peerdisconnect', peer, addr);
});
peer.on('connect', function peerConnect() {
self.emit('peerconnect', peer, addr);
});
peer.on('ready', function peerReady() {
self.emit('peerready', peer, addr);
});

View File

@ -100,4 +100,46 @@ describe('Pool', function() {
});
it('should propagate connect, ready, and disconnect peer events', function(done) {
var peerConnectStub = sinon.stub(Peer.prototype, 'connect', function() {
this.emit('connect', this, {});
this.emit('ready');
});
var peerDisconnectStub = sinon.stub(Peer.prototype, 'disconnect', function() {
this.emit('disconnect', this, {});
});
var pool = new Pool();
pool._addAddr({
ip: {
v4: 'localhost'
}
});
// Not great, but needed so pool won't catch its on event and fail the test
pool.removeAllListeners('peerdisconnect');
var poolDisconnectStub;
pool.on('peerconnect', function(peer, addr) {
pool.on('peerready', function(peer, addr) {
// disconnect when the peer is ready
poolDisconnectStub = sinon.stub(Pool.prototype, 'disconnect', function() {
peer.disconnect();
});
pool.disconnect();
});
});
pool.on('peerdisconnect', function(peer, addr) {
// Restore stubs
peerConnectStub.restore();
peerDisconnectStub.restore();
poolDisconnectStub.restore();
// done
done();
});
pool.connect();
});
});