2020-07-10 15:09:32 +00:00
|
|
|
import {retry} from '../src/internal/requestUtils'
|
2020-10-12 18:21:01 +00:00
|
|
|
import {HttpClientError} from '@actions/http-client'
|
2020-07-10 15:09:32 +00:00
|
|
|
|
2020-10-12 18:21:01 +00:00
|
|
|
interface ITestResponse {
|
2020-07-10 15:09:32 +00:00
|
|
|
statusCode: number
|
|
|
|
result: string | null
|
2020-10-12 18:21:01 +00:00
|
|
|
error: Error | null
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestResponse(
|
|
|
|
action: number | Error,
|
|
|
|
result: string | null = null
|
|
|
|
): ITestResponse {
|
|
|
|
if (action instanceof Error) {
|
|
|
|
return {
|
|
|
|
statusCode: -1,
|
|
|
|
result,
|
|
|
|
error: action
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
statusCode: action,
|
|
|
|
result,
|
|
|
|
error: null
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function handleResponse(
|
2020-10-12 18:21:01 +00:00
|
|
|
response: ITestResponse | undefined
|
|
|
|
): Promise<ITestResponse> {
|
2020-07-10 15:09:32 +00:00
|
|
|
if (!response) {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
fail('Retry method called too many times')
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:21:01 +00:00
|
|
|
if (response.error) {
|
|
|
|
throw response.error
|
2020-07-10 15:09:32 +00:00
|
|
|
} else {
|
|
|
|
return Promise.resolve(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testRetryExpectingResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
responses: ITestResponse[],
|
2020-07-10 15:09:32 +00:00
|
|
|
expectedResult: string | null
|
|
|
|
): Promise<void> {
|
|
|
|
responses = responses.reverse() // Reverse responses since we pop from end
|
|
|
|
|
|
|
|
const actualResult = await retry(
|
|
|
|
'test',
|
|
|
|
async () => handleResponse(responses.pop()),
|
2020-10-12 18:21:01 +00:00
|
|
|
(response: ITestResponse) => response.statusCode,
|
2020-08-17 20:32:04 +00:00
|
|
|
2, // maxAttempts
|
|
|
|
0 // delay
|
2020-07-10 15:09:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(actualResult.result).toEqual(expectedResult)
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:48:15 +00:00
|
|
|
async function testRetryConvertingErrorToResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
responses: ITestResponse[],
|
2020-08-17 15:48:15 +00:00
|
|
|
expectedStatus: number,
|
|
|
|
expectedResult: string | null
|
|
|
|
): Promise<void> {
|
|
|
|
responses = responses.reverse() // Reverse responses since we pop from end
|
|
|
|
|
|
|
|
const actualResult = await retry(
|
|
|
|
'test',
|
|
|
|
async () => handleResponse(responses.pop()),
|
2020-10-12 18:21:01 +00:00
|
|
|
(response: ITestResponse) => response.statusCode,
|
2020-08-17 20:32:04 +00:00
|
|
|
2, // maxAttempts
|
|
|
|
0, // delay
|
2020-08-17 15:48:15 +00:00
|
|
|
(e: Error) => {
|
2020-10-12 18:21:01 +00:00
|
|
|
if (e instanceof HttpClientError) {
|
|
|
|
return {
|
|
|
|
statusCode: e.statusCode,
|
|
|
|
result: null,
|
|
|
|
error: null
|
|
|
|
}
|
2020-08-17 15:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(actualResult.statusCode).toEqual(expectedStatus)
|
|
|
|
expect(actualResult.result).toEqual(expectedResult)
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:09:32 +00:00
|
|
|
async function testRetryExpectingError(
|
2020-10-12 18:21:01 +00:00
|
|
|
responses: ITestResponse[]
|
2020-07-10 15:09:32 +00:00
|
|
|
): Promise<void> {
|
|
|
|
responses = responses.reverse() // Reverse responses since we pop from end
|
|
|
|
|
|
|
|
expect(
|
|
|
|
retry(
|
|
|
|
'test',
|
|
|
|
async () => handleResponse(responses.pop()),
|
2020-10-12 18:21:01 +00:00
|
|
|
(response: ITestResponse) => response.statusCode,
|
2020-08-17 20:32:04 +00:00
|
|
|
2, // maxAttempts,
|
|
|
|
0 // delay
|
2020-07-10 15:09:32 +00:00
|
|
|
)
|
|
|
|
).rejects.toBeInstanceOf(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
test('retry works on successful response', async () => {
|
2020-10-12 18:21:01 +00:00
|
|
|
await testRetryExpectingResult([TestResponse(200, 'Ok')], 'Ok')
|
2020-07-10 15:09:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('retry works after retryable status code', async () => {
|
|
|
|
await testRetryExpectingResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
[TestResponse(503), TestResponse(200, 'Ok')],
|
2020-07-10 15:09:32 +00:00
|
|
|
'Ok'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('retry fails after exhausting retries', async () => {
|
|
|
|
await testRetryExpectingError([
|
2020-10-12 18:21:01 +00:00
|
|
|
TestResponse(503),
|
|
|
|
TestResponse(503),
|
|
|
|
TestResponse(200, 'Ok')
|
2020-07-10 15:09:32 +00:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('retry fails after non-retryable status code', async () => {
|
2020-10-12 18:21:01 +00:00
|
|
|
await testRetryExpectingError([TestResponse(500), TestResponse(200, 'Ok')])
|
2020-07-10 15:09:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('retry works after error', async () => {
|
|
|
|
await testRetryExpectingResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
[TestResponse(new Error('Test error')), TestResponse(200, 'Ok')],
|
2020-07-10 15:09:32 +00:00
|
|
|
'Ok'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('retry returns after client error', async () => {
|
|
|
|
await testRetryExpectingResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
[TestResponse(400), TestResponse(200, 'Ok')],
|
2020-07-10 15:09:32 +00:00
|
|
|
null
|
|
|
|
)
|
|
|
|
})
|
2020-08-17 15:48:15 +00:00
|
|
|
|
|
|
|
test('retry converts errors to response object', async () => {
|
|
|
|
await testRetryConvertingErrorToResult(
|
2020-10-12 18:21:01 +00:00
|
|
|
[TestResponse(new HttpClientError('Test error', 409))],
|
2020-08-17 15:48:15 +00:00
|
|
|
409,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
})
|