1
0
Fork 0
toolkit/packages/tool-cache/__tests__/retry-helper.test.ts

126 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

import * as core from '@actions/core'
import {RetryHelper} from '../src/retry-helper'
let info: string[]
let retryHelper: RetryHelper
describe('retry-helper tests', () => {
beforeAll(() => {
// Mock @actions/core info()
jest.spyOn(core, 'info').mockImplementation((message: string) => {
info.push(message)
})
retryHelper = new RetryHelper(3, 0, 0)
})
beforeEach(() => {
// Reset info
info = []
})
afterAll(() => {
// Restore
jest.restoreAllMocks()
})
it('first attempt succeeds', async () => {
const actual = await retryHelper.execute(async () => {
return 'some result'
})
expect(actual).toBe('some result')
expect(info).toHaveLength(0)
})
it('second attempt succeeds', async () => {
let attempts = 0
const actual = await retryHelper.execute(async () => {
if (++attempts === 1) {
throw new Error('some error')
}
return Promise.resolve('some result')
})
expect(attempts).toBe(2)
expect(actual).toBe('some result')
expect(info).toHaveLength(2)
expect(info[0]).toBe('some error')
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
})
it('third attempt succeeds', async () => {
let attempts = 0
const actual = await retryHelper.execute(async () => {
if (++attempts < 3) {
throw new Error(`some error ${attempts}`)
}
return Promise.resolve('some result')
})
expect(attempts).toBe(3)
expect(actual).toBe('some result')
expect(info).toHaveLength(4)
expect(info[0]).toBe('some error 1')
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
expect(info[2]).toBe('some error 2')
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
})
it('all attempts fail', async () => {
let attempts = 0
Audit Fix (#1480) * fixing audit failures * replacing lerna bootstrap with npm command * audit fix for cache and tool-cache * updating tunnel * upgrading core packages * re-adding tunnel as prod dep * updating dependencies * updating exec deps * updating exec io package * . * Revert * updating packages * adding core as dep * updating learna config * updating lerna commands * Removing audit failing packages in cache + tool-cache * updating contribution bootstrap description * updating libraries * prettier lint * hiding stricter rules * updating prettier command * Removing unknown flag * Adding eslint prettier * ignoring sym links * updating ignore path * updating prettier rules * changing prettier + github ver * updating ts and ignores * Revert ts * Adding unknown ignores * downgrading lerna * . * adding nx * Adding lint auto lint rules * updating eslint ignore for glob packages * Adding subdirs to ignore * adding flag for ignore pattern in linter * Expanding ignore regex * Adding ignore rules * adding another ignore pattern to tsconfig eslint * adding ignore pattern to eslintrc * syncing package-json * updating traverse * . * test adding core and http client to base package * running npm ci * adding tsconfig paths * adding base URL * Adding explicit path to core and http-client * editing tsc call * updating artifact packages * force build * updating lock file version * updating lock file version * upgrading node version * Adding babel traverse back * fixing build issue * fixing typescript ver * updating package json * Adding ignore for artifact test * adding ignore to flags * unlink after test completes * cleanup * merge + package edit
2023-08-03 20:36:11 +00:00
let error: Error = null as unknown as Error
try {
await retryHelper.execute(() => {
throw new Error(`some error ${++attempts}`)
})
} catch (err) {
error = err
}
expect(error.message).toBe('some error 3')
expect(attempts).toBe(3)
expect(info).toHaveLength(4)
expect(info[0]).toBe('some error 1')
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
expect(info[2]).toBe('some error 2')
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
})
it('checks retryable after first attempt', async () => {
let attempts = 0
Audit Fix (#1480) * fixing audit failures * replacing lerna bootstrap with npm command * audit fix for cache and tool-cache * updating tunnel * upgrading core packages * re-adding tunnel as prod dep * updating dependencies * updating exec deps * updating exec io package * . * Revert * updating packages * adding core as dep * updating learna config * updating lerna commands * Removing audit failing packages in cache + tool-cache * updating contribution bootstrap description * updating libraries * prettier lint * hiding stricter rules * updating prettier command * Removing unknown flag * Adding eslint prettier * ignoring sym links * updating ignore path * updating prettier rules * changing prettier + github ver * updating ts and ignores * Revert ts * Adding unknown ignores * downgrading lerna * . * adding nx * Adding lint auto lint rules * updating eslint ignore for glob packages * Adding subdirs to ignore * adding flag for ignore pattern in linter * Expanding ignore regex * Adding ignore rules * adding another ignore pattern to tsconfig eslint * adding ignore pattern to eslintrc * syncing package-json * updating traverse * . * test adding core and http client to base package * running npm ci * adding tsconfig paths * adding base URL * Adding explicit path to core and http-client * editing tsc call * updating artifact packages * force build * updating lock file version * updating lock file version * upgrading node version * Adding babel traverse back * fixing build issue * fixing typescript ver * updating package json * Adding ignore for artifact test * adding ignore to flags * unlink after test completes * cleanup * merge + package edit
2023-08-03 20:36:11 +00:00
let error: Error = null as unknown as Error
try {
await retryHelper.execute(
async () => {
throw new Error(`some error ${++attempts}`)
},
() => false
)
} catch (err) {
error = err
}
expect(error.message).toBe('some error 1')
expect(attempts).toBe(1)
expect(info).toHaveLength(0)
})
it('checks retryable after second attempt', async () => {
let attempts = 0
Audit Fix (#1480) * fixing audit failures * replacing lerna bootstrap with npm command * audit fix for cache and tool-cache * updating tunnel * upgrading core packages * re-adding tunnel as prod dep * updating dependencies * updating exec deps * updating exec io package * . * Revert * updating packages * adding core as dep * updating learna config * updating lerna commands * Removing audit failing packages in cache + tool-cache * updating contribution bootstrap description * updating libraries * prettier lint * hiding stricter rules * updating prettier command * Removing unknown flag * Adding eslint prettier * ignoring sym links * updating ignore path * updating prettier rules * changing prettier + github ver * updating ts and ignores * Revert ts * Adding unknown ignores * downgrading lerna * . * adding nx * Adding lint auto lint rules * updating eslint ignore for glob packages * Adding subdirs to ignore * adding flag for ignore pattern in linter * Expanding ignore regex * Adding ignore rules * adding another ignore pattern to tsconfig eslint * adding ignore pattern to eslintrc * syncing package-json * updating traverse * . * test adding core and http client to base package * running npm ci * adding tsconfig paths * adding base URL * Adding explicit path to core and http-client * editing tsc call * updating artifact packages * force build * updating lock file version * updating lock file version * upgrading node version * Adding babel traverse back * fixing build issue * fixing typescript ver * updating package json * Adding ignore for artifact test * adding ignore to flags * unlink after test completes * cleanup * merge + package edit
2023-08-03 20:36:11 +00:00
let error: Error = null as unknown as Error
try {
await retryHelper.execute(
async () => {
throw new Error(`some error ${++attempts}`)
},
(e: Error) => e.message === 'some error 1'
)
} catch (err) {
error = err
}
expect(error.message).toBe('some error 2')
expect(attempts).toBe(2)
expect(info).toHaveLength(2)
expect(info[0]).toBe('some error 1')
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
})
})