-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathmemleak_test.go
More file actions
185 lines (161 loc) · 5.68 KB
/
memleak_test.go
File metadata and controls
185 lines (161 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2026 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package discovery
import (
"context"
"fmt"
"runtime"
"sync"
"testing"
"time"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// appendToMapBuggy reproduces the pre-fix AppendToMap behaviour for comparison.
func appendToMapBuggy(r *CRDiscoverer, gvkps ...groupVersionKindPlural) {
if r.Map == nil {
r.Map = map[string]map[string][]kindPlural{}
}
if r.GVKToReflectorStopChanMap == nil {
r.GVKToReflectorStopChanMap = map[string]chan struct{}{}
}
for _, gvkp := range gvkps {
if _, ok := r.Map[gvkp.Group]; !ok {
r.Map[gvkp.Group] = map[string][]kindPlural{}
}
if _, ok := r.Map[gvkp.Group][gvkp.Version]; !ok {
r.Map[gvkp.Group][gvkp.Version] = []kindPlural{}
}
r.Map[gvkp.Group][gvkp.Version] = append(r.Map[gvkp.Group][gvkp.Version], kindPlural{Kind: gvkp.Kind, Plural: gvkp.Plural})
r.GVKToReflectorStopChanMap[gvkp.GroupVersionKind.String()] = make(chan struct{})
}
}
func makeGVKPs(n int) []groupVersionKindPlural {
gvkps := make([]groupVersionKindPlural, n)
for i := range n {
gvkps[i] = groupVersionKindPlural{
GroupVersionKind: schema.GroupVersionKind{
Group: fmt.Sprintf("group%d.example.com", i),
Version: "v1",
Kind: fmt.Sprintf("Kind%d", i),
},
Plural: fmt.Sprintf("kind%ds", i),
}
}
return gvkps
}
func heapKB() uint64 {
runtime.GC()
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
return m.HeapInuse / 1024
}
// TestMemoryLeakSimulation runs the buggy and fixed AppendToMap side-by-side
// across many poll cycles and reports heap growth and map entry counts.
func TestMemoryLeakSimulation(t *testing.T) {
const (
numGVKs = 5
pollCycles = 500
)
gvkps := makeGVKPs(numGVKs)
buggyDiscoverer := &CRDiscoverer{}
heapBefore := heapKB()
goroutinesBefore := runtime.NumGoroutine()
for range pollCycles {
appendToMapBuggy(buggyDiscoverer, gvkps...)
}
heapAfterBuggy := heapKB()
goroutinesAfterBuggy := runtime.NumGoroutine()
buggyKindCount := 0
for _, versions := range buggyDiscoverer.Map {
for _, kinds := range versions {
buggyKindCount += len(kinds)
}
}
buggyChannelCount := len(buggyDiscoverer.GVKToReflectorStopChanMap)
fixedDiscoverer := &CRDiscoverer{}
heapBeforeFixed := heapKB()
for range pollCycles {
fixedDiscoverer.AppendToMap(gvkps...)
}
heapAfterFixed := heapKB()
fixedKindCount := 0
for _, versions := range fixedDiscoverer.Map {
for _, kinds := range versions {
fixedKindCount += len(kinds)
}
}
fixedChannelCount := len(fixedDiscoverer.GVKToReflectorStopChanMap)
t.Logf("Simulation: %d GVKs × %d poll cycles", numGVKs, pollCycles)
t.Logf("")
t.Logf(" │ BUGGY (pre-fix) │ FIXED (post-fix)")
t.Logf(" ─────────────────────┼───────────────────┼──────────────────")
t.Logf(" Kind entries in map │ %17d │ %d", buggyKindCount, fixedKindCount)
t.Logf(" Stop channels live │ %17d │ %d", buggyChannelCount, fixedChannelCount)
t.Logf(" Heap before (KB) │ %17d │ %d", heapBefore, heapBeforeFixed)
t.Logf(" Heap after (KB) │ %17d │ %d", heapAfterBuggy, heapAfterFixed)
t.Logf(" Heap growth (KB) │ %17d │ %d", int64(heapAfterBuggy)-int64(heapBefore), int64(heapAfterFixed)-int64(heapBeforeFixed)) //nolint:gosec
t.Logf(" Goroutines before │ %17d │ (same baseline)", goroutinesBefore)
t.Logf(" Goroutines after │ %17d │ (no goroutines started)", goroutinesAfterBuggy)
if buggyKindCount != numGVKs*pollCycles {
t.Errorf("[buggy] expected %d kind entries (linear growth), got %d", numGVKs*pollCycles, buggyKindCount)
}
if fixedKindCount != numGVKs {
t.Errorf("[fixed] expected exactly %d kind entries (stable), got %d", numGVKs, fixedKindCount)
}
if fixedChannelCount != numGVKs {
t.Errorf("[fixed] expected exactly %d stop channels (stable), got %d", numGVKs, fixedChannelCount)
}
buggyGrowth := int64(heapAfterBuggy) - int64(heapBefore) //nolint:gosec
fixedGrowth := int64(heapAfterFixed) - int64(heapBeforeFixed) //nolint:gosec
t.Logf("Heap growth comparison is diagnostic only: buggy=%d KB fixed=%d KB", buggyGrowth, fixedGrowth)
}
// TestGoroutineLeakSimulation verifies that goroutines started with the fixed
// pattern (bridge goroutine selecting on both GVK stop channel and context
// cancellation) exit when the context is cancelled.
func TestGoroutineLeakSimulation(t *testing.T) {
const (
numGVKs = 5
rebuilds = 20
)
var wg sync.WaitGroup
for range rebuilds {
ctx, cancel := context.WithCancel(context.Background())
for range numGVKs {
gvkStopCh := make(chan struct{})
stopCh := make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
defer close(stopCh)
select {
case <-gvkStopCh:
case <-ctx.Done():
}
}()
_ = stopCh
}
cancel()
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
t.Logf("All %d bridge goroutines exited after context cancellation", numGVKs*rebuilds)
case <-time.After(2 * time.Second):
t.Errorf("goroutines did not exit within 2s after context cancellation")
}
}