cleanup test code

This commit is contained in:
Ivan Socolsky 2015-03-31 12:29:28 -03:00
parent 6209a2ac97
commit ff8c360435
2 changed files with 111 additions and 102 deletions

View File

@ -20,20 +20,30 @@ var Notification = require('./model/notification');
function BlockchainMonitor() {
var self = this;
this.subscriptions = {};
this.sockets = {};
this.sockets['livenet'] = self._getBlockchainExplorerSocket('insight', 'livenet');
this.sockets['testnet'] = self._getBlockchainExplorerSocket('insight', 'testnet');
this.subscriber = {};
this.subscriber['livenet'] = self._getAddressSubscriber('insight', 'livenet');
this.subscriber['testnet'] = self._getAddressSubscriber('insight', 'testnet');
};
nodeutil.inherits(BlockchainMonitor, events.EventEmitter);
BlockchainMonitor.prototype._getBlockchainExplorerSocket = function(provider, network) {
BlockchainMonitor.prototype._getAddressSubscriber = function(provider, network) {
$.checkArgument(provider == 'insight', 'Blockchain monitor ' + provider + ' not supported');
var explorer = new BlockchainExplorer({
provider: provider,
network: network,
});
return explorer.initSocket();
var socket = explorer.initSocket();
// TODO: Extract on its own class once more providers are implemented
return {
subscribe: function(address, handler) {
socket.emit('subscribe', address);
socket.on(address, handler);
},
};
};
BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
@ -63,11 +73,10 @@ BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) {
var addresses = [].concat(addresses);
var network = Bitcore.Address.fromString(addresses[0]).network.name;
var socket = self.sockets[network];
var subscriber = self.subscriber[network];
_.each(addresses, function(address) {
self.subscriptions[walletId].addresses.push(address);
socket.emit('subscribe', address);
socket.on(address, _.bind(handlerFor, walletId, address));
subscriber.subscribe(address, _.bind(handlerFor, walletId, address));
});
};

View File

@ -202,100 +202,6 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var db, storage, blockchainExplorer;
describe('Blockchain monitor', function() {
var bcSocket, monitor;
beforeEach(function() {
db = levelup(memdown, {
valueEncoding: 'json'
});
storage = new Storage({
db: db
});
blockchainExplorer = sinon.stub();
WalletService.initialize({
storage: storage,
blockchainExplorer: blockchainExplorer,
});
helpers.offset = 0;
bcSocket = sinon.stub();
bcSocket.emit = sinon.stub();
bcSocket.on = sinon.stub();
sinon.stub(BlockchainMonitor.prototype, '_getBlockchainExplorerSocket').onFirstCall().returns(bcSocket);
monitor = new BlockchainMonitor();
});
afterEach(function() {
BlockchainMonitor.prototype._getBlockchainExplorerSocket.restore();
});
it('should subscribe wallet', function(done) {
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
server.createAddress({}, function(err, address2) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
bcSocket.emit.calledTwice.should.be.true;
bcSocket.emit.calledWith('subscribe', address1.address).should.be.true;
bcSocket.emit.calledWith('subscribe', address2.address).should.be.true;
done();
});
});
});
});
});
it('should be able to subscribe new address', function(done) {
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
bcSocket.emit.calledOnce.should.be.true;
bcSocket.emit.calledWith('subscribe', address1.address).should.be.true;
server.createAddress({}, function(err, address2) {
should.not.exist(err);
monitor.subscribeAddresses(wallet.id, address2.address);
bcSocket.emit.calledTwice.should.be.true;
bcSocket.emit.calledWith('subscribe', address2.address).should.be.true;
done();
});
});
});
});
});
it('should create NewIncomingTx notification when a new tx arrives on registered address', function(done) {
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
bcSocket.on.calledOnce.should.be.true;
bcSocket.on.getCall(0).args[0].should.equal(address1.address);
var handler = bcSocket.on.getCall(0).args[1];
_.isFunction(handler).should.be.true;
var emitSpy = sinon.spy(monitor, 'emit');
handler('txid');
emitSpy.calledOnce.should.be.true;
emitSpy.getCall(0).args[0].should.equal('notification');
var notification = emitSpy.getCall(0).args[1];
notification.type.should.equal('NewIncomingTx');
notification.data.address.should.equal(address1.address);
notification.data.txid.should.equal('txid');
done();
});
});
});
});
});
describe('Wallet service', function() {
beforeEach(function() {
db = levelup(memdown, {
@ -2568,3 +2474,97 @@ describe('Wallet service', function() {
});
});
});
describe('Blockchain monitor', function() {
var addressSubscriber;
beforeEach(function() {
db = levelup(memdown, {
valueEncoding: 'json'
});
storage = new Storage({
db: db
});
blockchainExplorer = sinon.stub();
WalletService.initialize({
storage: storage,
blockchainExplorer: blockchainExplorer,
});
helpers.offset = 0;
addressSubscriber = sinon.stub();
addressSubscriber.subscribe = sinon.stub();
sinon.stub(BlockchainMonitor.prototype, '_getAddressSubscriber').onFirstCall().returns(addressSubscriber);
});
afterEach(function() {
BlockchainMonitor.prototype._getAddressSubscriber.restore();
});
it('should subscribe wallet', function(done) {
var monitor = new BlockchainMonitor();
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
server.createAddress({}, function(err, address2) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
addressSubscriber.subscribe.calledTwice.should.be.true;
addressSubscriber.subscribe.calledWith(address1.address).should.be.true;
addressSubscriber.subscribe.calledWith(address2.address).should.be.true;
done();
});
});
});
});
});
it('should be able to subscribe new address', function(done) {
var monitor = new BlockchainMonitor();
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
addressSubscriber.subscribe.calledOnce.should.be.true;
addressSubscriber.subscribe.calledWith(address1.address).should.be.true;
server.createAddress({}, function(err, address2) {
should.not.exist(err);
monitor.subscribeAddresses(wallet.id, address2.address);
addressSubscriber.subscribe.calledTwice.should.be.true;
addressSubscriber.subscribe.calledWith(address2.address).should.be.true;
done();
});
});
});
});
});
it('should create NewIncomingTx notification when a new tx arrives on registered address', function(done) {
var monitor = new BlockchainMonitor();
helpers.createAndJoinWallet(2, 2, function(server, wallet) {
server.createAddress({}, function(err, address1) {
should.not.exist(err);
monitor.subscribeWallet(server, function(err) {
should.not.exist(err);
addressSubscriber.subscribe.calledOnce.should.be.true;
addressSubscriber.subscribe.getCall(0).args[0].should.equal(address1.address);
var handler = addressSubscriber.subscribe.getCall(0).args[1];
_.isFunction(handler).should.be.true;
monitor.on('notification', function(notification) {
notification.type.should.equal('NewIncomingTx');
notification.data.address.should.equal(address1.address);
notification.data.txid.should.equal('txid');
done();
});
handler('txid');
});
});
});
});
});