Skip to content

Commit

Permalink
test(gatsby): check if worker can access node created in different pr…
Browse files Browse the repository at this point in the history
…ocess (#31771)

* test(gatsby): check if worker can access node created in different process

* make timeout longer for workerpool tests
  • Loading branch information
pieh authored Jun 8, 2021
1 parent af692f7 commit f92c2b0
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
`<rootDir>/node_modules/`,
`<rootDir>/packages/gatsby-admin/.cache/`,
`<rootDir>/packages/gatsby-plugin-gatsby-cloud/src/__tests__/mocks/`,
`<rootDir>/packages/gatsby/src/utils/worker/__tests__/test-helpers/`,
`<rootDir>/deprecated-packages/`,
`__tests__/fixtures`,
`__testfixtures__/`,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
},
"devDependencies": {
"@babel/cli": "^7.14.3",
"@babel/register": "^7.13.16",
"@babel/runtime": "^7.14.0",
"@types/eslint": "^7.2.6",
"@types/micromatch": "^4.0.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/src/datastore/lmdb/lmdb-datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { emitter, replaceReducer } from "../../redux"

const rootDbFile =
process.env.NODE_ENV === `test`
? `test-datastore-${process.env.JEST_WORKER_ID}`
? `test-datastore-${
process.env.FORCE_TEST_DATABASE_ID ?? process.env.JEST_WORKER_ID
}`
: `datastore`

let rootDb
Expand Down
59 changes: 59 additions & 0 deletions packages/gatsby/src/utils/worker/__tests__/datastore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
createTestWorker,
GatsbyTestWorkerPool,
itWhenLMDB,
} from "./test-helpers"
import { store } from "../../../redux"
import { actions } from "../../../redux/actions"
import { getDataStore } from "../../../datastore"

let worker: GatsbyTestWorkerPool | undefined

beforeEach(() => {
store.dispatch({ type: `DELETE_CACHE` })
})

afterEach(() => {
if (worker) {
worker.end()
worker = undefined
}
})

itWhenLMDB(`worker can access node created in main process`, async () => {
worker = createTestWorker()

const testNodeId = `shared-node`

expect(getDataStore().getNode(testNodeId)).toBeFalsy()
expect(await worker.getNodeFromWorker(testNodeId)).toBeFalsy()

const node = {
id: testNodeId,
parent: null,
children: [],
internal: { type: `SharedNode`, contentDigest: `0` },
field: `should-be-accessible-in-worker`,
}
await store.dispatch(actions.createNode(node, { name: `test` }))
await getDataStore().ready()

const nodeStoredInMainProcess = getDataStore().getNode(testNodeId)
const nodeStoredInWorkerProcess = await worker.getNodeFromWorker(testNodeId)

expect(nodeStoredInWorkerProcess).toMatchInlineSnapshot(`
Object {
"children": Array [],
"field": "should-be-accessible-in-worker",
"id": "shared-node",
"internal": Object {
"contentDigest": "0",
"counter": 1,
"owner": "test",
"type": "SharedNode",
},
"parent": null,
}
`)
expect(nodeStoredInWorkerProcess).toEqual(nodeStoredInMainProcess)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getNode } from "../../../../datastore"

// re-export all usual methods from production worker
export * from "../../child"

// additional functions to be able to write assertions that won't be available in production code
export function getNodeFromWorker(nodeId: string): ReturnType<typeof getNode> {
return getNode(nodeId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Worker from "jest-worker"
import type { CreateWorkerPoolType } from "../../types"

export type GatsbyTestWorkerPool = CreateWorkerPoolType<
typeof import("./child-for-tests")
>

export function createTestWorker(): GatsbyTestWorkerPool {
// all child processes of this worker pool would have JEST_WORKER_ID set to 1
// but running jest tests would create processes with possibly other IDs
// this will let child processes use same database ID as parent process (one that executes test)
process.env.FORCE_TEST_DATABASE_ID = process.env.JEST_WORKER_ID

const worker = new Worker(require.resolve(`./wrapper-for-tests`), {
numWorkers: 1,
forkOptions: {
silent: false,
},
maxRetries: 1,
}) as GatsbyTestWorkerPool
return worker
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./create-test-worker"
export * from "./jest-helpers"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// spawning processing will occasionally exceed default time for test
// this sets up a bit longer time for each test to avoid flakiness due to tests not being executed within short time frame
jest.setTimeout(35000)

export const itWhenLMDB = process.env.GATSBY_EXPERIMENTAL_LMDB_STORE
? it
: it.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// spawned process won't use jest config to support TS, so we need to add support ourselves
require(`@babel/register`)({
extensions: [`.js`, `.ts`],
configFile: require.resolve(`../../../../../babel.config.js`),
})

module.exports = require(`./child-for-tests`)

0 comments on commit f92c2b0

Please sign in to comment.