Skip to content

Commit

Permalink
test: make sure weak references are not GCed too early
Browse files Browse the repository at this point in the history
This fixes flaky `util.inspect` tests by making sure that the
inspected WeakMap and WeakSet only contains objects that still have
other hard references. Otherwise the garbage collection could collect
these objects to early.

PR-URL: #27482
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed May 4, 2019
1 parent aa281d2 commit a278814
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ util.inspect(process);
assert.strict.equal(out, expected);
}

{ // Test WeakMap
{ // Test WeakMap && WeakSet
const obj = {};
const arr = [];
const weakMap = new WeakMap([[obj, arr], [arr, obj]]);
Expand All @@ -1634,16 +1634,16 @@ util.inspect(process);
out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true });
// It is not possible to determine the output reliable.
expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }';
const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
'extra: true }';
let expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
'extra: true }';
assert(out === expect || out === expectAlt,
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
}

{ // Test WeakSet
const weakSet = new WeakSet([{}, [1]]);
let out = util.inspect(weakSet, { showHidden: true });
let expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
// Test WeakSet
arr.push(1);
const weakSet = new WeakSet([obj, arr]);
out = util.inspect(weakSet, { showHidden: true });
expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
assert.strictEqual(out, expect);

out = util.inspect(weakSet);
Expand All @@ -1658,10 +1658,12 @@ util.inspect(process);
out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true });
// It is not possible to determine the output reliable.
expect = 'WeakSet { {}, ... 1 more item, extra: true }';
const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, ' +
'extra: true }';
expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, extra: true }';
assert(out === expect || out === expectAlt,
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
// Keep references to the WeakMap entries, otherwise they could be GCed too
// early.
assert(obj && arr);
}

{ // Test argument objects.
Expand Down

0 comments on commit a278814

Please sign in to comment.