Skip to content

Commit

Permalink
fix: confirmations should be reset when error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Faber committed Dec 31, 2021
1 parent 9177088 commit deae0c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('utils', () => {
await expect(res).to.eventually.be.fulfilled
})

it('should reset confirmation counter', async () => {
const stub = sandbox.stub(fetch, 'Promise').returns(successResp)
const confirmations = 3
const res = waitTillAvailable(url, { confirmations })
await tick(confirmations + 2) // wait extra rounds
expect(stub).to.have.callCount(confirmations)
await expect(res).to.eventually.be.fulfilled
})

it('should bail when a request returns an unexpected status code', async () => {
const stub = sandbox.stub(fetch, 'Promise').returns(failResp)
const retries = 3
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const waitTillAvailable = async (url: string, opts?: WaitTillAvailableOpt
try {
const res = await fetch(url, fetchOptions)
if (res.status !== options.status) {
confirmations = 0
console.warn(`GET ${url} ${res.status} !== ${options.status}`)
const err = new Error(`Wrong status code: ${res.status}`)
// if we get a 404 or 503 we know some changes in either nginx or istio might still not be ready
Expand All @@ -112,6 +113,7 @@ export const waitTillAvailable = async (url: string, opts?: WaitTillAvailableOpt
}
} catch (e) {
// Print system errors like ECONNREFUSED
confirmations = 0
console.error(`Error in try #${attempt}: `, e.message)
if (options.retries !== 0 && attempt === options.retries!) {
bail(new Error(`Max retries (${options.retries}) has been reached!`))
Expand Down

0 comments on commit deae0c3

Please sign in to comment.