Skip to content

Commit

Permalink
make test-gc-and-finalize slightly more devious
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Jun 2, 2021
1 parent b1560a7 commit a3a6149
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/SwingSet/test/test-gc-and-finalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@ test(`have gc() on Node.js`, async t => {
// `gc` C callback on the global object.
});

function setup() {
const victim = { doomed: 'oh no' };
function setup1() {
// The worst case I can think of is a four-pole counter-rotating cycle,
// held in place by N and S, with E as our victim/sensor. Dropping N or S
// does not drop a refcount to zero.
const N = {};
const S = {};
const E = { N, S };
const W = { N, S };
N.E = E;
N.W = W;
S.E = E;
S.W = W;
const finalized = ['finalizer not called'];
const fr = new FinalizationRegistry(_tag => {
finalized[0] = 'finalizer was called';
});
const wr = new WeakRef(victim);
fr.register(victim, 'tag');
const wr = new WeakRef(E);
fr.register(E, 'tag');
return { N, S, finalized, fr, wr };
}

function setup2() {
const { N: _ignoreN, S: _ignoreS, finalized, fr, wr } = setup1();
// I want N and S to be dropped after E and W.
return { finalized, fr, wr };
}

Expand All @@ -32,7 +48,7 @@ async function provokeGC() {

// we must retain the FinalizationRegistry to let the callback fire
// eslint-disable-next-line no-unused-vars
const { finalized, fr, wr } = setup();
const { finalized, fr, wr } = setup2();

// the transition from UNREACHABLE to COLLECTED can happen at any moment,
// but is far more likely to happen if we force it
Expand Down Expand Up @@ -82,7 +98,8 @@ test(`can provoke gc on xsnap`, async t => {
const vat = xsnap(opts);
const code = `
${gcAndFinalize}
${setup}
${setup1}
${setup2}
${provokeGC}
provokeGC().then(data => issueCommand(ArrayBuffer.fromString(JSON.stringify(data))));
`;
Expand Down

0 comments on commit a3a6149

Please sign in to comment.