-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
test-util.mjs
86 lines (73 loc) · 2.22 KB
/
test-util.mjs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { setTimeout } from 'timers'
import { promisify } from 'util'
import chalk from 'chalk'
import Dockerode from 'dockerode'
import prettyBytes from 'pretty-bytes'
const sleep = promisify(setTimeout)
const client = new Dockerode()
export async function createSuite(testFile, maxMemoryUsage) {
console.info(chalk.cyanBright(`Create container to test ${testFile}`))
const container = await client.createContainer({
Image: 'node:lts-slim',
Cmd: ['/bin/bash', '-c', `node --expose-gc memory-testing/${testFile}.mjs`],
AttachStdout: true,
AttachStderr: true,
Tty: true,
WorkingDir: '/napi-rs',
Env: ['MAX_OLD_SPACE_SIZE=256', 'FORCE_COLOR=1'],
HostConfig: {
Binds: [`${process.cwd()}:/napi-rs:rw`],
Memory: 256 * 1024 * 1024,
},
})
console.info(chalk.cyanBright('Container created, starting ...'))
await container.start()
container.attach(
{ stream: true, stdout: true, stderr: true },
function (err, stream) {
if (err) {
console.error(err)
process.exit(1)
}
stream.pipe(process.stdout)
},
)
const stats = await container.stats()
let shouldAssertMemoryUsage = false
let initialMemoryUsage
await new Promise((resolve, reject) => {
const initialDate = Date.now()
stats.on('data', (d) => {
const { memory_stats } = JSON.parse(d.toString('utf8'))
if (Date.now() - initialDate > 10000 && !shouldAssertMemoryUsage) {
resolve()
initialMemoryUsage = memory_stats.usage
shouldAssertMemoryUsage = true
}
if (shouldAssertMemoryUsage && memory_stats?.usage) {
const memoryGrowth = memory_stats.usage - initialMemoryUsage
if (memoryGrowth > (maxMemoryUsage ?? initialMemoryUsage)) {
console.info(
chalk.redBright(
`Potential memory leak, memory growth: ${prettyBytes(
memoryGrowth,
)}`,
),
)
process.exit(1)
}
}
})
stats.on('error', reject)
})
console.info(
chalk.red(`Initial memory usage: ${prettyBytes(initialMemoryUsage)}`),
)
await sleep(60000)
try {
await container.stop()
await container.remove()
} catch (e) {
console.error(e)
}
}