From a278814818245027606e30a9d2d66ca5b4be2b80 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 29 Apr 2019 21:03:19 +0200 Subject: [PATCH] test: make sure weak references are not GCed too early MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Rich Trott Reviewed-By: Anna Henningsen --- test/parallel/test-util-inspect.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 5ac270f5f84e9a..5c54654a73ce58 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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]]); @@ -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); @@ -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.