mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
chore(test): improve test coverage for tcp-serverAppender
appenders/tcp-server.js - Line 19 - dataSoFar = events.pop(); appenders/tcp-server.js - Line 27 - dataSoFar = '';
This commit is contained in:
parent
65fe707350
commit
64ef06b849
@ -5,21 +5,20 @@ const vcr = require("../../lib/appenders/recording");
|
||||
const levels = require("../../lib/levels");
|
||||
const LoggingEvent = require("../../lib/LoggingEvent");
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
vcr: { type: "recording" },
|
||||
tcp: { type: "tcp-server", port: 5678 }
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ["vcr"], level: "debug" }
|
||||
}
|
||||
});
|
||||
|
||||
// give the socket a chance to start up
|
||||
test("TCP Server", batch => {
|
||||
batch.test(
|
||||
"should listen for TCP messages and re-send via process.send",
|
||||
t => {
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
vcr: { type: "recording" },
|
||||
tcp: { type: "tcp-server", port: 5678 }
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ["vcr"], level: "debug" }
|
||||
}
|
||||
});
|
||||
// give the socket a chance to start up
|
||||
setTimeout(() => {
|
||||
const socket = net.connect(5678, () => {
|
||||
socket.write(
|
||||
@ -83,8 +82,114 @@ test("TCP Server", batch => {
|
||||
|
||||
socket.unref();
|
||||
}, 100);
|
||||
|
||||
batch.end();
|
||||
}
|
||||
);
|
||||
|
||||
batch.test(
|
||||
"sending incomplete messages in chunks",
|
||||
t => {
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
vcr: { type: "recording" },
|
||||
tcp: { type: "tcp-server" }
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ["vcr"], level: "debug" }
|
||||
}
|
||||
});
|
||||
// give the socket a chance to start up
|
||||
setTimeout(() => {
|
||||
const socket = net.connect(5000, () => {
|
||||
const syncWrite = (dataArray = [], finalCallback) => {
|
||||
if (dataArray === null) {
|
||||
dataArray = [];
|
||||
} else if (!Array.isArray(dataArray)) {
|
||||
dataArray = [dataArray];
|
||||
}
|
||||
if (typeof finalCallback !== "function") {
|
||||
finalCallback = () => {};
|
||||
}
|
||||
setTimeout(() => {
|
||||
if (!dataArray.length) {
|
||||
finalCallback();
|
||||
} else if (dataArray.length === 1) {
|
||||
socket.write(dataArray.shift(), finalCallback);
|
||||
} else {
|
||||
socket.write(dataArray.shift(), () => { syncWrite(dataArray, finalCallback); });
|
||||
}
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const dataArray = [
|
||||
"__LOG4JS__",
|
||||
"Hello__LOG4JS__World",
|
||||
"__LOG4JS__",
|
||||
"testing nonsense",
|
||||
`__LOG4JS__more nonsense__LOG4JS__`
|
||||
];
|
||||
|
||||
const finalCallback = () => {
|
||||
socket.end();
|
||||
setTimeout(() => {
|
||||
log4js.shutdown(() => {
|
||||
const logs = vcr.replay();
|
||||
t.equal(logs.length, 8);
|
||||
t.match(logs[4], {
|
||||
data: [
|
||||
"Unable to parse log:",
|
||||
"Hello",
|
||||
"because: ",
|
||||
SyntaxError
|
||||
],
|
||||
categoryName: "log4js",
|
||||
level: { levelStr: "ERROR" },
|
||||
context: {}
|
||||
});
|
||||
t.match(logs[5], {
|
||||
data: [
|
||||
"Unable to parse log:",
|
||||
"World",
|
||||
"because: ",
|
||||
SyntaxError
|
||||
],
|
||||
categoryName: "log4js",
|
||||
level: { levelStr: "ERROR" },
|
||||
context: {}
|
||||
});
|
||||
t.match(logs[6], {
|
||||
data: [
|
||||
"Unable to parse log:",
|
||||
"testing nonsense",
|
||||
"because: ",
|
||||
SyntaxError
|
||||
],
|
||||
categoryName: "log4js",
|
||||
level: { levelStr: "ERROR" },
|
||||
context: {}
|
||||
});
|
||||
t.match(logs[7], {
|
||||
data: [
|
||||
"Unable to parse log:",
|
||||
"more nonsense",
|
||||
"because: ",
|
||||
SyntaxError
|
||||
],
|
||||
categoryName: "log4js",
|
||||
level: { levelStr: "ERROR" },
|
||||
context: {}
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
}, 100);
|
||||
};
|
||||
|
||||
syncWrite(dataArray, finalCallback);
|
||||
});
|
||||
|
||||
socket.unref();
|
||||
}, 100);
|
||||
}
|
||||
);
|
||||
|
||||
batch.end();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user