Skip to content

Commit

Permalink
test: ensure short-lived signals are GCed
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Oct 11, 2024
1 parent 08f548d commit 7fde7c9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/parallel/test-abortsignal-drop-settled-signals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ const makeSubsequentCalls = (limit, done, holdReferences = false) => {
run(1);
};

const runShortLivedSourceSignal = (limit, done) => {
const signalRefs = new Set();
let dependantSymbol;

function run(iteration) {
if (iteration > limit) {
global.gc();
done(signalRefs);
return;
}

const ac = new AbortController();
signalRefs.add(new WeakRef(ac.signal));
AbortSignal.any([ac.signal]);

if (!dependantSymbol) {
const kDependantSignals = Object.getOwnPropertySymbols(ac.signal).find(
(s) => s.toString() === 'Symbol(kDependantSignals)'
);
dependantSymbol = kDependantSignals;
}

setImmediate(() => run(iteration + 1));
}

run(1);
};

const limit = 10_000;

describe('when there is a long-lived signal', () => {
Expand All @@ -59,3 +87,16 @@ describe('when there is a long-lived signal', () => {
}, true);
});
});

describe('when there is a short-lived signal', () => {
it('does not prevent source signal from being GCed', (t, done) => {
runShortLivedSourceSignal(limit, (signalRefs) => {
setImmediate(() => {
const unGCedSignals = [...signalRefs].filter((ref) => ref.deref());

t.assert.equal(unGCedSignals, 0);
done();
});
});
});
});

0 comments on commit 7fde7c9

Please sign in to comment.