Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): clear and close lmdb after each test suite #36343

Merged
merged 6 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ module.exports = {
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
setupFiles: [`<rootDir>/.jestSetup.js`],
setupFilesAfterEnv: [`jest-extended`],
testEnvironment: `<rootDir>/jest.environment.ts`,
}
23 changes: 23 additions & 0 deletions jest.environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const NodeEnvironment = require(`jest-environment-node`)

class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context)
}

async teardown(): Promise<void> {
// close open lmdbs after running test suite
// this prevent dangling open handles that sometimes cause problems
// particularly in windows tests (failures to move or delete a db file)
if (this.global.__GATSBY_OPEN_ROOT_LMDBS) {
for (const rootDb of this.global.__GATSBY_OPEN_ROOT_LMDBS.values()) {
await rootDb.clearAsync()
await rootDb.close()
}
this.global.__GATSBY_OPEN_ROOT_LMDBS = undefined
}
await super.teardown()
}
}

module.exports = CustomEnvironment
11 changes: 5 additions & 6 deletions packages/gatsby/src/utils/__tests__/cache-lmdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ describeWhenLMDB(`cache-lmdb`, () => {
let cache

beforeAll(async () => {
const { default: GatsbyCacheLmdb } = await import(`../cache-lmdb`)
cache = new GatsbyCacheLmdb({ name: `__test__` }).init()
const fileDir = path.join(
process.cwd(),
`.cache/caches-lmdb-${process.env.JEST_WORKER_ID}`
const { default: GatsbyCacheLmdb, resetCache } = await import(
`../cache-lmdb`
)
await fs.emptyDir(fileDir)

await resetCache()
cache = new GatsbyCacheLmdb({ name: `__test__` }).init()
})

it(`it can be instantiated`, () => {
Expand Down
46 changes: 35 additions & 11 deletions packages/gatsby/src/utils/cache-lmdb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { open, RootDatabase, Database, DatabaseOptions } from "lmdb"
import fs from "fs-extra"
import path from "path"
import * as fs from "fs-extra"
import * as path from "path"

// Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb"
const cacheDbFile =
Expand All @@ -13,8 +13,16 @@ const cacheDbFile =
}`
: `caches-lmdb`

const dbPath = path.join(process.cwd(), `.cache/${cacheDbFile}`)

function getAlreadyOpenedStore(): RootDatabase | undefined {
if (!globalThis.__GATSBY_OPEN_ROOT_LMDBS) {
globalThis.__GATSBY_OPEN_ROOT_LMDBS = new Map()
}
return globalThis.__GATSBY_OPEN_ROOT_LMDBS.get(dbPath)
}

export default class GatsbyCacheLmdb {
private static store
private db: Database | undefined
private encoding: DatabaseOptions["encoding"]
public readonly name: string
Expand Down Expand Up @@ -44,15 +52,21 @@ export default class GatsbyCacheLmdb {
}

private static getStore(): RootDatabase {
if (!GatsbyCacheLmdb.store) {
GatsbyCacheLmdb.store = open({
name: `root`,
path: path.join(process.cwd(), `.cache/${cacheDbFile}`),
compression: true,
maxDbs: 200,
})
let rootDb = getAlreadyOpenedStore()
if (rootDb) {
return rootDb
}
return GatsbyCacheLmdb.store

rootDb = open({
name: `root`,
path: dbPath,
compression: true,
maxDbs: 200,
})

globalThis.__GATSBY_OPEN_ROOT_LMDBS.set(dbPath, rootDb)

return rootDb
}

private getDb(): Database {
Expand All @@ -78,3 +92,13 @@ export default class GatsbyCacheLmdb {
return this.getDb().remove(key) as unknown as Promise<void>
}
}

export async function resetCache(): Promise<void> {
const store = getAlreadyOpenedStore()
if (store) {
await store.close()
globalThis.__GATSBY_OPEN_ROOT_LMDBS.delete(dbPath)
}

await fs.emptyDir(dbPath)
}