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. PR-URL: https://github.com/nodejs/node/pull/27482 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
f8cf9f96dd
commit
caf9d3c1d0
@ -1614,7 +1614,7 @@ util.inspect(process);
|
|||||||
assert.strict.equal(out, expected);
|
assert.strict.equal(out, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
{ // Test WeakMap
|
{ // Test WeakMap && WeakSet
|
||||||
const obj = {};
|
const obj = {};
|
||||||
const arr = [];
|
const arr = [];
|
||||||
const weakMap = new WeakMap([[obj, arr], [arr, obj]]);
|
const weakMap = new WeakMap([[obj, arr], [arr, obj]]);
|
||||||
@ -1634,16 +1634,16 @@ util.inspect(process);
|
|||||||
out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true });
|
out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true });
|
||||||
// It is not possible to determine the output reliable.
|
// It is not possible to determine the output reliable.
|
||||||
expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }';
|
expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }';
|
||||||
const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
|
let expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' +
|
||||||
'extra: true }';
|
'extra: true }';
|
||||||
assert(out === expect || out === expectAlt,
|
assert(out === expect || out === expectAlt,
|
||||||
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
|
`Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`);
|
||||||
}
|
|
||||||
|
|
||||||
{ // Test WeakSet
|
// Test WeakSet
|
||||||
const weakSet = new WeakSet([{}, [1]]);
|
arr.push(1);
|
||||||
let out = util.inspect(weakSet, { showHidden: true });
|
const weakSet = new WeakSet([obj, arr]);
|
||||||
let expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
|
out = util.inspect(weakSet, { showHidden: true });
|
||||||
|
expect = 'WeakSet { [ 1, [length]: 1 ], {} }';
|
||||||
assert.strictEqual(out, expect);
|
assert.strictEqual(out, expect);
|
||||||
|
|
||||||
out = util.inspect(weakSet);
|
out = util.inspect(weakSet);
|
||||||
@ -1658,10 +1658,12 @@ util.inspect(process);
|
|||||||
out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true });
|
out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true });
|
||||||
// It is not possible to determine the output reliable.
|
// It is not possible to determine the output reliable.
|
||||||
expect = 'WeakSet { {}, ... 1 more item, extra: true }';
|
expect = 'WeakSet { {}, ... 1 more item, extra: true }';
|
||||||
const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, ' +
|
expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, extra: true }';
|
||||||
'extra: true }';
|
|
||||||
assert(out === expect || out === expectAlt,
|
assert(out === expect || out === expectAlt,
|
||||||
`Found: "${out}"\nrather than: "${expect}"\nor: "${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.
|
{ // Test argument objects.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user