( fn, maxCount = 4096, generateSnapshotAt = Infinity, logEvery = 128)
| 90 | // other logic in V8 such as bytecode aging, and it can slow down the test |
| 91 | // significantly, so it should be used scarcely and only as a last resort. |
| 92 | async function checkIfCollectable( |
| 93 | fn, maxCount = 4096, generateSnapshotAt = Infinity, logEvery = 128) { |
| 94 | let anyFinalized = false; |
| 95 | let count = 0; |
| 96 | |
| 97 | const f = new FinalizationRegistry(() => { |
| 98 | anyFinalized = true; |
| 99 | }); |
| 100 | |
| 101 | async function createObject() { |
| 102 | const obj = await fn(); |
| 103 | f.register(obj); |
| 104 | if (count++ < maxCount && !anyFinalized) { |
| 105 | setImmediate(createObject, 1); |
| 106 | } |
| 107 | // This can force a more thorough GC, but can slow the test down |
| 108 | // significantly in a big heap. Use it with care. |
| 109 | if (count % generateSnapshotAt === 0) { |
| 110 | // XXX(joyeecheung): This itself can consume a bit of JS heap memory, |
| 111 | // but the other alternative writeHeapSnapshot can run into disk space |
| 112 | // not enough problems in the CI & be slower depending on file system. |
| 113 | // Just do this for now as long as it works and only invent some |
| 114 | // internal voodoo when we absolutely have no other choice. |
| 115 | require('v8').getHeapSnapshot().pause().read(); |
| 116 | console.log(`Generated heap snapshot at ${count}`); |
| 117 | } |
| 118 | if (count % logEvery === 0) { |
| 119 | console.log(`Created ${count} objects`); |
| 120 | } |
| 121 | if (anyFinalized) { |
| 122 | console.log(`Found finalized object at ${count}, stop testing`); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | createObject(); |
| 127 | } |
| 128 | |
| 129 | // Repeat an operation and give GC some breathing room at every iteration. |
| 130 | async function runAndBreathe(fn, repeat, waitTime = 20) { |
no test coverage detected
searching dependent graphs…