Skip to content

Commit 278c4ba

Browse files
committed
update test
1 parent 41fce50 commit 278c4ba

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

extension/memorylimiterextension/memorylimiter_test.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,63 @@ func TestMemoryPressureResponse(t *testing.T) {
2121
ctx := context.Background()
2222

2323
tests := []struct {
24-
name string
25-
mlCfg *Config
26-
getMemFn func() (uint64, error)
27-
readMemStatsFn func(m *runtime.MemStats)
28-
expectError bool
24+
name string
25+
mlCfg *Config
26+
memAlloc uint64
27+
expectError bool
2928
}{
3029
{
31-
name: "fixed_limit",
30+
name: "Below memAllocLimit",
3231
mlCfg: &Config{
33-
CheckInterval: 1 * time.Nanosecond,
34-
MemoryLimitMiB: 1024,
35-
MemorySpikeLimitMiB: 512,
32+
CheckInterval: time.Second,
33+
MemoryLimitPercentage: 50,
34+
MemorySpikePercentage: 1,
3635
},
36+
memAlloc: 800,
3737
expectError: false,
3838
},
3939
{
40-
name: "percent_limit",
40+
name: "Above memAllocLimit",
41+
mlCfg: &Config{
42+
CheckInterval: time.Second,
43+
MemoryLimitPercentage: 50,
44+
MemorySpikePercentage: 1,
45+
},
46+
memAlloc: 1800,
47+
expectError: true,
48+
},
49+
{
50+
name: "Below memSpikeLimit",
4151
mlCfg: &Config{
42-
CheckInterval: 1 * time.Nanosecond,
52+
CheckInterval: time.Second,
4353
MemoryLimitPercentage: 50,
44-
MemorySpikePercentage: 30,
54+
MemorySpikePercentage: 10,
4555
},
56+
memAlloc: 800,
4657
expectError: false,
4758
},
4859
{
49-
name: "fixed_limit",
60+
name: "Above memSpikeLimit",
5061
mlCfg: &Config{
51-
CheckInterval: 1 * time.Nanosecond,
62+
CheckInterval: time.Second,
5263
MemoryLimitPercentage: 50,
53-
MemorySpikePercentage: 30,
64+
MemorySpikePercentage: 11,
5465
},
55-
getMemFn: totalMemory,
56-
readMemStatsFn: readMemStats,
57-
expectError: true,
66+
memAlloc: 800,
67+
expectError: true,
5868
},
5969
}
6070
for _, tt := range tests {
6171
t.Run(tt.name, func(t *testing.T) {
62-
if tt.getMemFn != nil {
63-
memorylimiter.GetMemoryFn = tt.getMemFn
64-
}
65-
if tt.readMemStatsFn != nil {
66-
memorylimiter.ReadMemStatsFn = tt.readMemStatsFn
72+
memorylimiter.GetMemoryFn = totalMemory
73+
memorylimiter.ReadMemStatsFn = func(ms *runtime.MemStats) {
74+
ms.Alloc = tt.memAlloc
6775
}
6876
ml, err := newMemoryLimiter(tt.mlCfg, zap.NewNop())
69-
7077
assert.NoError(t, err)
78+
7179
assert.NoError(t, ml.Start(ctx, &mockHost{}))
72-
time.Sleep(50 * time.Millisecond)
80+
ml.memLimiter.CheckMemLimits()
7381
mustRefuse := ml.MustRefuse()
7482
if tt.expectError {
7583
assert.True(t, mustRefuse)
@@ -94,9 +102,5 @@ func (h *mockHost) GetExtensions() map[component.ID]component.Component {
94102
}
95103

96104
func totalMemory() (uint64, error) {
97-
return uint64(4096), nil
98-
}
99-
100-
func readMemStats(m *runtime.MemStats) {
101-
m.Alloc = 2000
105+
return uint64(2048), nil
102106
}

internal/memorylimiter/memorylimiter_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func TestBallastSize(t *testing.T) {
162162

163163
got.startMonitoring()
164164
require.NoError(t, got.Start(context.Background(), &host{ballastSize: 113}))
165+
assert.Equal(t, uint64(113), got.ballastSize)
165166
require.NoError(t, got.Shutdown(context.Background()))
166167
}
167168

0 commit comments

Comments
 (0)