Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit c81cec5

Browse files
committed
Use setTimeout in polling exchange rate and test poll in test
1 parent 15da9a4 commit c81cec5

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/action/wallet.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { observe } from 'mobx';
77
import { toBuffer, parseSat, checkHttpStatus, nap } from '../helper';
8-
import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY } from '../config';
8+
import { MIN_PASSWORD_LENGTH, NOTIFICATION_DELAY, RATE_DELAY } from '../config';
99
import * as log from './log';
1010

1111
class WalletAction {
@@ -285,10 +285,9 @@ class WalletAction {
285285
* @return {Promise<undefined>}
286286
*/
287287
async pollExchangeRate() {
288-
// Poll every 15 minutes, starting now.
289288
await this.getExchangeRate();
290-
let interval = 15 * 60 * 1000;
291-
setInterval(() => this.getExchangeRate(), interval);
289+
clearTimeout(this.tPollRate);
290+
this.tPollRate = setTimeout(() => this.pollExchangeRate(), RATE_DELAY);
292291
}
293292

294293
/**

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports.RETRY_DELAY = 3000;
22
module.exports.LND_INIT_DELAY = 5000;
33
module.exports.NOTIFICATION_DELAY = 5000;
4+
module.exports.RATE_DELAY = 15 * 60 * 1000;
45

56
module.exports.LND_PORT = 10009;
67
module.exports.LND_PEER_PORT = 10019;

test/unit/action/wallet.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import WalletAction from '../../../src/action/wallet';
55
import NavAction from '../../../src/action/nav';
66
import NotificationAction from '../../../src/action/notification';
77
import * as logger from '../../../src/action/log';
8+
import { nap } from '../../../src/helper';
89
import nock from 'nock';
910
import 'isomorphic-fetch';
1011

@@ -23,6 +24,7 @@ describe('Action Wallet Unit Tests', () => {
2324
store = new Store();
2425
require('../../../src/config').RETRY_DELAY = 1;
2526
require('../../../src/config').NOTIFICATION_DELAY = 1;
27+
require('../../../src/config').RATE_DELAY = 1;
2628
grpc = sinon.createStubInstance(GrpcAction);
2729
db = sinon.createStubInstance(AppStorage);
2830
notification = sinon.createStubInstance(NotificationAction);
@@ -31,6 +33,7 @@ describe('Action Wallet Unit Tests', () => {
3133
});
3234

3335
afterEach(() => {
36+
clearTimeout(wallet.tPollRate);
3437
sandbox.restore();
3538
});
3639

@@ -328,7 +331,8 @@ describe('Action Wallet Unit Tests', () => {
328331
it('should call getExchangeRate', async () => {
329332
sandbox.stub(wallet, 'getExchangeRate');
330333
await wallet.pollExchangeRate();
331-
expect(wallet.getExchangeRate, 'was called once');
334+
await nap(30);
335+
expect(wallet.getExchangeRate.callCount, 'to be greater than', 1);
332336
});
333337
});
334338

0 commit comments

Comments
 (0)