From 15da9a4ea50a6310b46713e78942ce7064608b67 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Mon, 6 Aug 2018 17:47:13 -0700 Subject: [PATCH 1/2] Update exchange rate every 15 minutes --- src/action/wallet.js | 14 +++++++++++++- test/unit/action/wallet.spec.js | 12 ++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/action/wallet.js b/src/action/wallet.js index 11f108a4c..5d3024334 100644 --- a/src/action/wallet.js +++ b/src/action/wallet.js @@ -113,7 +113,7 @@ class WalletAction { this.getBalance(), this.getChannelBalance(), this.getNewAddress(), - this.getExchangeRate(), + this.pollExchangeRate(), ]); } @@ -279,6 +279,18 @@ class WalletAction { } } + /** + * Poll for the current btc/fiat exchange rate based on the currently selected + * fiat currency every 15 minutes. + * @return {Promise} + */ + async pollExchangeRate() { + // Poll every 15 minutes, starting now. + await this.getExchangeRate(); + let interval = 15 * 60 * 1000; + setInterval(() => this.getExchangeRate(), interval); + } + /** * Fetch a current btc/fiat exchange rate based on the currently selected * fiat currency and persist the value on disk for the next time the app diff --git a/test/unit/action/wallet.spec.js b/test/unit/action/wallet.spec.js index c9851aa64..9b9d8f3a9 100644 --- a/test/unit/action/wallet.spec.js +++ b/test/unit/action/wallet.spec.js @@ -106,10 +106,10 @@ describe('Action Wallet Unit Tests', () => { describe('update()', () => { it('should refresh balances, exchange rate and address', async () => { - sandbox.stub(wallet, 'getExchangeRate'); + sandbox.stub(wallet, 'pollExchangeRate'); await wallet.update(); expect(grpc.sendCommand, 'was called thrice'); - expect(wallet.getExchangeRate, 'was called once'); + expect(wallet.pollExchangeRate, 'was called once'); }); }); @@ -324,6 +324,14 @@ describe('Action Wallet Unit Tests', () => { }); }); + describe('pollExchangeRate()', () => { + it('should call getExchangeRate', async () => { + sandbox.stub(wallet, 'getExchangeRate'); + await wallet.pollExchangeRate(); + expect(wallet.getExchangeRate, 'was called once'); + }); + }); + describe('getExchangeRate()', () => { it('should get exchange rate', async () => { nock('https://blockchain.info') From c81cec57131bd5bd0a145347916e8dd373535308 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 7 Aug 2018 11:42:42 +0200 Subject: [PATCH 2/2] Use setTimeout in polling exchange rate and test poll in test --- src/action/wallet.js | 7 +++---- src/config.js | 1 + test/unit/action/wallet.spec.js | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/action/wallet.js b/src/action/wallet.js index 5d3024334..3104f9332 100644 --- a/src/action/wallet.js +++ b/src/action/wallet.js @@ -5,7 +5,7 @@ import { observe } from 'mobx'; import { toBuffer, parseSat, checkHttpStatus, nap } from '../helper'; -import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY } from '../config'; +import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY, RATE_DELAY } from '../config'; import * as log from './log'; class WalletAction { @@ -285,10 +285,9 @@ class WalletAction { * @return {Promise} */ async pollExchangeRate() { - // Poll every 15 minutes, starting now. await this.getExchangeRate(); - let interval = 15 * 60 * 1000; - setInterval(() => this.getExchangeRate(), interval); + clearTimeout(this.tPollRate); + this.tPollRate = setTimeout(() => this.pollExchangeRate(), RATE_DELAY); } /** diff --git a/src/config.js b/src/config.js index 0ffeb3cde..db8c60f56 100644 --- a/src/config.js +++ b/src/config.js @@ -1,6 +1,7 @@ module.exports.RETRY_DELAY = 3000; module.exports.LND_INIT_DELAY = 5000; module.exports.NOTIFICATION_DELAY = 5000; +module.exports.RATE_DELAY = 15 * 60 * 1000; module.exports.LND_PORT = 10009; module.exports.LND_PEER_PORT = 10019; diff --git a/test/unit/action/wallet.spec.js b/test/unit/action/wallet.spec.js index 9b9d8f3a9..a8c82b47f 100644 --- a/test/unit/action/wallet.spec.js +++ b/test/unit/action/wallet.spec.js @@ -5,6 +5,7 @@ import WalletAction from '../../../src/action/wallet'; import NavAction from '../../../src/action/nav'; import NotificationAction from '../../../src/action/notification'; import * as logger from '../../../src/action/log'; +import { nap } from '../../../src/helper'; import nock from 'nock'; import 'isomorphic-fetch'; @@ -23,6 +24,7 @@ describe('Action Wallet Unit Tests', () => { store = new Store(); require('../../../src/config').RETRY_DELAY = 1; require('../../../src/config').NOTIFICATION_DELAY = 1; + require('../../../src/config').RATE_DELAY = 1; grpc = sinon.createStubInstance(GrpcAction); db = sinon.createStubInstance(AppStorage); notification = sinon.createStubInstance(NotificationAction); @@ -31,6 +33,7 @@ describe('Action Wallet Unit Tests', () => { }); afterEach(() => { + clearTimeout(wallet.tPollRate); sandbox.restore(); }); @@ -328,7 +331,8 @@ describe('Action Wallet Unit Tests', () => { it('should call getExchangeRate', async () => { sandbox.stub(wallet, 'getExchangeRate'); await wallet.pollExchangeRate(); - expect(wallet.getExchangeRate, 'was called once'); + await nap(30); + expect(wallet.getExchangeRate.callCount, 'to be greater than', 1); }); });