From 97f39db0814ef93025394ead44036520edef8d89 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 13 Mar 2015 15:05:08 -0400 Subject: [PATCH] added tests for pool _addConnectedPeer --- test/pool.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/pool.js b/test/pool.js index 3acd135..45cfaf8 100644 --- a/test/pool.js +++ b/test/pool.js @@ -336,6 +336,33 @@ describe('Pool', function() { pool.connect(); }); + describe('#_addConnectedPeer', function() { + + it('should add a peer', function() { + /* jshint sub: true */ + var pool = new Pool({network: Networks.livenet, maxSize: 1}); + pool._addPeerEventHandlers = sinon.stub(); + pool._addConnectedPeer({ + on: sinon.stub() + }, {hash: 'hash'}); + should.exist(pool._connectedPeers['hash']); + pool._addPeerEventHandlers.calledOnce.should.equal(true); + }); + + it('should not already added peer', function() { + /* jshint sub: true */ + var pool = new Pool({network: Networks.livenet, maxSize: 1}); + pool._addPeerEventHandlers = sinon.stub(); + pool._connectedPeers['hash'] = {}; + pool._addConnectedPeer({ + on: sinon.stub() + }, {hash: 'hash'}); + should.exist(pool._connectedPeers['hash']); + pool._addPeerEventHandlers.calledOnce.should.equal(false); + }); + + }); + describe('#listen', function() { it('create a server', function(done) { @@ -376,6 +403,31 @@ describe('Pool', function() { pool.listen(); }); + it('should handle an ipv4 connection', function(done) { + var ipv4 = '127.0.0.1'; + sinon.stub(net, 'createServer', function(callback) { + callback({ + remoteAddress: ipv4 + }); + return { + listen: sinon.stub() + }; + }); + sinon.stub(net, 'isIPv6', function() { + return false; + }); + var pool = new Pool({network: Networks.livenet, maxSize: 1}); + pool._addAddr = function(addr) { + should.exist(addr.ip.v4); + addr.ip.v4.should.equal(ipv4); + net.isIPv6.restore(); + net.createServer.restore(); + done(); + }; + pool._addConnectedPeer = sinon.stub(); + pool.listen(); + }); + });