Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: make sure weak references are not GCed too early
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.
  • Loading branch information
BridgeAR committed Apr 30, 2019
commit 8cc10b07b184a1297948c199d0a916169bdd5b71
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 to
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
// early.
assert(obj && arr);
}

{ // Test argument objects.
Expand Down