fix(test): force GC behaviour for determinism#2807
Merged
Conversation
|
The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2807 +/- ##
==========================================
+ Coverage 56.62% 56.63% +0.01%
==========================================
Files 2031 2031
Lines 165929 165929
==========================================
+ Hits 93962 93982 +20
+ Misses 63734 63721 -13
+ Partials 8233 8226 -7
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
pompon0
approved these changes
Feb 5, 2026
masih
approved these changes
Feb 5, 2026
| _ = el.Next() | ||
| els[i] = nil // Clear reference to allow GC | ||
| } | ||
| els = nil // Clear the slice to allow GC of all elements |
Collaborator
There was a problem hiding this comment.
I would probably rewrite this entire test to use runtime.ReadMemStats. That should be more reliable and inherently less flaky. Because, it won't depend on the finalizer timing.
Something like this:
func TestGCRandom(t *testing.T) {
const numElements = 10000
// Force GC and get baseline
runtime.GC()
runtime.GC() // twice to clean up finalizers from previous tests
var before runtime.MemStats
runtime.ReadMemStats(&before)
l := New()
type value struct {
Data [128]byte // Make it large enough to measure
}
for i := 0; i < numElements; i++ {
v := new(value)
l.PushBack(v)
}
els := make([]*CElement, 0, numElements)
for el := l.Front(); el != nil; el = el.Next() {
els = append(els, el)
}
// Remove in random order
for _, i := range mrand.Perm(numElements) {
l.Remove(els[i])
els[i] = nil
}
els = nil
// Force GC and measure
runtime.GC()
runtime.GC()
var after runtime.MemStats
runtime.ReadMemStats(&after)
// Check that memory was reclaimed (allow some slack for runtime overhead)
leaked := int64(after.HeapAlloc) - int64(before.HeapAlloc)
maxAcceptableLeak := int64(numElements * 64) // Much less than numElements * sizeof(value)
if leaked > maxAcceptableLeak {
t.Errorf("potential memory leak: %d bytes not reclaimed", leaked)
}
}
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cursor-supplied change, aims to prevent failures like this one https://github.com/sei-protocol/sei-chain/actions/runs/21715802000/job/62631307540?pr=2805