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