Skip to content

Commit

Permalink
test(memlimit): add FromSystem tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KimMachineGun committed Jan 9, 2024
1 parent e243cf4 commit e5e0257
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
run: |
docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896
- name: Run tests in Go container (system memory limit 16757788671b)
run: |
docker run --rm -v=$(pwd):/app -w=/app golang:1.19 go test -v ./... -expected-system=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
test-ubuntu-22_04:
runs-on: ubuntu-22.04

Expand All @@ -46,3 +50,7 @@ jobs:
- name: Run tests in Go container (4321m)
run: |
docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896
- name: Run tests in Go container (system memory limit 16757788671b)
run: |
docker run --rm -v=$(pwd):/app -w=/app golang:1.19 go test -v ./... -expected-system=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
10 changes: 7 additions & 3 deletions memlimit/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func TestFromCgroup(t *testing.T) {
if expected == 0 {
t.Skip()
}

limit, err := FromCgroup()
if cgVersion == cgroups.Unavailable && err != ErrNoCgroup {
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrNoCgroup)
Expand All @@ -24,7 +28,7 @@ func TestFromCgroup(t *testing.T) {
}

func TestFromCgroupV1(t *testing.T) {
if cgVersion != cgroups.Legacy {
if expected == 0 || cgVersion != cgroups.Legacy {
t.Skip()
}
limit, err := FromCgroupV1()
Expand All @@ -37,7 +41,7 @@ func TestFromCgroupV1(t *testing.T) {
}

func TestFromCgroupHybrid(t *testing.T) {
if cgVersion != cgroups.Hybrid {
if expected == 0 || cgVersion != cgroups.Hybrid {
t.Skip()
}
limit, err := FromCgroupHybrid()
Expand All @@ -50,7 +54,7 @@ func TestFromCgroupHybrid(t *testing.T) {
}

func TestFromCgroupV2(t *testing.T) {
if cgVersion != cgroups.Unified {
if expected == 0 || cgVersion != cgroups.Unified {
t.Skip()
}
limit, err := FromCgroupV2()
Expand Down
60 changes: 51 additions & 9 deletions memlimit/memlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
)

var (
cgVersion cgroups.CGMode
expected uint64
cgVersion cgroups.CGMode
expected uint64
expectedSystem uint64
)

func TestMain(m *testing.M) {
flag.Uint64Var(&expected, "expected", 0, "Expected cgroup's memory limit")
flag.Uint64Var(&expectedSystem, "expected-system", 0, "Expected system memory limit")
flag.Parse()

cgVersion = cgroups.Mode()
Expand All @@ -45,7 +47,7 @@ func TestSetGoMemLimit(t *testing.T) {
},
want: int64(float64(expected) * 0.5),
wantErr: nil,
skip: cgVersion == cgroups.Unavailable,
skip: expected == 0 || cgVersion == cgroups.Unavailable,
},
{
name: "0.9",
Expand All @@ -54,7 +56,7 @@ func TestSetGoMemLimit(t *testing.T) {
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: cgVersion == cgroups.Unavailable,
skip: expected == 0 || cgVersion == cgroups.Unavailable,
},
{
name: "Unavailable",
Expand Down Expand Up @@ -103,7 +105,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: cgVersion == cgroups.Unavailable,
skip: expected == 0 || cgVersion == cgroups.Unavailable,
},
{
name: "FromCgroup_Unavaliable",
Expand All @@ -113,7 +115,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
},
want: 0,
wantErr: ErrNoCgroup,
skip: cgVersion != cgroups.Unavailable,
skip: expected == 0 || cgVersion != cgroups.Unavailable,
},
{
name: "FromCgroupV1",
Expand All @@ -123,7 +125,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: cgVersion != cgroups.Legacy,
skip: expected == 0 || cgVersion != cgroups.Legacy,
},
{
name: "FromCgroupHybrid",
Expand All @@ -133,7 +135,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: cgVersion != cgroups.Hybrid,
skip: expected == 0 || cgVersion != cgroups.Hybrid,
},
{
name: "FromCgroupV2",
Expand All @@ -143,7 +145,47 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: cgVersion != cgroups.Unified,
skip: expected == 0 || cgVersion != cgroups.Unified,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.skip {
t.Skip()
}
got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio)
if err != tt.wantErr {
t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("SetGoMemLimitWithProvider() got = %v, want %v", got, tt.want)
}
})
}
}

func TestSetGoMemLimitWithProvider_WithSystemProvider(t *testing.T) {
type args struct {
provider Provider
ratio float64
}
tests := []struct {
name string
args args
want int64
wantErr error
skip bool
}{
{
name: "FromSystem",
args: args{
provider: FromSystem,
ratio: 0.9,
},
want: int64(float64(expectedSystem) * 0.9),
wantErr: nil,
skip: expectedSystem == 0,
},
}
for _, tt := range tests {
Expand Down
58 changes: 56 additions & 2 deletions memlimit/memlimit_unsupported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
package memlimit

import (
"errors"
"flag"
"os"
"testing"
)

var (
expected uint64
)

func TestMain(m *testing.M) {
flag.Uint64Var(&expected, "expected", 0, "Expected memory limit")
flag.Parse()

os.Exit(m.Run())
}

func TestSetGoMemLimit(t *testing.T) {
type args struct {
ratio float64
Expand Down Expand Up @@ -37,7 +51,7 @@ func TestSetGoMemLimit(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SetGoMemLimit(tt.args.ratio)
if err != tt.wantErr {
if !errors.Is(err, tt.wantErr) {
t.Errorf("SetGoMemLimit() error = %v, wantErr %v", err, tt.wantErr)
return
}
Expand Down Expand Up @@ -99,7 +113,47 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio)
if err != tt.wantErr {
if !errors.Is(err, tt.wantErr) {
t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("SetGoMemLimitWithProvider() got = %v, want %v", got, tt.want)
}
})
}
}

func TestSetGoMemLimitWithProvider_WithSystemProvider(t *testing.T) {
type args struct {
provider Provider
ratio float64
}
tests := []struct {
name string
args args
want int64
wantErr error
skip bool
}{
{
name: "FromSystem",
args: args{
provider: FromSystem,
ratio: 0.9,
},
want: int64(float64(expected) * 0.9),
wantErr: nil,
skip: expected == 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.skip {
t.Skip()
}
got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio)
if !errors.Is(err, tt.wantErr) {
t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr)
return
}
Expand Down

0 comments on commit e5e0257

Please sign in to comment.