-
Notifications
You must be signed in to change notification settings - Fork 485
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
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>
- Loading branch information
1 parent
aa1b3d1
commit cb9784b
Showing
4 changed files
with
94 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,34 @@ | ||
// | ||
// 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 string | ||
func CheckMinInterval(value string, min string, lc logger.LoggingClient) bool { | ||
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 | ||
} | ||
minDuration, err := time.ParseDuration(min) | ||
if err != nil { | ||
lc.Errorf("failed to parse the minimum duration string %s to a duration time value: %v", value, err) | ||
return false | ||
} | ||
|
||
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 | ||
} | ||
return true | ||
} |
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,35 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckMinInterval(t *testing.T) { | ||
lc := logger.NewMockClient() | ||
|
||
tests := []struct { | ||
name string | ||
interval string | ||
min string | ||
result bool | ||
}{ | ||
{"valid - interval is bigger than the minimum value", "1s", "10ms", true}, | ||
{"invalid - interval is smaller than the minimum value", "100us", "1ms", false}, | ||
{"invalid - parsing duration string failed", "INVALID", "1ms", false}, | ||
} | ||
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) | ||
}) | ||
} | ||
} |
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