You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.6 KiB
98 lines
2.6 KiB
'use strict';
|
|
|
|
if (typeof(window) === 'undefined'){
|
|
|
|
// Node.js Tests
|
|
|
|
var chai = require('chai');
|
|
|
|
/* jshint unused: false */
|
|
var should = chai.should();
|
|
var expect = chai.expect;
|
|
|
|
var dns = require('dns');
|
|
var sinon = require('sinon');
|
|
|
|
var bitcore = require('../..');
|
|
var Peer = bitcore.transport.Peer;
|
|
var MessagesData = require('../data/messages');
|
|
var Messages = bitcore.transport.Messages;
|
|
var Pool = bitcore.transport.Pool;
|
|
var Networks = bitcore.Networks;
|
|
|
|
describe('Pool', function() {
|
|
|
|
it('should be able to create instance', function() {
|
|
var pool = new Pool();
|
|
pool.network.should.equal(Networks.livenet);
|
|
});
|
|
|
|
it('should be able to create instance setting the network', function() {
|
|
var pool = new Peer(Networks.testnet);
|
|
pool.network.should.equal(Networks.livenet);
|
|
});
|
|
|
|
it('should discover peers via dns', function() {
|
|
var stub = sinon.stub(dns, 'resolve', function(seed, callback){
|
|
callback(null, ['10.10.10.1', '10.10.10.2', '10.10.10.3']);
|
|
});
|
|
var pool = new Pool(Networks.livenet);
|
|
pool.connect();
|
|
pool.disconnect();
|
|
pool._addrs.length.should.equal(3);
|
|
stub.restore();
|
|
});
|
|
|
|
it('should not discover peers via dns', function() {
|
|
var pool = new Pool();
|
|
pool._addAddr({ip: {v4: '10.10.10.1'}});
|
|
pool.connect();
|
|
pool.disconnect();
|
|
pool._addrs.length.should.equal(1);
|
|
});
|
|
|
|
it('should add new addrs as they are announced over the network', function(done) {
|
|
|
|
// only emit an event, no need to connect
|
|
var peerConnectStub = sinon.stub(Peer.prototype, 'connect', function(){
|
|
this._readMessage();
|
|
this.emit('ready');
|
|
});
|
|
|
|
// mock a addr peer event
|
|
var peerMessageStub = sinon.stub(Peer.prototype, '_readMessage', function(){
|
|
var payload = new Buffer(MessagesData.ADDR.payload, 'hex');
|
|
var message = new Messages.Addresses().fromBuffer(payload);
|
|
this.emit(message.command, message);
|
|
});
|
|
|
|
var pool = new Pool();
|
|
|
|
pool._addAddr({ip: {v4: 'localhost'}});
|
|
|
|
// listen for the event
|
|
pool.on('peeraddr', function(peer, message) {
|
|
pool._addrs.length.should.equal(502);
|
|
|
|
// restore stubs
|
|
peerConnectStub.restore();
|
|
peerMessageStub.restore();
|
|
|
|
for (var i = 0; i < pool._addrs.length; i++) {
|
|
should.exist(pool._addrs[i].hash);
|
|
should.exist(pool._addrs[i].ip);
|
|
should.exist(pool._addrs[i].ip.v4);
|
|
}
|
|
|
|
// done
|
|
done();
|
|
});
|
|
|
|
pool.connect();
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|