-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(gatsby): check if worker can access node created in different pr…
…ocess (#31771) * test(gatsby): check if worker can access node created in different process * make timeout longer for workerpool tests
- Loading branch information
Showing
9 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
9 changes: 9 additions & 0 deletions
9
packages/gatsby/src/utils/worker/__tests__/test-helpers/child-for-tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/gatsby/src/utils/worker/__tests__/test-helpers/create-test-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/gatsby/src/utils/worker/__tests__/test-helpers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./create-test-worker" | ||
export * from "./jest-helpers" |
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/utils/worker/__tests__/test-helpers/jest-helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/utils/worker/__tests__/test-helpers/wrapper-for-tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |