|
| 1 | +"use strict"; |
| 2 | +// Flags: --expose-gc |
| 3 | + |
| 4 | +const test_reference = loadAddon("test_reference"); |
| 5 | + |
| 6 | +// This test script uses external values with finalizer callbacks |
| 7 | +// in order to track when values get garbage-collected. Each invocation |
| 8 | +// of a finalizer callback increments the finalizeCount property. |
| 9 | +assert.strictEqual(test_reference.finalizeCount, 0); |
| 10 | + |
| 11 | +// Run each test function in sequence, |
| 12 | +// with an async delay and GC call between each. |
| 13 | +async function runTests() { |
| 14 | + (() => { |
| 15 | + const symbol = test_reference.createSymbol("testSym"); |
| 16 | + test_reference.createReference(symbol, 0); |
| 17 | + assert.strictEqual(test_reference.referenceValue, symbol); |
| 18 | + })(); |
| 19 | + test_reference.deleteReference(); |
| 20 | + |
| 21 | + (() => { |
| 22 | + const symbol = test_reference.createSymbolFor("testSymFor"); |
| 23 | + test_reference.createReference(symbol, 0); |
| 24 | + assert.strictEqual(test_reference.referenceValue, symbol); |
| 25 | + })(); |
| 26 | + test_reference.deleteReference(); |
| 27 | + |
| 28 | + (() => { |
| 29 | + const symbol = test_reference.createSymbolFor("testSymFor"); |
| 30 | + test_reference.createReference(symbol, 1); |
| 31 | + assert.strictEqual(test_reference.referenceValue, symbol); |
| 32 | + assert.strictEqual(test_reference.referenceValue, Symbol.for("testSymFor")); |
| 33 | + })(); |
| 34 | + test_reference.deleteReference(); |
| 35 | + |
| 36 | + (() => { |
| 37 | + const symbol = test_reference.createSymbolForEmptyString(); |
| 38 | + test_reference.createReference(symbol, 0); |
| 39 | + assert.strictEqual(test_reference.referenceValue, Symbol.for("")); |
| 40 | + })(); |
| 41 | + test_reference.deleteReference(); |
| 42 | + |
| 43 | + (() => { |
| 44 | + const symbol = test_reference.createSymbolForEmptyString(); |
| 45 | + test_reference.createReference(symbol, 1); |
| 46 | + assert.strictEqual(test_reference.referenceValue, symbol); |
| 47 | + assert.strictEqual(test_reference.referenceValue, Symbol.for("")); |
| 48 | + })(); |
| 49 | + test_reference.deleteReference(); |
| 50 | + |
| 51 | + assert.throws( |
| 52 | + () => test_reference.createSymbolForIncorrectLength(), |
| 53 | + /Invalid argument/, |
| 54 | + ); |
| 55 | + |
| 56 | + (() => { |
| 57 | + const value = test_reference.createExternal(); |
| 58 | + assert.strictEqual(test_reference.finalizeCount, 0); |
| 59 | + assert.strictEqual(typeof value, "object"); |
| 60 | + test_reference.checkExternal(value); |
| 61 | + })(); |
| 62 | + await gcUntil( |
| 63 | + "External value without a finalizer", |
| 64 | + () => test_reference.finalizeCount === 0, |
| 65 | + ); |
| 66 | + |
| 67 | + (() => { |
| 68 | + const value = test_reference.createExternalWithFinalize(); |
| 69 | + assert.strictEqual(test_reference.finalizeCount, 0); |
| 70 | + assert.strictEqual(typeof value, "object"); |
| 71 | + test_reference.checkExternal(value); |
| 72 | + })(); |
| 73 | + await gcUntil( |
| 74 | + "External value with a finalizer", |
| 75 | + () => test_reference.finalizeCount === 1, |
| 76 | + ); |
| 77 | + |
| 78 | + (() => { |
| 79 | + const value = test_reference.createExternalWithFinalize(); |
| 80 | + assert.strictEqual(test_reference.finalizeCount, 0); |
| 81 | + test_reference.createReference(value, 0); |
| 82 | + assert.strictEqual(test_reference.referenceValue, value); |
| 83 | + })(); |
| 84 | + // Value should be GC'd because there is only a weak ref |
| 85 | + await gcUntil( |
| 86 | + "Weak reference", |
| 87 | + () => |
| 88 | + test_reference.referenceValue === undefined && |
| 89 | + test_reference.finalizeCount === 1, |
| 90 | + ); |
| 91 | + test_reference.deleteReference(); |
| 92 | + |
| 93 | + (() => { |
| 94 | + const value = test_reference.createExternalWithFinalize(); |
| 95 | + assert.strictEqual(test_reference.finalizeCount, 0); |
| 96 | + test_reference.createReference(value, 1); |
| 97 | + assert.strictEqual(test_reference.referenceValue, value); |
| 98 | + })(); |
| 99 | + // Value should NOT be GC'd because there is a strong ref |
| 100 | + await gcUntil("Strong reference", () => test_reference.finalizeCount === 0); |
| 101 | + test_reference.deleteReference(); |
| 102 | + await gcUntil( |
| 103 | + "Strong reference (cont.d)", |
| 104 | + () => test_reference.finalizeCount === 1, |
| 105 | + ); |
| 106 | + |
| 107 | + (() => { |
| 108 | + const value = test_reference.createExternalWithFinalize(); |
| 109 | + assert.strictEqual(test_reference.finalizeCount, 0); |
| 110 | + test_reference.createReference(value, 1); |
| 111 | + })(); |
| 112 | + // Value should NOT be GC'd because there is a strong ref |
| 113 | + await gcUntil( |
| 114 | + "Strong reference, increment then decrement to weak reference", |
| 115 | + () => test_reference.finalizeCount === 0, |
| 116 | + ); |
| 117 | + assert.strictEqual(test_reference.incrementRefcount(), 2); |
| 118 | + // Value should NOT be GC'd because there is a strong ref |
| 119 | + await gcUntil( |
| 120 | + "Strong reference, increment then decrement to weak reference (cont.d-1)", |
| 121 | + () => test_reference.finalizeCount === 0, |
| 122 | + ); |
| 123 | + assert.strictEqual(test_reference.decrementRefcount(), 1); |
| 124 | + // Value should NOT be GC'd because there is a strong ref |
| 125 | + await gcUntil( |
| 126 | + "Strong reference, increment then decrement to weak reference (cont.d-2)", |
| 127 | + () => test_reference.finalizeCount === 0, |
| 128 | + ); |
| 129 | + assert.strictEqual(test_reference.decrementRefcount(), 0); |
| 130 | + // Value should be GC'd because the ref is now weak! |
| 131 | + await gcUntil( |
| 132 | + "Strong reference, increment then decrement to weak reference (cont.d-3)", |
| 133 | + () => test_reference.finalizeCount === 1, |
| 134 | + ); |
| 135 | + test_reference.deleteReference(); |
| 136 | + // Value was already GC'd |
| 137 | + await gcUntil( |
| 138 | + "Strong reference, increment then decrement to weak reference (cont.d-4)", |
| 139 | + () => test_reference.finalizeCount === 1, |
| 140 | + ); |
| 141 | +} |
| 142 | +runTests(); |
| 143 | + |
| 144 | +// This test creates a napi_ref on an object that has |
| 145 | +// been wrapped by napi_wrap and for which the finalizer |
| 146 | +// for the wrap calls napi_delete_ref on that napi_ref. |
| 147 | +// |
| 148 | +// Since both the wrap and the reference use the same |
| 149 | +// object the finalizer for the wrap and reference |
| 150 | +// may run in the same gc and in any order. |
| 151 | +// |
| 152 | +// It does that to validate that napi_delete_ref can be |
| 153 | +// called before the finalizer has been run for the |
| 154 | +// reference (there is a finalizer behind the scenes even |
| 155 | +// though it cannot be passed to napi_create_reference). |
| 156 | +// |
| 157 | +// Since the order is not guaranteed, run the |
| 158 | +// test a number of times maximize the chance that we |
| 159 | +// get a run with the desired order for the test. |
| 160 | +// |
| 161 | +// 1000 reliably recreated the problem without the fix |
| 162 | +// required to ensure delete could be called before |
| 163 | +// the finalizer in manual testing. |
| 164 | +for (let i = 0; i < 1000; i++) { |
| 165 | + const wrapObject = new Object(); |
| 166 | + test_reference.validateDeleteBeforeFinalize(wrapObject); |
| 167 | + let gcCount = 1; |
| 168 | + gcUntil("test", () => gcCount-- > 0); |
| 169 | +} |
0 commit comments