mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-19 05:19:35 +00:00
fix bug in metrics first/last calculation if second arg was zero
This commit is contained in:
@@ -79,14 +79,14 @@ func (m Metric) Len() int {
|
||||
}
|
||||
|
||||
func first(t1, t2 time.Time) time.Time {
|
||||
if !t1.IsZero() && t1.Before(t2) {
|
||||
if t2.IsZero() || (!t1.IsZero() && t1.Before(t2)) {
|
||||
return t1
|
||||
}
|
||||
return t2
|
||||
}
|
||||
|
||||
func last(t1, t2 time.Time) time.Time {
|
||||
if !t1.IsZero() && t1.After(t2) {
|
||||
if t2.IsZero() || (!t1.IsZero() && t1.After(t2)) {
|
||||
return t1
|
||||
}
|
||||
return t2
|
||||
|
||||
29
report/metrics_internal_test.go
Normal file
29
report/metrics_internal_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFirstLast(t *testing.T) {
|
||||
zero, t1, t2 := time.Time{}, time.Now(), time.Now().Add(1*time.Minute)
|
||||
tests := []struct {
|
||||
arg1, arg2, first, last time.Time
|
||||
}{
|
||||
{zero, zero, zero, zero},
|
||||
{t1, zero, t1, t1},
|
||||
{zero, t1, t1, t1},
|
||||
{t1, t1, t1, t1},
|
||||
{t1, t2, t1, t2},
|
||||
{t2, t1, t1, t2},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := first(test.arg1, test.arg2); !got.Equal(test.first) {
|
||||
t.Errorf("first(%q, %q) => %q, Expected: %q", test.arg1, test.arg2, got, test.first)
|
||||
}
|
||||
|
||||
if got := last(test.arg1, test.arg2); !got.Equal(test.last) {
|
||||
t.Errorf("last(%q, %q) => %q, Expected: %q", test.arg1, test.arg2, got, test.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user