| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package memorylimiter |
| 5 |
|
| 6 |
import ( |
| 7 |
"path/filepath" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/confmap/confmaptest" |
| 15 |
) |
| 16 |
|
| 17 |
func TestUnmarshalConfig(t *testing.T) { |
| 18 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 19 |
require.NoError(t, err) |
| 20 |
cfg := &Config{} |
| 21 |
assert.NoError(t, cm.Unmarshal(&cfg)) |
| 22 |
assert.Equal(t, |
| 23 |
&Config{ |
| 24 |
CheckInterval: 5 * time.Second, |
| 25 |
MemoryLimitMiB: 4000, |
| 26 |
MemorySpikeLimitMiB: 500, |
| 27 |
}, cfg) |
| 28 |
} |
| 29 |
|
| 30 |
func TestConfigValidate(t *testing.T) { |
| 31 |
tests := []struct { |
| 32 |
name string |
| 33 |
cfg *Config |
| 34 |
err error |
| 35 |
}{ |
| 36 |
{ |
| 37 |
name: "valid", |
| 38 |
cfg: &Config{ |
| 39 |
MemoryLimitMiB: 5722, |
| 40 |
MemorySpikeLimitMiB: 1907, |
| 41 |
CheckInterval: 100 * time.Millisecond, |
| 42 |
}, |
| 43 |
err: nil, |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "zero check interval", |
| 47 |
cfg: &Config{ |
| 48 |
CheckInterval: 0, |
| 49 |
}, |
| 50 |
err: errCheckIntervalOutOfRange, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "unset memory limit", |
| 54 |
cfg: &Config{ |
| 55 |
CheckInterval: 1 * time.Second, |
| 56 |
MemoryLimitMiB: 0, |
| 57 |
MemoryLimitPercentage: 0, |
| 58 |
}, |
| 59 |
err: errLimitOutOfRange, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "invalid memory spike limit", |
| 63 |
cfg: &Config{ |
| 64 |
CheckInterval: 1 * time.Second, |
| 65 |
MemoryLimitMiB: 10, |
| 66 |
MemorySpikeLimitMiB: 10, |
| 67 |
}, |
| 68 |
err: errSpikeLimitOutOfRange, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "invalid memory percentage limit", |
| 72 |
cfg: &Config{ |
| 73 |
CheckInterval: 1 * time.Second, |
| 74 |
MemoryLimitPercentage: 101, |
| 75 |
}, |
| 76 |
err: errLimitPercentageOutOfRange, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "invalid memory spike percentage limit", |
| 80 |
cfg: &Config{ |
| 81 |
CheckInterval: 1 * time.Second, |
| 82 |
MemoryLimitPercentage: 50, |
| 83 |
MemorySpikePercentage: 60, |
| 84 |
}, |
| 85 |
err: errSpikeLimitPercentageOutOfRange, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "invalid gc intervals", |
| 89 |
cfg: &Config{ |
| 90 |
CheckInterval: 100 * time.Millisecond, |
| 91 |
MinGCIntervalWhenSoftLimited: 50 * time.Millisecond, |
| 92 |
MinGCIntervalWhenHardLimited: 100 * time.Millisecond, |
| 93 |
MemoryLimitMiB: 5722, |
| 94 |
MemorySpikeLimitMiB: 1907, |
| 95 |
}, |
| 96 |
err: errInconsistentGCMinInterval, |
| 97 |
}, |
| 98 |
} |
| 99 |
for _, tt := range tests { |
| 100 |
t.Run(tt.name, func(t *testing.T) { |
| 101 |
err := tt.cfg.Validate() |
| 102 |
assert.Equal(t, tt.err, err) |
| 103 |
}) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestUnmarshalInvalidConfig(t *testing.T) { |
| 108 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "negative_unsigned_limits_config.yaml")) |
| 109 |
require.NoError(t, err) |
| 110 |
cfg := &Config{} |
| 111 |
err = cm.Unmarshal(&cfg) |
| 112 |
require.ErrorContains(t, err, "'limit_mib' cannot parse value as 'uint32': -2000 overflows uint") |
| 113 |
require.ErrorContains(t, err, "'spike_limit_mib' cannot parse value as 'uint32': -2300 overflows uint") |
| 114 |
} |