Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and axe312ger committed Nov 9, 2021
1 parent 7489299 commit 750a8df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
29 changes: 5 additions & 24 deletions packages/gatsby-core-utils/src/__tests__/fetch-remote-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check

import path from "path"
import zlib from "zlib"
import os from "os"
Expand Down Expand Up @@ -347,8 +348,6 @@ describe(`fetch-remote-file`, () => {
jest.runAllTimers()
await requests[0]

jest.useRealTimers()

// we still expect 2 fetches because cache can't save fast enough
expect(gotStream).toBeCalledTimes(2)
expect(fsMove).toBeCalledTimes(1)
Expand Down Expand Up @@ -408,20 +407,12 @@ describe(`fetch-remote-file`, () => {
jest.runAllTimers()
await requests[0]

jest.useRealTimers()

// we still expect 4 fetches because cache can't save fast enough
expect(gotStream).toBeCalledTimes(4)
expect(fsMove).toBeCalledTimes(2)
})

it(`doesn't keep lock when file download failed`, async () => {
jest.setTimeout(7000)

// we don't want to wait for polling to finish
jest.useFakeTimers()
jest.runAllTimers()

const cacheInternals = new Map()
const workerCache = {
get(key) {
Expand All @@ -443,17 +434,13 @@ describe(`fetch-remote-file`, () => {
})
).rejects.toThrow()

jest.runAllTimers()

await expect(
fetchRemoteFileInstanceTwo({
url: `http://external.com/500.jpg`,
cache: workerCache,
})
).rejects.toThrow()

jest.useRealTimers()

expect(gotStream).toBeCalledTimes(3)
expect(fsMove).toBeCalledTimes(0)
})
Expand All @@ -468,8 +455,6 @@ describe(`fetch-remote-file`, () => {
})

it(`fails when 500 is triggered`, async () => {
jest.setTimeout(7000)

await expect(
fetchRemoteFile({
url: `http://external.com/500.jpg`,
Expand Down Expand Up @@ -529,13 +514,13 @@ Fetch details:
})

it(`Retries when server returns 503 error till server returns 200`, async () => {
jest.setTimeout(7000)

const filePath = await fetchRemoteFile({
const fetchRemoteFileInstance = fetchRemoteFile({
url: `http://external.com/503-twice.svg`,
cache,
})

const filePath = await fetchRemoteFileInstance

expect(path.basename(filePath)).toBe(`503-twice.svg`)
expect(getFileSize(filePath)).resolves.toBe(
await getFileSize(path.join(__dirname, `./fixtures/gatsby-logo.svg`))
Expand All @@ -544,8 +529,6 @@ Fetch details:
})

it(`Stops retry when maximum attempts is reached`, async () => {
jest.setTimeout(7000)

await expect(
fetchRemoteFile({
url: `http://external.com/503-forever.svg`,
Expand Down Expand Up @@ -579,9 +562,7 @@ Fetch details:
expect(gotStream).toBeCalledTimes(3)
})
// @todo retry on network errors
it.skip(`Retries on network errors`, async () => {
jest.setTimeout(7000)

it(`Retries on network errors`, async () => {
await expect(
fetchRemoteFile({
url: `http://external.com/network-error.svg`,
Expand Down
22 changes: 13 additions & 9 deletions packages/gatsby-core-utils/src/fetch-remote-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT
? parseInt(process.env.GATSBY_INCOMPLETE_RETRY_LIMIT, 10)
: 3

// jest doesn't allow us to run all timings infinitely, so we set it 0 in tests
const BACKOFF_TIME = process.env.NODE_ENV === `test` ? 0 : 1000

function range(start: number, end: number): Array<number> {
return Array(end - start)
.fill(null)
Expand Down Expand Up @@ -396,16 +399,17 @@ function requestRemoteNode(
// (error.code && ERROR_CODES_TO_RETRY.includes(error.code))
) {
if (attempt < INCOMPLETE_RETRY_LIMIT) {
// @todo The tests don't like the delay. I tried several ways and positions.
new Promise(resolve => setTimeout(resolve, 1000 * attempt)).then(() =>
requestRemoteNode(
url,
headers,
tmpFilename,
httpOptions,
attempt + 1
setTimeout(() => {
resolve(
requestRemoteNode(
url,
headers,
tmpFilename,
httpOptions,
attempt + 1
)
)
)
}, BACKOFF_TIME * attempt)

return undefined
} else {
Expand Down

0 comments on commit 750a8df

Please sign in to comment.