Merge pull request #2607 from my-git9/metricstest3

add ut for util/metrics
This commit is contained in:
karmada-bot 2022-11-19 15:49:02 +08:00 committed by GitHub
commit d46db85ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 10 deletions

View File

@ -19,16 +19,6 @@ func GetResultByError(err error) string {
return ResultSuccess
}
// DurationInMicroseconds gets the time in microseconds.
func DurationInMicroseconds(start time.Time) float64 {
return float64(time.Since(start).Nanoseconds()) / float64(time.Microsecond.Nanoseconds())
}
// DurationInMilliseconds gets the time in milliseconds.
func DurationInMilliseconds(start time.Time) float64 {
return float64(time.Since(start).Nanoseconds()) / float64(time.Millisecond.Nanoseconds())
}
// DurationInSeconds gets the time in seconds.
func DurationInSeconds(start time.Time) float64 {
return time.Since(start).Seconds()

View File

@ -0,0 +1,32 @@
package metrics
import (
"errors"
"testing"
)
func TestGetResultByError(t *testing.T) {
tests := []struct {
name string
error error
want string
}{
{
name: "error is nil",
error: nil,
want: "success",
},
{
name: "error is not nil",
error: errors.New("hello,error"),
want: "error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetResultByError(tt.error); got != tt.want {
t.Errorf("GetResultByError() = %v, want %v", got, tt.want)
}
})
}
}