Skip to content

Commit

Permalink
chore(test): updated multiFileAppender tests' teardown() to reinstate…
Browse files Browse the repository at this point in the history
… debug and process.stderr.write
  • Loading branch information
lamweili committed Mar 15, 2022
1 parent 7534f9c commit 63b8f53
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions test/tap/multi-file-appender-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ test("multiFile appender", batch => {
);

batch.test("should close file after timeout", t => {
t.teardown(async () => {
await new Promise(resolve => { log4js.shutdown(resolve); });
await removeFiles("logs/C.log");
});
/* checking that the file is closed after a timeout is done by looking at the debug logs
since detecting file locks with node.js is platform specific.
*/
Expand All @@ -90,7 +86,16 @@ test("multiFile appender", batch => {
originalWrite.apply(process.stderr, [string, encoding, fd]);
}
};
debug.enable("log4js:multiFile");
const originalNamespace = debug.disable();
debug.enable(`${originalNamespace}, log4js:multiFile`);

t.teardown(async () => {
await new Promise(resolve => { log4js.shutdown(resolve); });
await removeFiles("logs/C.log");
process.stderr.write = originalWrite;
debug.enable(originalNamespace);
});

const timeoutMs = 25;
log4js.configure({
appenders: {
Expand All @@ -113,19 +118,11 @@ test("multiFile appender", batch => {
`C not used for > ${timeoutMs} ms => close`,
"(timeout1) should have closed"
);
if (!debugWasEnabled) {
debug.disable("log4js:multiFile");
}
process.stderr.write = originalWrite;
t.end();
}, timeoutMs*1 + 50); // add a 50 ms delay
});

batch.test("should close file safely after timeout", t => {
t.teardown(async () => {
await new Promise(resolve => { sandboxedLog4js.shutdown(resolve); }); // eslint-disable-line no-use-before-define
await removeFiles("logs/C.log");
});
const error = new Error("fileAppender shutdown error");
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
requires: {
Expand Down Expand Up @@ -157,7 +154,16 @@ test("multiFile appender", batch => {
originalWrite.apply(process.stderr, [string, encoding, fd]);
}
};
debug.enable("log4js:multiFile");
const originalNamespace = debug.disable();
debug.enable(`${originalNamespace}, log4js:multiFile`);

t.teardown(async () => {
await new Promise(resolve => { sandboxedLog4js.shutdown(resolve); });
await removeFiles("logs/C.log");
process.stderr.write = originalWrite;
debug.enable(originalNamespace);
});

const timeoutMs = 25;
sandboxedLog4js.configure({
appenders: {
Expand Down Expand Up @@ -185,19 +191,11 @@ test("multiFile appender", batch => {
`ignore error on file shutdown: ${error.message}`,
"safely shutdown"
);
if (!debugWasEnabled) {
debug.disable("log4js:multiFile");
}
process.stderr.write = originalWrite;
t.end();
}, timeoutMs*1 + 50); // add a 50 ms delay
});

batch.test("should close file after extended timeout", t => {
t.teardown(async () => {
await new Promise(resolve => { log4js.shutdown(resolve); });
await removeFiles("logs/D.log");
});
/* checking that the file is closed after a timeout is done by looking at the debug logs
since detecting file locks with node.js is platform specific.
*/
Expand All @@ -210,7 +208,16 @@ test("multiFile appender", batch => {
originalWrite.apply(process.stderr, [string, encoding, fd]);
}
};
debug.enable("log4js:multiFile");
const originalNamespace = debug.disable();
debug.enable(`${originalNamespace}, log4js:multiFile`);

t.teardown(async () => {
await new Promise(resolve => { log4js.shutdown(resolve); });
await removeFiles("logs/D.log");
process.stderr.write = originalWrite;
debug.enable(originalNamespace);
});

const timeoutMs = 100;
log4js.configure({
appenders: {
Expand Down Expand Up @@ -247,18 +254,11 @@ test("multiFile appender", batch => {
`D not used for > ${timeoutMs} ms => close`,
"(timeout2) should have closed"
);
if (!debugWasEnabled) {
debug.disable("log4js:multiFile");
}
process.stderr.write = originalWrite;
t.end();
}, timeoutMs*2 + 50); // add a 50 ms delay
});

batch.test("should clear interval for active timers on shutdown", t => {
t.teardown(async () => {
await removeFiles("logs/D.log");
});
/* checking that the file is closed after a timeout is done by looking at the debug logs
since detecting file locks with node.js is platform specific.
*/
Expand All @@ -271,7 +271,15 @@ test("multiFile appender", batch => {
originalWrite.apply(process.stderr, [string, encoding, fd]);
}
};
debug.enable("log4js:multiFile");
const originalNamespace = debug.disable();
debug.enable(`${originalNamespace}, log4js:multiFile`);

t.teardown(async () => {
await removeFiles("logs/D.log");
process.stderr.write = originalWrite;
debug.enable(originalNamespace);
});

const timeoutMs = 100;
log4js.configure({
appenders: {
Expand Down Expand Up @@ -302,10 +310,6 @@ test("multiFile appender", batch => {
"calling shutdown for D",
"should have called shutdown"
);
if (!debugWasEnabled) {
debug.disable("log4js:multiFile");
}
process.stderr.write = originalWrite;
t.end();
});
});
Expand Down

0 comments on commit 63b8f53

Please sign in to comment.