Skip to content

Commit

Permalink
fix(test): clear and close lmdb after each test suite (#36343)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Aug 11, 2022
1 parent 7fcf580 commit f990e08
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -149,4 +149,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
@@ -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
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
@@ -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)
}

0 comments on commit f990e08

Please sign in to comment.