-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
Copy pathtest-abortsignal-drop-settled-signals.mjs
170 lines (137 loc) Β· 4.72 KB
/
test-abortsignal-drop-settled-signals.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Flags: --expose_gc
//
import '../common/index.mjs';
import { gcUntil } from '../common/gc.js';
import { describe, it } from 'node:test';
function makeSubsequentCalls(limit, done, holdReferences = false) {
let dependantSymbol;
let signalRef;
const ac = new AbortController();
const retainedSignals = [];
const handler = () => { };
function run(iteration) {
if (iteration > limit) {
// This setImmediate is necessary to ensure that in the last iteration the remaining signal is GCed (if not
// retained)
setImmediate(() => {
global.gc();
done(ac.signal, dependantSymbol);
});
return;
}
if (holdReferences) {
retainedSignals.push(AbortSignal.any([ac.signal]));
} else {
// Using a WeakRef to avoid retaining information that will interfere with the test
signalRef = new WeakRef(AbortSignal.any([ac.signal]));
signalRef.deref().addEventListener('abort', handler);
}
dependantSymbol ??= Object.getOwnPropertySymbols(ac.signal).find(
(s) => s.toString() === 'Symbol(kDependantSignals)'
);
setImmediate(() => {
// Removing the event listener at some moment in the future
// Which will then allow the signal to be GCed
signalRef?.deref()?.removeEventListener('abort', handler);
run(iteration + 1);
});
}
run(1);
};
function runShortLivedSourceSignal(limit, done) {
const signalRefs = new Set();
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]);
setImmediate(() => run(iteration + 1));
}
run(1);
};
function runWithOrphanListeners(limit, done) {
let composedSignalRef;
const composedSignalRefs = [];
const handler = () => { };
function run(iteration) {
const ac = new AbortController();
if (iteration > limit) {
setImmediate(() => {
global.gc();
setImmediate(() => {
global.gc();
done(composedSignalRefs);
});
});
return;
}
composedSignalRef = new WeakRef(AbortSignal.any([ac.signal]));
composedSignalRef.deref().addEventListener('abort', handler);
const otherComposedSignalRef = new WeakRef(AbortSignal.any([composedSignalRef.deref()]));
otherComposedSignalRef.deref().addEventListener('abort', handler);
composedSignalRefs.push(composedSignalRef, otherComposedSignalRef);
setImmediate(() => {
run(iteration + 1);
});
}
run(1);
}
const limit = 10_000;
describe('when there is a long-lived signal', () => {
it('drops settled dependant signals', (t, done) => {
makeSubsequentCalls(limit, (signal, depandantSignalsKey) => {
setImmediate(() => {
t.assert.strictEqual(signal[depandantSignalsKey].size, 0);
done();
});
});
});
it('keeps all active dependant signals', (t, done) => {
makeSubsequentCalls(limit, (signal, depandantSignalsKey) => {
t.assert.strictEqual(signal[depandantSignalsKey].size, limit);
done();
}, true);
});
});
it('does not prevent source signal from being GCed if it is short-lived', (t, done) => {
runShortLivedSourceSignal(limit, (signalRefs) => {
setImmediate(() => {
const unGCedSignals = [...signalRefs].filter((ref) => ref.deref());
t.assert.strictEqual(unGCedSignals.length, 0);
done();
});
});
});
it('drops settled dependant signals when signal is composite', (t, done) => {
const controllers = Array.from({ length: 2 }, () => new AbortController());
const composedSignal1 = AbortSignal.any([controllers[0].signal]);
const composedSignalRef = new WeakRef(AbortSignal.any([composedSignal1, controllers[1].signal]));
const kDependantSignals = Object.getOwnPropertySymbols(controllers[0].signal).find(
(s) => s.toString() === 'Symbol(kDependantSignals)'
);
setImmediate(() => {
global.gc({ execution: 'async' }).then(() => {
t.assert.strictEqual(composedSignalRef.deref(), undefined);
t.assert.strictEqual(controllers[0].signal[kDependantSignals].size, 2);
t.assert.strictEqual(controllers[1].signal[kDependantSignals].size, 1);
setImmediate(() => {
t.assert.strictEqual(controllers[0].signal[kDependantSignals].size, 0);
t.assert.strictEqual(controllers[1].signal[kDependantSignals].size, 0);
done();
});
});
});
});
it('drops settled signals even when there are listeners', (t, done) => {
runWithOrphanListeners(limit, async (signalRefs) => {
await gcUntil('all signals are GCed', () => {
const unGCedSignals = [...signalRefs].filter((ref) => ref.deref());
return unGCedSignals.length === 0;
});
done();
});
});