-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Prevent reactive variables from retaining otherwise unreachable InMemoryCache objects. #7661
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
652c3e0
Avoid writing to cache in QueryInfo#markResult if stopped.
benjamn 6ec1fb3
Associate separate varDep with each InMemoryCache instance.
benjamn 26905f0
Add a standalone script for testing cache garbage collection.
benjamn ad20f88
Convert standalone GC test to use Mocha.
benjamn 83adc96
Import from @apollo/client/core and install graphql peer dependency.
benjamn f22392b
Run scripts/memory tests as part of test:ci.
benjamn a26a88b
Use Node.js 14 instead of 12 for Circle CI tests.
benjamn e69ae90
Use itAsync test helper to ensure clearTimeout called.
benjamn 53a6f7c
Make sure FinalizationRegistry itself not prematurely collected.
benjamn 668711a
Make sure Mocha exits after tests finish/fail/timeout.
benjamn f42648a
Mention PR #7661 in CHANGELOG.md.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Memory and Garbage Collection tests | ||
|
||
This directory contains a few important memory-related tests that were | ||
difficult to run within the usual Jest environment. Instead, these tests | ||
run directly in Node.js, importing `@apollo/client` from the `../../dist` | ||
directory, rather than from `../../src`. | ||
|
||
## Running the tests | ||
|
||
Run `npm install` in this directory, followed by `npm test`. Failure is | ||
indicated by a non-zero exit code. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "apollo-client-memory-tests", | ||
"private": true, | ||
"scripts": { | ||
"preinstall": "cd ../.. && npm i && npm run build", | ||
"test": "mocha --exit --expose-gc tests.js" | ||
}, | ||
"dependencies": { | ||
"@apollo/client": "file:../../dist", | ||
"graphql": "^15.5.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^8.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const assert = require("assert"); | ||
const { | ||
ApolloClient, | ||
InMemoryCache, | ||
gql, | ||
makeVar, | ||
} = require("@apollo/client/core"); | ||
|
||
function itAsync(message, testFn) { | ||
const start = Date.now(); | ||
let timeout; | ||
(function pollGC() { | ||
gc(); // enabled by --expose-gc | ||
// Passing --exit to mocha should cause the process to exit after | ||
// tests pass/fail/timeout, but (in case that fails) we also set a | ||
// hard limit of 10 seconds for GC polling. | ||
if (Date.now() < start + 10000) { | ||
timeout = setTimeout(pollGC, 100); | ||
} | ||
})(); | ||
return it(message, () => new Promise(testFn).finally(() => { | ||
clearTimeout(timeout); | ||
})); | ||
} | ||
|
||
const registries = []; | ||
function makeRegistry(callback, reject) { | ||
assert.strictEqual(typeof callback, "function"); | ||
assert.strictEqual(typeof reject, "function"); | ||
|
||
const registry = new FinalizationRegistry(key => { | ||
try { | ||
callback(key); | ||
} catch (error) { | ||
// Exceptions thrown in FinalizationRegistry callbacks can be tricky | ||
// for test frameworks to catch, without some help. | ||
reject(error); | ||
} | ||
}); | ||
|
||
// If the registry object itself gets garbage collected before the | ||
// callback fires, the callback might never be called: | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry#notes_on_cleanup_callbacks | ||
registries.push(registry); | ||
|
||
return registry; | ||
} | ||
|
||
describe("garbage collection", () => { | ||
itAsync("should collect client.cache after client.stop()", (resolve, reject) => { | ||
const registry = makeRegistry(key => { | ||
assert.strictEqual(key, "client.cache"); | ||
resolve(); | ||
}, reject); | ||
|
||
const localVar = makeVar(123); | ||
|
||
(function (client) { | ||
registry.register(client.cache, "client.cache"); | ||
|
||
client.watchQuery({ | ||
query: gql`query { local }`, | ||
}).subscribe({ | ||
next(result) { | ||
assert.deepStrictEqual(result.data, { | ||
local: 123, | ||
}); | ||
|
||
client.stop(); | ||
}, | ||
}); | ||
|
||
})(new ApolloClient({ | ||
cache: new InMemoryCache({ | ||
typePolicies: { | ||
Query: { | ||
fields: { | ||
local() { | ||
return localVar(); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you're ever tempted to use a
FinalizationRegistry
, make sure you don't accidentally let the registry itself get garbage collected, because then any unfired callbacks you've registered will never fire!