Skip to content

Commit

Permalink
fix: Remove CheckMinInterval return value and modify tests
Browse files Browse the repository at this point in the history
Remove CheckMinInterval return value and verify the mock lc method is called in tests.

Signed-off-by: Lindsey Cheng <beckysocute@gmail.com>
  • Loading branch information
lindseysimple committed Jul 14, 2023
1 parent b9a3891 commit 4c96589
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
8 changes: 3 additions & 5 deletions internal/pkg/utils/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import (

// CheckMinInterval parses the ISO 8601 time duration string to Duration type
// and evaluates if the duration value is smaller than the suggested minimum duration
func CheckMinInterval(value string, minDuration time.Duration, lc logger.LoggingClient) bool {
func CheckMinInterval(value string, minDuration time.Duration, lc logger.LoggingClient) {
valueDuration, err := time.ParseDuration(value)
if err != nil {
lc.Errorf("failed to parse the interval duration string %s to a duration time value: %v", value, err)
return false
return
}

if valueDuration < minDuration {
// the duration value is smaller than the min
lc.Warnf("the interval value '%s' is smaller than the suggested value '%s', which might cause abnormal CPU increase", valueDuration, minDuration)
return false
lc.Warnf("the interval value '%s' is smaller than the suggested value '%s', which might cause abnormal CPU increase", value, minDuration)
}
return true
}
49 changes: 36 additions & 13 deletions internal/pkg/utils/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,54 @@
package utils

import (
"errors"
"testing"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"

"github.com/stretchr/testify/assert"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger/mocks"
)

func TestCheckMinInterval(t *testing.T) {
lc := logger.NewMockClient()
validInterval := "1s"
lessThanMinInterval := "100us"
invalidInterval := "INVALID"
minInterval1 := 10 * time.Millisecond
minInterval2 := 1 * time.Millisecond

warnMsg := "the interval value '%s' is smaller than the suggested value '%s', which might cause abnormal CPU increase"
errMsg := "failed to parse the interval duration string %s to a duration time value: %v"
expectedErr := errors.New("time: invalid duration \"" + invalidInterval + "\"")

lcMock := &mocks.LoggingClient{}
lcMock.On("Warnf", warnMsg, validInterval, minInterval1)
lcMock.On("Warnf", warnMsg, lessThanMinInterval, minInterval2)
lcMock.On("Errorf", errMsg, invalidInterval, expectedErr)

tests := []struct {
name string
interval string
min time.Duration
result bool
name string
interval string
min time.Duration
logExpected bool
logLevel string
logMsg string
err error
}{
{"valid - interval is bigger than the minimum value", "1s", 10 * time.Millisecond, true},
{"invalid - interval is smaller than the minimum value", "100us", 1 * time.Millisecond, false},
{"invalid - parsing duration string failed", "INVALID", 1 * time.Millisecond, false},
{"valid - interval is bigger than the minimum value", validInterval, minInterval1, false, "", "", nil},
{"invalid - interval is smaller than the minimum value", lessThanMinInterval, minInterval2, true, "Warnf", warnMsg, nil},
{"invalid - parsing duration string failed", invalidInterval, minInterval2, true, "Errorf", errMsg, expectedErr},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CheckMinInterval(tt.interval, tt.min, lc)
assert.Equal(t, tt.result, result)
CheckMinInterval(tt.interval, tt.min, lcMock)
if tt.logExpected {
if tt.logLevel == "Warnf" {
lcMock.AssertCalled(t, tt.logLevel, tt.logMsg, tt.interval, tt.min)
} else {
lcMock.AssertCalled(t, tt.logLevel, tt.logMsg, tt.interval, tt.err)
}
} else {
lcMock.AssertNotCalled(t, tt.logLevel, tt.logMsg, tt.interval, tt.min)
}
})
}
}

0 comments on commit 4c96589

Please sign in to comment.