diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2021-07-08 13:24:04 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2021-07-08 13:24:04 +0200 |
| commit | a77a58850d4e005df88acbac27782c0a382ce6b8 (patch) | |
| tree | 8c41f5052ea265b5470629be4612476e292e28d9 /gui/test | |
| parent | 91a30f07505eaea3f4669ba874ee497af43d89a3 (diff) | |
| parent | 3eaff61fca0121bb1a555624d00a6984d3e562c9 (diff) | |
| download | mullvadvpn-a77a58850d4e005df88acbac27782c0a382ce6b8.tar.xz mullvadvpn-a77a58850d4e005df88acbac27782c0a382ce6b8.zip | |
Merge branch 'improve-account-data-fetching'
Diffstat (limited to 'gui/test')
| -rw-r--r-- | gui/test/account-data-cache.spec.ts | 123 |
1 files changed, 88 insertions, 35 deletions
diff --git a/gui/test/account-data-cache.spec.ts b/gui/test/account-data-cache.spec.ts index c482e16a5d..6ed10c6908 100644 --- a/gui/test/account-data-cache.spec.ts +++ b/gui/test/account-data-cache.spec.ts @@ -135,41 +135,6 @@ describe('IAccountData cache', () => { }); }); - it('should refetch if account has expired', async () => { - const expiredSpy = spy(); - const nonExpiredSpy = spy(); - - const update = new Promise<void>((resolve, reject) => { - let firstAttempt = true; - const fetch = () => { - if (firstAttempt) { - expiredSpy(); - firstAttempt = false; - setTimeout(() => clock.tick(60_000), 0); - return Promise.resolve({ - expiry: new Date('1969-01-01').toISOString(), - }); - } else { - nonExpiredSpy(); - resolve(); - return Promise.resolve(dummyAccountData); - } - }; - - const cache = new AccountDataCache(fetch, () => {}); - - cache.fetch(dummyAccountToken, { - onFinish: () => {}, - onError: (_error: Error) => reject(), - }); - }); - - return expect(update).to.eventually.be.fulfilled.then(() => { - expect(expiredSpy).to.have.been.called.once; - expect(nonExpiredSpy).to.have.been.called.once; - }); - }); - it('should clear scheduled retry if another fetch is performed', async () => { const firstError = spy(); const secondSuccess = spy(); @@ -261,4 +226,92 @@ describe('IAccountData cache', () => { return expect(update).to.eventually.be.fulfilled; }); + + it('should invalidate after 60 seconds', async () => { + const fetchSpy = spy(); + const expiry = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(); + + const update = new Promise<void>((resolve, reject) => { + const cache = new AccountDataCache( + (_accountToken) => { + fetchSpy(); + return Promise.resolve({ expiry }); + }, + () => {}, + ); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + clock.tick(59_000); + // Timeout to let asynchronous tasks finish + await new Promise((resolve) => setTimeout(resolve)); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + clock.tick(1_000); + // Timeout to let asynchronous tasks finish + await new Promise((resolve) => setTimeout(resolve)); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + resolve(); + }, + onError: (_error: Error) => reject(), + }); + }, + onError: (_error: Error) => reject(), + }); + }, + onError: (_error: Error) => reject(), + }); + }); + + return expect(update).to.eventually.be.fulfilled.then(() => { + expect(fetchSpy).to.have.been.called.twice; + }); + }); + + it('should invalidate after 10 seconds when epired', async () => { + const fetchSpy = spy(); + const expiry = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); + + const update = new Promise<void>((resolve, reject) => { + const cache = new AccountDataCache( + (_accountToken) => { + fetchSpy(); + return Promise.resolve({ expiry }); + }, + () => {}, + ); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + clock.tick(9_000); + // Timeout to let asynchronous tasks finish + await new Promise((resolve) => setTimeout(resolve)); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + clock.tick(1_000); + // Timeout to let asynchronous tasks finish + await new Promise((resolve) => setTimeout(resolve)); + + cache.fetch(dummyAccountToken, { + onFinish: async () => { + resolve(); + }, + onError: (_error: Error) => reject(), + }); + }, + onError: (_error: Error) => reject(), + }); + }, + onError: (_error: Error) => reject(), + }); + }); + + return expect(update).to.eventually.be.fulfilled.then(() => { + expect(fetchSpy).to.have.been.called.twice; + }); + }); }); |
