Skip to content

Commit f990e08

Browse files
authoredAug 11, 2022
fix(test): clear and close lmdb after each test suite (#36343)

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed
 

‎jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,5 @@ module.exports = {
149149
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
150150
setupFiles: [`<rootDir>/.jestSetup.js`],
151151
setupFilesAfterEnv: [`jest-extended`],
152+
testEnvironment: `<rootDir>/jest.environment.ts`,
152153
}

‎jest.environment.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const NodeEnvironment = require(`jest-environment-node`)
2+
3+
class CustomEnvironment extends NodeEnvironment {
4+
constructor(config, context) {
5+
super(config, context)
6+
}
7+
8+
async teardown(): Promise<void> {
9+
// close open lmdbs after running test suite
10+
// this prevent dangling open handles that sometimes cause problems
11+
// particularly in windows tests (failures to move or delete a db file)
12+
if (this.global.__GATSBY_OPEN_ROOT_LMDBS) {
13+
for (const rootDb of this.global.__GATSBY_OPEN_ROOT_LMDBS.values()) {
14+
await rootDb.clearAsync()
15+
await rootDb.close()
16+
}
17+
this.global.__GATSBY_OPEN_ROOT_LMDBS = undefined
18+
}
19+
await super.teardown()
20+
}
21+
}
22+
23+
module.exports = CustomEnvironment

‎packages/gatsby/src/utils/__tests__/cache-lmdb.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ describeWhenLMDB(`cache-lmdb`, () => {
1212
let cache
1313

1414
beforeAll(async () => {
15-
const { default: GatsbyCacheLmdb } = await import(`../cache-lmdb`)
16-
cache = new GatsbyCacheLmdb({ name: `__test__` }).init()
17-
const fileDir = path.join(
18-
process.cwd(),
19-
`.cache/caches-lmdb-${process.env.JEST_WORKER_ID}`
15+
const { default: GatsbyCacheLmdb, resetCache } = await import(
16+
`../cache-lmdb`
2017
)
21-
await fs.emptyDir(fileDir)
18+
19+
await resetCache()
20+
cache = new GatsbyCacheLmdb({ name: `__test__` }).init()
2221
})
2322

2423
it(`it can be instantiated`, () => {

‎packages/gatsby/src/utils/cache-lmdb.ts

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { open, RootDatabase, Database, DatabaseOptions } from "lmdb"
2-
import fs from "fs-extra"
3-
import path from "path"
2+
import * as fs from "fs-extra"
3+
import * as path from "path"
44

55
// Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb"
66
const cacheDbFile =
@@ -13,8 +13,16 @@ const cacheDbFile =
1313
}`
1414
: `caches-lmdb`
1515

16+
const dbPath = path.join(process.cwd(), `.cache/${cacheDbFile}`)
17+
18+
function getAlreadyOpenedStore(): RootDatabase | undefined {
19+
if (!globalThis.__GATSBY_OPEN_ROOT_LMDBS) {
20+
globalThis.__GATSBY_OPEN_ROOT_LMDBS = new Map()
21+
}
22+
return globalThis.__GATSBY_OPEN_ROOT_LMDBS.get(dbPath)
23+
}
24+
1625
export default class GatsbyCacheLmdb {
17-
private static store
1826
private db: Database | undefined
1927
private encoding: DatabaseOptions["encoding"]
2028
public readonly name: string
@@ -44,15 +52,21 @@ export default class GatsbyCacheLmdb {
4452
}
4553

4654
private static getStore(): RootDatabase {
47-
if (!GatsbyCacheLmdb.store) {
48-
GatsbyCacheLmdb.store = open({
49-
name: `root`,
50-
path: path.join(process.cwd(), `.cache/${cacheDbFile}`),
51-
compression: true,
52-
maxDbs: 200,
53-
})
55+
let rootDb = getAlreadyOpenedStore()
56+
if (rootDb) {
57+
return rootDb
5458
}
55-
return GatsbyCacheLmdb.store
59+
60+
rootDb = open({
61+
name: `root`,
62+
path: dbPath,
63+
compression: true,
64+
maxDbs: 200,
65+
})
66+
67+
globalThis.__GATSBY_OPEN_ROOT_LMDBS.set(dbPath, rootDb)
68+
69+
return rootDb
5670
}
5771

5872
private getDb(): Database {
@@ -78,3 +92,13 @@ export default class GatsbyCacheLmdb {
7892
return this.getDb().remove(key) as unknown as Promise<void>
7993
}
8094
}
95+
96+
export async function resetCache(): Promise<void> {
97+
const store = getAlreadyOpenedStore()
98+
if (store) {
99+
await store.close()
100+
globalThis.__GATSBY_OPEN_ROOT_LMDBS.delete(dbPath)
101+
}
102+
103+
await fs.emptyDir(dbPath)
104+
}

0 commit comments

Comments
 (0)
Please sign in to comment.