better tests

This commit is contained in:
Matias Alejo Garcia 2017-11-02 14:31:56 -03:00
parent 23278a06fb
commit 5c22852328
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
4 changed files with 35 additions and 9 deletions

View File

@ -99,6 +99,9 @@ Defaults.HISTORY_CACHE_ADDRESS_THRESOLD = 100;
// Number of addresses from which balance in cache for a few seconds
Defaults.BALANCE_CACHE_ADDRESS_THRESOLD = Defaults.HISTORY_CACHE_ADDRESS_THRESOLD;
Defaults.BALANCE_CACHE_DIRECT_DURATION = 60;
Defaults.BALANCE_CACHE_DURATION = 10;
// Cache time for blockchain height (in seconds)
Defaults.BLOCKHEIGHT_CACHE_TIME = 10 * 60;

View File

@ -1248,10 +1248,10 @@ WalletService.prototype._getBalanceFromAddresses = function(opts, cb, i) {
function checkBalanceCache(cb) {
if (opts.addresses.length < Defaults.BALANCE_CACHE_ADDRESS_THRESOLD)
if (opts.addresses.length < Defaults.BALANCE_CACHE_ADDRESS_THRESOLD || !opts.fastCache)
return cb();
self.storage.checkAndUseBalanceCache(self.walletId, opts.addresses, cb);
self.storage.checkAndUseBalanceCache(self.walletId, opts.addresses, opts.fastCache, cb);
};
function storeBalanceCache(balance, cb) {
@ -1319,7 +1319,8 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) {
self._getBalanceFromAddresses({
coin: opts.coin,
addresses: addresses
addresses: addresses,
fastCache: opts.fastCache,
}, function(err, balance, cacheUsed) {
if (err) return cb(err);
@ -1403,6 +1404,7 @@ WalletService.prototype.getBalance = function(opts, cb, i) {
}
if (!opts.twoStep) {
opts.fastCache = Defaults.BALANCE_CACHE_DIRECT_DURATION;
return self._getBalanceOneStep(opts, cb);
}
@ -1441,7 +1443,10 @@ WalletService.prototype.getBalance = function(opts, cb, i) {
setTimeout(function() {
log.debug('Running full balance query');
opts.alreadyQueriedLength = activeAddresses.length;
opts.fastCache = Defaults.BALANCE_CACHE_DURATION;
self._getBalanceOneStep(opts, function(err, fullBalance, skipped) {
if (err) return;
if (!skipped && !_.isEqual(partialBalance, fullBalance)) {

View File

@ -1071,11 +1071,10 @@ Storage.prototype._addressHash = function(addresses) {
return Bitcore.crypto.Hash.ripemd160(new Buffer(all)).toString('hex');
};
Storage.prototype.checkAndUseBalanceCache = function(walletId, addresses, cb) {
Storage.prototype.checkAndUseBalanceCache = function(walletId, addresses, duration, cb) {
var self = this;
var key = self._addressHash(addresses);
var now = Date.now();
var BALANCE_CACHE_DURATION = 10;
self.db.collection(collections.CACHE).findOne({
@ -1086,7 +1085,7 @@ Storage.prototype.checkAndUseBalanceCache = function(walletId, addresses, cb) {
if (err) return cb(err);
if (!ret) return cb();
var validFor = ret.ts + 10 * 1000 - now;
var validFor = ret.ts + duration * 1000 - now;
if (validFor > 0) {
log.debug('','Using Balance Cache valid for %d ms more', validFor);

View File

@ -2677,14 +2677,14 @@ describe('Wallet service', function() {
});
it('should not get balance from cache, after 11secs', function(done) {
it('should not get balance from cache, after X secs, on a direct hit', function(done) {
helpers.stubUtxos(server, wallet, [1, 'u2', 3], function() {
server.getBalance({
twoStep: false
}, function(err, balance, cacheUsed) {
should.not.exist(err);
should.not.exist(cacheUsed);
clock.tick((10+1) * 1000);
clock.tick(( Defaults.BALANCE_CACHE_DIRECT_DURATION +1) * 1000);
server.getBalance({
twoStep: false
}, function(err, balance, cacheUsed) {
@ -2698,6 +2698,25 @@ describe('Wallet service', function() {
});
it('should not get balance from cache, after X secs, on a twostep hit', function(done) {
helpers.stubUtxos(server, wallet, [1, 'u2', 3], function() {
server.getBalance({
twoStep: false
}, function(err, balance, cacheUsed) {
should.not.exist(err);
should.not.exist(cacheUsed);
clock.tick(( Defaults.BALANCE_CACHE_DIRECT_DURATION - 1) * 1000);
server.getBalance({
twoStep: true
}, function(err, balance, cacheUsed) {
should.not.exist(err);
should.not.exist(cacheUsed);
checkBalance(balance);
done();
});
});
});
});
});
@ -7579,7 +7598,7 @@ describe('Wallet service', function() {
function(server, wallet, next) {
server.scan({}, function(err) {
should.not.exist(err);
server.getBalance(wallet.id, function(err, balance) {
server.getBalance({}, function(err, balance) {
balance.totalAmount.should.equal(helpers.toSatoshi(6));
next();
})