From ff8c3604359f4df5e5ffe7a12863077f15d88cd7 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 31 Mar 2015 12:29:28 -0300 Subject: [PATCH] cleanup test code --- lib/blockchainmonitor.js | 25 +++-- test/integration/server.js | 188 ++++++++++++++++++------------------- 2 files changed, 111 insertions(+), 102 deletions(-) diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index a66fc8c..2947e72 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -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)); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index 4486473..bd700e2 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -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'); + }); + }); + }); + }); +});