forked from log4js-node/log4js-node
/
file-descriptor-leak-test.js
80 lines (65 loc) · 2.41 KB
/
file-descriptor-leak-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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { test } = require("tap");
const fs = require("fs");
const path = require("path");
const log4js = require("../../lib/log4js");
const removeFiles = async filenames => {
if (!Array.isArray(filenames))
filenames = [filenames];
const promises = filenames.map(filename => fs.promises.unlink(filename));
await Promise.allSettled(promises);
};
// no file descriptors on Windows, so don't run the tests
if (process.platform !== "win32") {
test("multiple log4js configure fd leak test", batch => {
const config = {
appenders: {},
categories: {
default: { appenders: [], level: 'debug' }
}
};
// create 11 appenders
const numOfAppenders = 11;
for (let i = 1; i <= numOfAppenders; i++) {
config.appenders[`app${i}`] = { type: 'file', filename: path.join(__dirname, `file${i}.log`) };
config.categories.default.appenders.push(`app${i}`);
}
const initialFd = fs.readdirSync('/proc/self/fd').length;
let loadedFd;
batch.test("initial log4js configure to increase file descriptor count", t => {
log4js.configure(config);
// wait for the file system to catch up
setTimeout(() => {
loadedFd = fs.readdirSync('/proc/self/fd').length;
t.equal(loadedFd, initialFd + numOfAppenders,
`file descriptor count should increase by ${numOfAppenders} after 1st configure() call`);
t.end();
}, 250);
});
batch.test("repeated log4js configure to not increase file descriptor count", t => {
log4js.configure(config);
log4js.configure(config);
log4js.configure(config);
// wait for the file system to catch up
setTimeout(() => {
t.equal(fs.readdirSync('/proc/self/fd').length, loadedFd,
`file descriptor count should be identical after repeated configure() calls`);
t.end();
}, 250);
});
batch.test("file descriptor count should return back to initial count", t => {
log4js.shutdown();
// wait for the file system to catch up
setTimeout(() => {
t.equal(fs.readdirSync('/proc/self/fd').length, initialFd,
`file descriptor count should be back to initial`);
t.end();
}, 250);
});
batch.teardown(async () => {
log4js.shutdown();
const filenames = Object.values(config.appenders).map(appender => appender.filename);
await removeFiles(filenames);
});
batch.end();
});
}