Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6609e55

Browse files
MaxAakerobsdedude
andauthored
TestKit backend: truncate logging to avoid clogging ram (#1330)
Writing to stdout in nodejs is done asynchronously. So when adding data to the queue faster than it can be flushed, the process will eventually crash because the system is running out of memory. By limiting the length of log messages, this problem can be avoided. Co-authored-by: Robsdedude <[email protected]>
1 parent 5691dec commit 6609e55

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

packages/testkit-backend/deno/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import channel from "./channel.ts";
1111
import controller from "./controller.ts";
1212
import { RequestHandlerMap } from "./domain.ts";
13+
import util from "node:util";
1314

1415
const requestHandlers: RequestHandlerMap = handlers as RequestHandlerMap;
1516

@@ -26,11 +27,23 @@ const binder = new CypherNativeBinders(neo4j);
2627
const descriptor = ["async", "deno"];
2728
const shouldRunTest = getShouldRunTest(descriptor);
2829
const getFeatures = createGetFeatures(descriptor);
29-
const logLevel = Deno.env.get("TEST_LOG_LEVEL");
30+
const logLevel = Deno.env.get("TEST_LOG_LEVEL") ?? "info";
3031
const createContext = () =>
3132
new Context(shouldRunTest, getFeatures, binder, logLevel);
3233

33-
configurableConsole.install(logLevel);
34+
const logWrapper = (...args) => {
35+
let output = args.map((arg) => {
36+
if (typeof arg === "string") return arg;
37+
return util.inspect(arg, { colors: Deno.stdout.isTTY });
38+
}).join(" ") + "\n";
39+
if (output.length > 1000 * 2 + 3) {
40+
output = output.substring(0, 1000) + "..." +
41+
output.substring(output.length - 1000);
42+
}
43+
Deno.stdout.write(new TextEncoder().encode(output));
44+
};
45+
46+
configurableConsole.install(logLevel, logWrapper);
3447
const listener = channel.listen(9876);
3548
const handle = controller.createHandler(neo4j, createContext, requestHandlers);
3649

packages/testkit-backend/src/channel/testkit-protocol.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ export default class Protocol extends EventEmitter {
6161
}
6262

6363
serializeResponse (response) {
64+
const { name, data } = response
6465
const responseStr = stringify(response)
65-
console.log('> writing response', responseStr)
66+
console.log('> writing response' + name)
67+
console.debug(data)
6668
return ['#response begin', responseStr, '#response end'].join('\n') + '\n'
6769
}
6870

packages/testkit-backend/src/console.configurable.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ const config = {
2020
}
2121

2222
export default {
23-
install (level = 'info') {
23+
install (level = 'info', logWrapper) {
2424
this.setLevel(level)
2525
// eslint-disable-next-line no-global-assign
2626
console = new Proxy({}, {
2727
get: (_, method) => (...args) => {
2828
if (config.canRun(method)) {
29-
originalConsole[method].apply(originalConsole, args)
29+
if (logWrapper != null) {
30+
logWrapper(...args)
31+
} else {
32+
originalConsole[method].apply(originalConsole, args)
33+
}
3034
}
3135
}
3236
})

packages/testkit-backend/src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as REQUEST_HANDLERS from './request-handlers.js'
99
import * as RX_REQUEST_HANDLERS from './request-handlers-rx.js'
1010
import remoteConsole from './console.remote.js'
1111
import configurableConsole from './console.configurable.js'
12+
import util from 'util'
1213

1314
const SUPPORTED_TLS = (() => {
1415
if (tls.DEFAULT_MAX_VERSION) {
@@ -40,7 +41,17 @@ function main () {
4041
const shouldRunTest = getShouldRunTest([...driverDescriptorList, sessionTypeDescriptor])
4142
const getFeatures = createGetFeatures([sessionTypeDescriptor], SUPPORTED_TLS)
4243

43-
configurableConsole.install(process.env.TEST_LOG_LEVEL || 'info')
44+
const logWrapper = (...args) => {
45+
let output = args.map(arg => {
46+
if (typeof arg === 'string') return arg
47+
return util.inspect(arg, { colors: process.stdout.isTTY })
48+
}).join(' ') + '\n'
49+
if (output.length > 1000 * 2 + 3) {
50+
output = output.substring(0, 1000) + '...' + output.substring(output.length - 1000)
51+
}
52+
process.stdout.write(output)
53+
}
54+
configurableConsole.install(process.env.TEST_LOG_LEVEL || 'info', logWrapper)
4455

4556
const newChannel = () => {
4657
if (channelType.toUpperCase() === 'WEBSOCKET') {

0 commit comments

Comments
 (0)