0
Files
src/third_party/blink/web_tests/js/weakrefs/finalization-registry-basic.html
Michael Lippautz 6212ed814b web_tests: Fix WeakRef tests reclaiming FinalizationRegistry
The registry is kept local in the closure and may be optimized by V8. In
some testing configuration we do GC between executing the initial
closure and processing the callbacks in the microtask queue. In such
cases the FinalizationRegistry would be prematurely collected and the
callbacks would not be called resulting in a timeout.

In practice this may happen as well when other microtasks that are
enqueued trigger GC.

Bug: chromium:1074061
Change-Id: I0d0b88f5fde757669b51d12ae37d043e68dc4362
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2172429
Reviewed-by: Michael Achenbach <machenbach@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763847}
2020-04-29 16:19:24 +00:00

36 lines
1.0 KiB
HTML

<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script type="module">
// Keep FinalizationRegistry global to avoid it being reclaimed by garbage
// collections that may happen until the registry is processed in microtasks.
let fr;
async_test(t => {
let called = false;
const callback = t.step_func_done(function(holdings) {
called = true;
assert_equals(holdings, 'holdings',
'holdings should be initialized correctly');
});
fr = new FinalizationRegistry(callback);
// The following is an IIFE to ensure that garbage becomes unreachable after
// the function returns.
(function() {
let garbage = {};
fr.register(garbage, 'holdings');
garbage = null;
})();
assert_false(called, 'finalizer should not be called in the same turn');
gc();
assert_false(called, 'finalizer should not be called in the same turn');
}, 'FinalizationRegistry registers an object and calls finalizer');
</script>