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.
  • Loading branch information
BridgeAR committed Sep 26, 2018
1 parent d6a6df9 commit 1ad6994
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 @@ -17,6 +17,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 @@ -82,6 +84,17 @@ function Timeout(callback, after, args, isRepeat) {
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[kRefed])
getTimers().active(this);
Expand Down
11 changes: 11 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ function TimersList(expiry, msecs) {
this.priorityQueuePosition = null;
}

// 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
});
};

const { _tickCallback: runNextTicks } = process;
function processTimers(now) {
debug('process timer lists %d', 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 1ad6994

Please sign in to comment.