Skip to content

Commit

Permalink
timers: use custom inspection for linked lists
Browse files Browse the repository at this point in the history
Inspecting linked lists is something that is not really useful.
Instead, just use a custom inspection function and hide everything
besides the first level.

PR-URL: #23108
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Oct 3, 2018
1 parent a2c1ce2 commit 4436a3d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const {
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');

const { inspect } = require('util');

// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2 ** 31 - 1;

Expand Down Expand Up @@ -80,6 +82,17 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
initAsyncResource(this, 'Timeout');
}

// Make sure the linked list only shows the minimal necessary information.
Timeout.prototype[inspect.custom] = function(_, options) {
return inspect(this, {
...options,
// Only inspect one level.
depth: 0,
// It should not recurse.
customInspect: false
});
};

Timeout.prototype.refresh = function() {
if (this._handle) {
// Would be more ideal with uv_timer_again(), however that API does not
Expand Down
11 changes: 11 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ function TimersList(msecs, unrefed) {
timer.start(msecs);
}

// Make sure the linked list only shows the minimal necessary information.
TimersList.prototype[util.inspect.custom] = function(_, options) {
return util.inspect(this, {
...options,
// Only inspect one level.
depth: 0,
// It should not recurse.
customInspect: false
});
};

function processTimers(now) {
if (this[owner_symbol])
return unrefdHandle(this[owner_symbol], now);
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-http2-socket-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (!common.hasCrypto)
const assert = require('assert');
const h2 = require('http2');
const net = require('net');
const util = require('util');

const { kTimeout } = require('internal/timers');

Expand Down Expand Up @@ -35,6 +36,22 @@ server.on('stream', common.mustCall(function(stream, headers) {
socket.setTimeout(987);
assert.strictEqual(session[kTimeout]._idleTimeout, 987);

// The indentation is corrected depending on the depth.
let inspectedTimeout = util.inspect(session[kTimeout]);
assert(inspectedTimeout.includes(' _idlePrev: [TimersList]'));
assert(inspectedTimeout.includes(' _idleNext: [TimersList]'));
assert(!inspectedTimeout.includes(' _idleNext: [TimersList]'));

inspectedTimeout = util.inspect([ session[kTimeout] ]);
assert(inspectedTimeout.includes(' _idlePrev: [TimersList]'));
assert(inspectedTimeout.includes(' _idleNext: [TimersList]'));
assert(!inspectedTimeout.includes(' _idleNext: [TimersList]'));

const inspectedTimersList = util.inspect([[ session[kTimeout]._idlePrev ]]);
assert(inspectedTimersList.includes(' _idlePrev: [Timeout]'));
assert(inspectedTimersList.includes(' _idleNext: [Timeout]'));
assert(!inspectedTimersList.includes(' _idleNext: [Timeout]'));

common.expectsError(() => socket.destroy, errMsg);
common.expectsError(() => socket.emit, errMsg);
common.expectsError(() => socket.end, errMsg);
Expand Down

0 comments on commit 4436a3d

Please sign in to comment.