Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MLN-7315 | feat: increase controller coverage
  • Loading branch information
Vitaly Isaev committed Oct 4, 2022
commit 5bc3fc03f82cb464b1ec91fffc3acc934dd860d6
34 changes: 34 additions & 0 deletions backpressure/operator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/

package backpressure

import (
"testing"

"github.com/go-logr/logr/testr"
"github.com/newcloudtechnologies/memlimiter/stats"
"github.com/stretchr/testify/require"
)

func TestOperator(t *testing.T) {
logger := testr.New(t)
notifications := make(chan *stats.MemLimiterStats, 1)

op := NewOperator(logger, WithNotificationsOption(notifications))

params := &stats.ControlParameters{
GOGC: 20,
ThrottlingPercentage: 80,
}

err := op.SetControlParameters(params)
require.NoError(t, err)

notification := <-notifications

require.Equal(t, params, notification.Backpressure.ControlParameters)
}
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
// Prepare validates config.
func (c *Config) Prepare() error {
if c == nil {
// This means that user wants to use stub instead of real memlimiter
return nil
}

Expand Down
25 changes: 25 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/

package memlimiter

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
t.Run("config nil", func(t *testing.T) {
var c *Config
require.NoError(t, c.Prepare())
})

t.Run("controller config nil", func(t *testing.T) {
c := &Config{ControllerNextGC: nil}
require.Error(t, c.Prepare())
})
}
43 changes: 43 additions & 0 deletions constructor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/

package memlimiter

import (
"testing"
"time"

"github.com/go-logr/logr/testr"
"github.com/newcloudtechnologies/memlimiter/stats"
"github.com/stretchr/testify/require"
)

func TestConstructor(t *testing.T) {
t.Run("stub", func(t *testing.T) {
logger := testr.New(t)

const delay = 10 * time.Millisecond

subscription := stats.NewSubscriptionDefault(logger, delay)
defer subscription.Quit()

service, err := NewServiceFromConfig(
logger,
nil, // use stub instead of real service
WithServiceStatsSubscription(subscription),
)

defer service.Quit()

require.NoError(t, err)

time.Sleep(2 * delay)

ss, err := service.GetStats()
require.NoError(t, err)
require.NotNil(t, ss)
})
}
25 changes: 25 additions & 0 deletions controller/nextgc/component_p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/

package nextgc

import (
"testing"

"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/require"
)

func TestComponentP(t *testing.T) {
logger := testr.New(t)
cmp := &componentP{logger: logger}

_, err := cmp.value(-1)
require.Error(t, err)

_, err = cmp.value(2)
require.NoError(t, err)
}
68 changes: 68 additions & 0 deletions controller/nextgc/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/

package nextgc

import (
"testing"

"github.com/newcloudtechnologies/memlimiter/utils/config/bytes"
"github.com/newcloudtechnologies/memlimiter/utils/config/duration"
"github.com/stretchr/testify/require"
)

func TestComponentConfig(t *testing.T) {
t.Run("bad RSS limit", func(t *testing.T) {
c := &ControllerConfig{RSSLimit: bytes.Bytes{Value: 0}}
require.Error(t, c.Prepare())
})

t.Run("bad danger zone GOGC", func(t *testing.T) {
c := &ControllerConfig{
RSSLimit: bytes.Bytes{Value: 1},
DangerZoneGOGC: 120,
}
require.Error(t, c.Prepare())
})

t.Run("bad danger zone throttling", func(t *testing.T) {
c := &ControllerConfig{
RSSLimit: bytes.Bytes{Value: 1},
DangerZoneGOGC: 50,
DangerZoneThrottling: 120,
}
require.Error(t, c.Prepare())
})

t.Run("bad period", func(t *testing.T) {
c := &ControllerConfig{
RSSLimit: bytes.Bytes{Value: 1},
DangerZoneGOGC: 50,
DangerZoneThrottling: 90,
Period: duration.Duration{Duration: 0},
}
require.Error(t, c.Prepare())
})

t.Run("bad component proportional", func(t *testing.T) {
c := &ControllerConfig{
RSSLimit: bytes.Bytes{Value: 1},
DangerZoneGOGC: 50,
DangerZoneThrottling: 90,
Period: duration.Duration{Duration: 1},
}
require.Error(t, c.Prepare())
})
}

func TestComponentProportionalConfig(t *testing.T) {
t.Run("invalid proportional config", func(t *testing.T) {
c := &ComponentProportionalConfig{
Coefficient: 0,
}
require.Error(t, c.Prepare())
})
}
12 changes: 9 additions & 3 deletions controller/nextgc/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ func TestController(t *testing.T) {
memoryBudgetExhausted := &stats.ServiceStatsMock{}
memoryBudgetExhausted.On("NextGC").Return(uint64(950 * bytefmt.MEGABYTE))
memoryBudgetExhausted.On("RSS").Return(uint64(900 * bytefmt.MEGABYTE))
memoryBudgetExhausted.On("ConsumptionReport").Return((*stats.ConsumptionReport)(nil))
cr1 := &stats.ConsumptionReport{
Cgo: map[string]uint64{"some_important_cache": 5 * bytefmt.MEGABYTE},
}
memoryBudgetExhausted.On("ConsumptionReport").Return(cr1)

// In the second case the memory budget utilization returns to the ordinary values.
memoryBudgetNormal := &stats.ServiceStatsMock{}
memoryBudgetNormal.On("NextGC").Return(uint64(300 * bytefmt.MEGABYTE))
memoryBudgetNormal.On("RSS").Return(uint64(500 * bytefmt.MEGABYTE))
memoryBudgetNormal.On("ConsumptionReport").Return((*stats.ConsumptionReport)(nil))
cr2 := &stats.ConsumptionReport{
Cgo: map[string]uint64{"some_important_cache": 1 * bytefmt.MEGABYTE},
}
memoryBudgetNormal.On("ConsumptionReport").Return(cr2)

subscriptionMock := &stats.SubscriptionMock{
Chan: make(chan stats.ServiceStats),
Expand Down Expand Up @@ -99,7 +105,7 @@ func TestController(t *testing.T) {
backpressureOperatorMock.On(
"SetControlParameters",
mock.MatchedBy(func(val *stats.ControlParameters) bool {
return val.GOGC == 80 && val.ThrottlingPercentage == 20
return val.GOGC == 78 && val.ThrottlingPercentage == 22
}),
).Return(nil).Once().Run(
func(args mock.Arguments) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794
github.com/go-logr/logr v1.2.3
github.com/go-logr/stdr v1.2.2
github.com/go-logr/zapr v1.2.3
github.com/golang/protobuf v1.5.2
github.com/pkg/errors v0.9.1
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
Expand Down