summaryrefslogtreecommitdiffhomepage
path: root/gui/test
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2020-04-21 12:58:21 +0200
committerOskar Nyberg <oskar@mullvad.net>2020-04-21 12:58:21 +0200
commit171530b04a5fdbc8c27fa4f29ee37a98545ab335 (patch)
tree09fd98542d83e4da1506c6ebfbf8c6fe21618da1 /gui/test
parent0e13ac8bf009d6fabfb9039f845791e9b233b856 (diff)
parent513b78e1f665f930397a123764de2b48309da3e3 (diff)
downloadmullvadvpn-171530b04a5fdbc8c27fa4f29ee37a98545ab335.tar.xz
mullvadvpn-171530b04a5fdbc8c27fa4f29ee37a98545ab335.zip
Merge branch 'prevent-account-data-cache-parallel-fetch'
Diffstat (limited to 'gui/test')
-rw-r--r--gui/test/account-data-cache.spec.ts121
1 files changed, 71 insertions, 50 deletions
diff --git a/gui/test/account-data-cache.spec.ts b/gui/test/account-data-cache.spec.ts
index 4d3dfc2ed3..bec1c6ef91 100644
--- a/gui/test/account-data-cache.spec.ts
+++ b/gui/test/account-data-cache.spec.ts
@@ -1,4 +1,4 @@
-import AccountDataCache, { AccountFetchRetryAction } from '../src/main/account-data-cache';
+import AccountDataCache from '../src/main/account-data-cache';
import { IAccountData } from '../src/shared/daemon-rpc-types';
import sinon from 'sinon';
import { expect, spy } from 'chai';
@@ -28,10 +28,7 @@ describe('IAccountData cache', () => {
const watcher = new Promise((resolve, reject) => {
cache.fetch(dummyAccountToken, {
onFinish: () => resolve(),
- onError: (_error: Error) => {
- reject();
- return AccountFetchRetryAction.stop;
- },
+ onError: (_error: Error) => reject(),
});
});
@@ -47,10 +44,7 @@ describe('IAccountData cache', () => {
const watcher = new Promise((resolve, reject) => {
cache.fetch(dummyAccountToken, {
onFinish: (_reason?: any) => resolve(),
- onError: (_error: Error) => {
- reject();
- return AccountFetchRetryAction.stop;
- },
+ onError: (_error: Error) => reject(),
});
});
@@ -66,10 +60,7 @@ describe('IAccountData cache', () => {
cache.fetch(dummyAccountToken, {
onFinish: () => {},
- onError: (_error: Error) => {
- reject();
- return AccountFetchRetryAction.stop;
- },
+ onError: (_error: Error) => reject(),
});
});
@@ -94,34 +85,7 @@ describe('IAccountData cache', () => {
cache.fetch(dummyAccountToken, {
onFinish: () => reject(),
- onError: (_error: Error) => AccountFetchRetryAction.retry,
- });
- });
-
- return expect(update).to.eventually.be.fulfilled;
- });
-
- it('should not retry if told to stop', async () => {
- const update = new Promise((resolve, reject) => {
- let firstAttempt = true;
- const fetch = () => {
- if (firstAttempt) {
- firstAttempt = false;
- setTimeout(() => clock.tick(14000), 0);
- return Promise.reject(new Error('First attempt fails'));
- } else {
- reject();
- return Promise.resolve(dummyAccountData);
- }
- };
-
- const cache = new AccountDataCache(fetch, () => resolve());
-
- setTimeout(resolve, 12000);
-
- cache.fetch(dummyAccountToken, {
- onFinish: () => {},
- onError: (_error: Error) => AccountFetchRetryAction.stop,
+ onError: (_error: Error) => {},
});
});
@@ -129,7 +93,7 @@ describe('IAccountData cache', () => {
});
it('should cancel first fetch', async () => {
- const firstError = spy((_error: Error) => AccountFetchRetryAction.stop);
+ const firstError = spy((_error: Error) => {});
const secondSuccess = spy();
const update = new Promise<IAccountData>((resolve, reject) => {
@@ -140,10 +104,7 @@ describe('IAccountData cache', () => {
cache.fetch('1231231231', {
onFinish: secondSuccess,
- onError: () => {
- reject();
- return AccountFetchRetryAction.stop;
- },
+ onError: () => reject(),
});
return new Promise<IAccountData>((resolve) => {
@@ -199,10 +160,7 @@ describe('IAccountData cache', () => {
cache.fetch(dummyAccountToken, {
onFinish: () => {},
- onError: (_error: Error) => {
- reject();
- return AccountFetchRetryAction.stop;
- },
+ onError: (_error: Error) => reject(),
});
});
@@ -211,4 +169,67 @@ describe('IAccountData cache', () => {
expect(nonExpiredSpy).to.have.been.called.once;
});
});
+
+ it('should clear scheduled retry if another fetch is performed', async () => {
+ const firstError = spy();
+ const secondSuccess = spy();
+ const updateHandler = spy();
+
+ const update = new Promise((resolve, reject) => {
+ let attempts = 0;
+ const fetch = () => {
+ attempts++;
+ if (attempts === 1) {
+ return Promise.reject(new Error('First attempt fails'));
+ } else if (attempts === 2) {
+ setTimeout(() => clock.tick(8000));
+ return Promise.resolve(dummyAccountData);
+ } else {
+ reject();
+ return Promise.resolve(dummyAccountData);
+ }
+ };
+
+ const cache = new AccountDataCache(fetch, updateHandler);
+
+ cache.fetch(dummyAccountToken, {
+ onFinish: () => {},
+ onError: (_error: Error) => firstError(),
+ });
+ setTimeout(() => {
+ cache.fetch(dummyAccountToken, {
+ onFinish: () => {
+ secondSuccess();
+ setTimeout(resolve);
+ },
+ onError: (_error: Error) => {},
+ });
+ });
+ });
+
+ return expect(update).to.eventually.be.fulfilled.then(() => {
+ expect(firstError).to.have.been.called.once;
+ expect(secondSuccess).to.have.been.called.once;
+ expect(updateHandler).to.have.been.called.twice;
+ });
+ });
+
+ it('should not perform a fetch if called twice synchronously', async () => {
+ const fetchSpy = spy();
+ const update = new Promise((resolve, _reject) => {
+ const fetch = () => {
+ fetchSpy();
+ return Promise.resolve(dummyAccountData);
+ };
+
+ const cache = new AccountDataCache(fetch, () => {});
+ const onError = (_error: Error) => {};
+ cache.fetch(dummyAccountToken, { onFinish: () => {}, onError });
+ cache.fetch(dummyAccountToken, { onFinish: () => resolve(), onError });
+ });
+
+ return expect(update).to.eventually.be.fulfilled.then(() => {
+ expect(fetchSpy).to.have.been.called.once;
+ });
+ });
});