-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add sleep util * add retry logic to hub API client * unit tests for retry
- Loading branch information
1 parent
a2800d7
commit 06e0acc
Showing
3 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
import mockPlugin from '@/fixtures/plugin.json'; | ||
|
||
import { HubAPIClient } from './HubAPIClient'; | ||
import { validatePluginData } from './validate'; | ||
|
||
describe('HubAPIClient', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should retry fetching a failed request', async () => { | ||
let retryCount = 0; | ||
jest.spyOn(axios, 'create').mockImplementation(() => { | ||
return { | ||
request: jest.fn(() => { | ||
if (retryCount > 1) { | ||
return Promise.resolve({ | ||
data: mockPlugin, | ||
status: 200, | ||
}); | ||
} | ||
|
||
retryCount += 1; | ||
throw new Error('failure'); | ||
}), | ||
} as unknown as AxiosInstance; | ||
}); | ||
|
||
const client = new HubAPIClient(); | ||
await expect(client.getPlugin('test')).resolves.toEqual( | ||
validatePluginData(mockPlugin), | ||
); | ||
}); | ||
|
||
it('should fail when the request fails too many times', async () => { | ||
jest.spyOn(axios, 'create').mockImplementation(() => { | ||
return { | ||
request: jest.fn().mockRejectedValue(new Error('failure')), | ||
} as unknown as AxiosInstance; | ||
}); | ||
|
||
const client = new HubAPIClient(); | ||
await expect(client.getPlugin('test')).rejects.toThrow('failure'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function sleep(duration: number): Promise<void> { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, duration); | ||
}); | ||
} |