-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
test: Make N-API weak-ref GC tests asynchronous #13121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,80 +11,110 @@ const test_reference = require(`./build/${common.buildType}/test_reference`); | |
// of a finalizer callback increments the finalizeCount property. | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
|
||
{ | ||
// External value without a finalizer | ||
let value = test_reference.createExternal(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(typeof value, 'object'); | ||
test_reference.checkExternal(value); | ||
value = null; | ||
global.gc(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
// Run each test function in sequence, | ||
// with an async delay and GC call between each. | ||
function runTests(i, title, tests) { | ||
if (tests[i]) { | ||
if (typeof tests[i] === 'string') { | ||
title = tests[i]; | ||
runTests(i + 1, title, tests); | ||
} else { | ||
try { | ||
tests[i](); | ||
} catch (e) { | ||
console.error('Test failed: ' + title); | ||
throw e; | ||
} | ||
setImmediate(() => { | ||
global.gc(); | ||
runTests(i + 1, title, tests); | ||
}); | ||
} | ||
} | ||
} | ||
runTests(0, undefined, [ | ||
|
||
{ | ||
// External value with a finalizer | ||
let value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(typeof value, 'object'); | ||
test_reference.checkExternal(value); | ||
value = null; | ||
global.gc(); | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
} | ||
|
||
{ | ||
// Strong reference | ||
let value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 1); | ||
assert.strictEqual(test_reference.referenceValue, value); | ||
value = null; | ||
global.gc(); // Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.deleteReference(); | ||
global.gc(); // Value should be GC'd because the strong ref was deleted | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
} | ||
|
||
{ | ||
// Strong reference, increment then decrement to weak reference | ||
let value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 1); | ||
value = null; | ||
global.gc(); // Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
'External value without a finalizer', | ||
|
||
() => { | ||
const value = test_reference.createExternal(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(typeof value, 'object'); | ||
test_reference.checkExternal(value); | ||
}, | ||
() => { | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
}, | ||
|
||
assert.strictEqual(test_reference.incrementRefcount(), 2); | ||
global.gc(); // Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
|
||
assert.strictEqual(test_reference.decrementRefcount(), 1); | ||
global.gc(); // Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
'External value with a finalizer', | ||
() => { | ||
const value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(typeof value, 'object'); | ||
test_reference.checkExternal(value); | ||
}, | ||
() => { | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
}, | ||
|
||
assert.strictEqual(test_reference.decrementRefcount(), 0); | ||
global.gc(); // Value should be GC'd because the ref is now weak! | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
'Weak reference', | ||
() => { | ||
const value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 0); | ||
assert.strictEqual(test_reference.referenceValue, value); | ||
}, | ||
() => { | ||
// Value should be GC'd because there is only a weak ref | ||
assert.strictEqual(test_reference.referenceValue, undefined); | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
test_reference.deleteReference(); | ||
}, | ||
|
||
test_reference.deleteReference(); | ||
global.gc(); // Value was already GC'd | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
} | ||
'Strong reference', | ||
() => { | ||
const value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 1); | ||
assert.strictEqual(test_reference.referenceValue, value); | ||
}, | ||
() => { | ||
// Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.deleteReference(); | ||
}, | ||
() => { | ||
// Value should be GC'd because the strong ref was deleted | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
}, | ||
|
||
{ | ||
// Weak reference | ||
let value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 0); | ||
assert.strictEqual(test_reference.referenceValue, value); | ||
value = null; | ||
setImmediate(common.mustCall(() => { | ||
// This test only works if gc() is called from an immediate callback. | ||
global.gc(); // Value should be GC'd because there is only a weak ref | ||
assert.strictEqual(test_reference.referenceValue, undefined); | ||
'Strong reference, increment then decrement to weak reference', | ||
() => { | ||
const value = test_reference.createExternalWithFinalize(); | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
test_reference.createReference(value, 1); | ||
}, | ||
() => { | ||
// Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(test_reference.incrementRefcount(), 2); | ||
}, | ||
() => { | ||
// Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(test_reference.decrementRefcount(), 1); | ||
}, | ||
() => { | ||
// Value should NOT be GC'd because there is a strong ref | ||
assert.strictEqual(test_reference.finalizeCount, 0); | ||
assert.strictEqual(test_reference.decrementRefcount(), 0); | ||
}, | ||
() => { | ||
// Value should be GC'd because the ref is now weak! | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
test_reference.deleteReference(); | ||
})); | ||
} | ||
}, | ||
() => { | ||
// Value was already GC'd | ||
assert.strictEqual(test_reference.finalizeCount, 1); | ||
}, | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you replace the
try...catch
,throw
, andconsole.error()
call with a call toassert.doesNotThrow()
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could work, except
assert.doesNotThrow()
loses the stack trace of the original exception; it only displays the message. So I prefer not to use it except when checking for a very specific exceptional condition. In this case, the original exception stack trace is likely to point to a line number of another assertion in the file, so that's very helpful information.