Skip to content

Commit 7cb6a39

Browse files
committed
Add more GC tests
1 parent 9447c4a commit 7cb6a39

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

test/van.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,18 @@ const runTests = async (van, msgDom, { debug }) => {
11691169
// We want to test the garbage-collection process is in place to ensure obsolete bindings and
11701170
// derivations can be cleaned up.
11711171
const gcTests = {
1172-
long_derivedDom: withHiddenDom(async (hiddenDom) => {
1172+
bindingBasic: withHiddenDom(async (hiddenDom) => {
1173+
const counter = van.state(0);
1174+
const bindingsPropKey = Object.entries(counter)
1175+
.find(([_, v]) => Array.isArray(v))[0];
1176+
van.add(hiddenDom, () => span(`Counter: ${counter.val}`));
1177+
for (let i = 0; i < 100; ++i)
1178+
++counter.val;
1179+
await sleep(waitMsOnDomUpdates);
1180+
assertEq(hiddenDom.innerHTML, "<span>Counter: 100</span>");
1181+
assertBetween(counter[bindingsPropKey].length, 1, 3);
1182+
}),
1183+
long_nestedBinding: withHiddenDom(async (hiddenDom) => {
11731184
const renderPre = van.state(false);
11741185
const text = van.state("Text");
11751186
const bindingsPropKey = Object.entries(renderPre)
@@ -1185,7 +1196,7 @@ const runTests = async (van, msgDom, { debug }) => {
11851196
assertBetween(renderPre[bindingsPropKey].length, 1, 3);
11861197
assertBetween(text[bindingsPropKey].length, 1, 3);
11871198
}),
1188-
long_conditionalDomFunc: withHiddenDom(async (hiddenDom) => {
1199+
long_conditionalBinding: withHiddenDom(async (hiddenDom) => {
11891200
const cond = van.state(true);
11901201
const a = van.state(0), b = van.state(0), c = van.state(0), d = van.state(0);
11911202
const bindingsPropKey = Object.entries(cond)
@@ -1206,7 +1217,7 @@ const runTests = async (van, msgDom, { debug }) => {
12061217
await sleep(1000);
12071218
allStates.every(s => assertBetween(s[bindingsPropKey].length, 1, 3));
12081219
}),
1209-
long_deriveBasic: async () => {
1220+
deriveBasic: () => {
12101221
const history = [];
12111222
const a = van.state(0);
12121223
const listenersPropKey = Object.entries(a)
@@ -1215,8 +1226,6 @@ const runTests = async (van, msgDom, { debug }) => {
12151226
for (let i = 0; i < 100; ++i)
12161227
++a.val;
12171228
assertEq(history.length, 101);
1218-
// Wait until GC kicks in
1219-
await sleep(1000);
12201229
assertBetween(a[listenersPropKey].length, 1, 3);
12211230
},
12221231
long_deriveInBindingFunc: withHiddenDom(async (hiddenDom) => {

test/van.test.nomodule.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,17 @@
11051105
})
11061106
};
11071107
const gcTests = {
1108-
long_derivedDom: withHiddenDom(async (hiddenDom) => {
1108+
bindingBasic: withHiddenDom(async (hiddenDom) => {
1109+
const counter = van2.state(0);
1110+
const bindingsPropKey = Object.entries(counter).find(([_, v]) => Array.isArray(v))[0];
1111+
van2.add(hiddenDom, () => span(`Counter: ${counter.val}`));
1112+
for (let i = 0; i < 100; ++i)
1113+
++counter.val;
1114+
await sleep(waitMsOnDomUpdates);
1115+
assertEq(hiddenDom.innerHTML, "<span>Counter: 100</span>");
1116+
assertBetween(counter[bindingsPropKey].length, 1, 3);
1117+
}),
1118+
long_nestedBinding: withHiddenDom(async (hiddenDom) => {
11091119
const renderPre = van2.state(false);
11101120
const text = van2.state("Text");
11111121
const bindingsPropKey = Object.entries(renderPre).find(([_, v]) => Array.isArray(v))[0];
@@ -1119,7 +1129,7 @@
11191129
assertBetween(renderPre[bindingsPropKey].length, 1, 3);
11201130
assertBetween(text[bindingsPropKey].length, 1, 3);
11211131
}),
1122-
long_conditionalDomFunc: withHiddenDom(async (hiddenDom) => {
1132+
long_conditionalBinding: withHiddenDom(async (hiddenDom) => {
11231133
const cond = van2.state(true);
11241134
const a2 = van2.state(0), b2 = van2.state(0), c = van2.state(0), d = van2.state(0);
11251135
const bindingsPropKey = Object.entries(cond).find(([_, v]) => Array.isArray(v))[0];
@@ -1138,15 +1148,14 @@
11381148
await sleep(1e3);
11391149
allStates.every((s) => assertBetween(s[bindingsPropKey].length, 1, 3));
11401150
}),
1141-
long_deriveBasic: async () => {
1151+
deriveBasic: () => {
11421152
const history = [];
11431153
const a2 = van2.state(0);
11441154
const listenersPropKey = Object.entries(a2).filter(([_, v]) => Array.isArray(v))[1][0];
11451155
van2.derive(() => history.push(a2.val));
11461156
for (let i = 0; i < 100; ++i)
11471157
++a2.val;
11481158
assertEq(history.length, 101);
1149-
await sleep(1e3);
11501159
assertBetween(a2[listenersPropKey].length, 1, 3);
11511160
},
11521161
long_deriveInBindingFunc: withHiddenDom(async (hiddenDom) => {

test/van.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,21 @@ const runTests = async (van: VanForTesting, msgDom: Element, {debug}: BundleOpti
15571557
// We want to test the garbage-collection process is in place to ensure obsolete bindings and
15581558
// derivations can be cleaned up.
15591559
const gcTests = {
1560-
long_derivedDom: withHiddenDom(async hiddenDom => {
1560+
bindingBasic: withHiddenDom(async hiddenDom => {
1561+
const counter = van.state(0)
1562+
const bindingsPropKey = Object.entries(counter)
1563+
.find(([_, v]) => Array.isArray(v))![0]
1564+
1565+
van.add(hiddenDom, () => span(`Counter: ${counter.val}`))
1566+
1567+
for (let i = 0; i < 100; ++i) ++counter.val
1568+
await sleep(waitMsOnDomUpdates)
1569+
1570+
assertEq(hiddenDom.innerHTML, "<span>Counter: 100</span>")
1571+
assertBetween(counter[bindingsPropKey].length, 1, 3)
1572+
}),
1573+
1574+
long_nestedBinding: withHiddenDom(async hiddenDom => {
15611575
const renderPre = van.state(false)
15621576
const text = van.state("Text")
15631577
const bindingsPropKey = Object.entries(renderPre)
@@ -1577,7 +1591,7 @@ const runTests = async (van: VanForTesting, msgDom: Element, {debug}: BundleOpti
15771591
assertBetween(text[bindingsPropKey].length, 1, 3)
15781592
}),
15791593

1580-
long_conditionalDomFunc: withHiddenDom(async hiddenDom => {
1594+
long_conditionalBinding: withHiddenDom(async hiddenDom => {
15811595
const cond = van.state(true)
15821596
const a = van.state(0), b = van.state(0), c = van.state(0), d = van.state(0)
15831597
const bindingsPropKey = Object.entries(cond)
@@ -1600,7 +1614,7 @@ const runTests = async (van: VanForTesting, msgDom: Element, {debug}: BundleOpti
16001614
allStates.every(s => assertBetween(s[bindingsPropKey].length, 1, 3))
16011615
}),
16021616

1603-
long_deriveBasic: async () => {
1617+
deriveBasic: () => {
16041618
const history: any[] = []
16051619
const a = van.state(0)
16061620
const listenersPropKey = Object.entries(a)
@@ -1611,9 +1625,6 @@ const runTests = async (van: VanForTesting, msgDom: Element, {debug}: BundleOpti
16111625
for (let i = 0; i < 100; ++i) ++a.val
16121626

16131627
assertEq(history.length, 101)
1614-
1615-
// Wait until GC kicks in
1616-
await sleep(1000)
16171628
assertBetween(a[listenersPropKey].length, 1, 3)
16181629
},
16191630

0 commit comments

Comments
 (0)