forked from seald/nedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openFds.test.js
61 lines (56 loc) · 1.59 KB
/
openFds.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs')
const fsPromises = fs.promises
const Nedb = require('../lib/datastore')
const N = 64
// A console.error triggers an error of the parent test
const test = async () => {
let filehandles = []
try {
for (let i = 0; i < 2 * N + 1; i++) {
const filehandle = await fsPromises.open('./test_lac/openFdsTestFile', 'r')
filehandles.push(filehandle)
}
console.error('No error occurred while opening a file too many times')
process.exit(1)
} catch (error) {
if (error.code !== 'EMFILE') {
console.error(error)
process.exit(1)
}
} finally {
for (const filehandle of filehandles) {
await filehandle.close()
}
filehandles = []
}
try {
for (let i = 0; i < N; i++) {
const filehandle = await fsPromises.open('./test_lac/openFdsTestFile2', 'r')
filehandles.push(filehandle)
}
} catch (error) {
console.error(`An unexpected error occurred when opening file not too many times with error: ${error}`)
process.exit(1)
} finally {
for (const filehandle of filehandles) {
await filehandle.close()
}
}
try {
const db = new Nedb({ filename: './workspace/openfds.db' })
await db.loadDatabaseAsync()
await db.removeAsync({}, { multi: true })
await db.insertAsync({ hello: 'world' })
for (let i = 0; i < 2 * N + 1; i++) {
await db.persistence.persistCachedDatabaseAsync()
}
} catch (error) {
console.error(`Got unexpected error during one persistence operation with error: ${error}`)
}
}
try {
test()
} catch (error) {
console.error(error)
process.exit(1)
}