-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add warn log for create/update interval (#4597)
* fix: Add warn log for create/update interval Add warn log for interval value from Interval and AutoEvent if the value is less than the suggested value. Closes #4586. Signed-off-by: Lindsey Cheng <beckysocute@gmail.com> * fix: Modify minimum duration param type to time.Duration Modify minimum duration param type of CheckMinInterval func to time.Duration. Signed-off-by: Lindsey Cheng <beckysocute@gmail.com> * fix: Remove CheckMinInterval return value and modify tests Remove CheckMinInterval return value and verify the mock lc method is called in tests. Signed-off-by: Lindsey Cheng <beckysocute@gmail.com> --------- Signed-off-by: Lindsey Cheng <beckysocute@gmail.com>
- Loading branch information
1 parent
fa614b6
commit db7b7ee
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
) | ||
|
||
// 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) { | ||
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 | ||
} | ||
|
||
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", value, minDuration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger/mocks" | ||
) | ||
|
||
func TestCheckMinInterval(t *testing.T) { | ||
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 | ||
logExpected bool | ||
logLevel string | ||
logMsg string | ||
err error | ||
}{ | ||
{"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) { | ||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters