polish tests
This commit is contained in:
parent
e15910a113
commit
89cee5804f
@ -1,18 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Mocha</title>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="mocha"></div>
|
|
||||||
<script src="../node_modules/mocha/mocha.js"></script>
|
|
||||||
<script>mocha.setup('bdd')</script>
|
|
||||||
<script src="../tests.js"></script>
|
|
||||||
<script>
|
|
||||||
mocha.run();
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
172
test/pool.js
172
test/pool.js
@ -1,105 +1,107 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
if (typeof(window) === 'undefined'){
|
var chai = require('chai');
|
||||||
|
|
||||||
// Node.js Tests
|
/* jshint unused: false */
|
||||||
|
var should = chai.should();
|
||||||
|
var expect = chai.expect;
|
||||||
|
|
||||||
var chai = require('chai');
|
var bitcore = require('bitcore');
|
||||||
|
var P2P = require('../');
|
||||||
|
var Peer = P2P.Peer;
|
||||||
|
var MessagesData = require('./data/messages');
|
||||||
|
var Messages = P2P.Messages;
|
||||||
|
var Pool = P2P.Pool;
|
||||||
|
var Networks = bitcore.Networks;
|
||||||
|
|
||||||
/* jshint unused: false */
|
var dns = require('dns');
|
||||||
var should = chai.should();
|
var sinon = require('sinon');
|
||||||
var expect = chai.expect;
|
|
||||||
|
|
||||||
var bitcore = require('bitcore');
|
describe('Pool', function() {
|
||||||
var P2P = require('../');
|
|
||||||
var Peer = P2P.Peer;
|
|
||||||
var MessagesData = require('./data/messages');
|
|
||||||
var Messages = P2P.Messages;
|
|
||||||
var Pool = P2P.Pool;
|
|
||||||
var Networks = bitcore.Networks;
|
|
||||||
|
|
||||||
var dns = require('dns');
|
it('should be able to create instance', function() {
|
||||||
var sinon = require('sinon');
|
var pool = new Pool();
|
||||||
|
should.exist(pool.network);
|
||||||
|
expect(pool.network).to.satisfy(function(network) {
|
||||||
|
if (network === Networks.testnet || network === Networks.livenet) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Pool', function() {
|
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 be able to create instance', function() {
|
it('should discover peers via dns', function() {
|
||||||
var pool = new Pool();
|
var stub = sinon.stub(dns, 'resolve', function(seed, callback) {
|
||||||
should.exist(pool.network);
|
callback(null, ['10.10.10.1', '10.10.10.2', '10.10.10.3']);
|
||||||
expect(pool.network).to.satisfy(function(network){
|
});
|
||||||
if (network === Networks.testnet || network === Networks.livenet) {
|
var pool = new Pool(Networks.livenet);
|
||||||
return true;
|
pool.connect();
|
||||||
}
|
pool.disconnect();
|
||||||
return false;
|
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');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to create instance setting the network', function() {
|
// mock a addr peer event
|
||||||
var pool = new Peer(Networks.testnet);
|
var peerMessageStub = sinon.stub(Peer.prototype, '_readMessage', function() {
|
||||||
pool.network.should.equal(Networks.livenet);
|
var payload = new Buffer(MessagesData.ADDR.payload, 'hex');
|
||||||
|
var message = new Messages.Addresses().fromBuffer(payload);
|
||||||
|
this.emit(message.command, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should discover peers via dns', function() {
|
var pool = new Pool();
|
||||||
var stub = sinon.stub(dns, 'resolve', function(seed, callback){
|
|
||||||
callback(null, ['10.10.10.1', '10.10.10.2', '10.10.10.3']);
|
pool._addAddr({
|
||||||
});
|
ip: {
|
||||||
var pool = new Pool(Networks.livenet);
|
v4: 'localhost'
|
||||||
pool.connect();
|
}
|
||||||
pool.disconnect();
|
|
||||||
pool._addrs.length.should.equal(3);
|
|
||||||
stub.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not discover peers via dns', function() {
|
// listen for the event
|
||||||
var pool = new Pool();
|
pool.on('peeraddr', function(peer, message) {
|
||||||
pool._addAddr({ip: {v4: '10.10.10.1'}});
|
pool._addrs.length.should.equal(502);
|
||||||
pool.connect();
|
|
||||||
pool.disconnect();
|
// restore stubs
|
||||||
pool._addrs.length.should.equal(1);
|
peerConnectStub.restore();
|
||||||
});
|
peerMessageStub.restore();
|
||||||
|
|
||||||
it('should add new addrs as they are announced over the network', function(done) {
|
for (var i = 0; i < pool._addrs.length; i++) {
|
||||||
|
should.exist(pool._addrs[i].hash);
|
||||||
// only emit an event, no need to connect
|
should.exist(pool._addrs[i].ip);
|
||||||
var peerConnectStub = sinon.stub(Peer.prototype, 'connect', function(){
|
should.exist(pool._addrs[i].ip.v4);
|
||||||
this._readMessage();
|
}
|
||||||
this.emit('ready');
|
|
||||||
});
|
// done
|
||||||
|
done();
|
||||||
// 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();
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pool.connect();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
|
||||||
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user