Add comprehensive tests for cache_across_reloads gc behaviors #404
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.
This PR adds comprehensive test coverage for the garbage collection behavior of the
cache_across_reloadsdecorator's memoization system.Background
The
cache_across_reloadsdecorator maintains two global dictionaries:memos: stores cached computation results keyed by(path, qualname)functions: stores function references keyed by(path, qualname)The
gc_memos()function, decorated with@post_reload, runs after module reloads to clean up orphaned memos for functions that no longer exist:This prevents memory leaks during hot reloading sessions when cached functions are removed or renamed.
Tests Added
Four new test functions comprehensively validate the garbage collection behavior:
test_cache_across_reloads_gc_function_removal: Verifies that memos are properly cleaned up when cached functions are completely removed from a moduletest_cache_across_reloads_gc_function_modification: Ensures memos persist when functions are modified but not removedtest_cache_across_reloads_gc_selective_removal: Tests partial cleanup when some functions are removed while others remaintest_cache_across_reloads_gc_across_modules: Validates gc behavior across multiple modulesEach test directly inspects the
memosandfunctionsdictionaries to verify correct garbage collection behavior under various hot reload scenarios.Fixes #348.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.