services/meta: improve readability of Contains function, add unit tests
parent
e7389b18c0
commit
a8692a9e24
|
@ -1321,9 +1321,9 @@ func (a ShardGroupInfos) Less(i, j int) bool {
|
|||
return iEnd.Before(jEnd)
|
||||
}
|
||||
|
||||
// Contains returns true if the shard group contains data for the timestamp.
|
||||
func (sgi *ShardGroupInfo) Contains(timestamp time.Time) bool {
|
||||
return !sgi.StartTime.After(timestamp) && sgi.EndTime.After(timestamp)
|
||||
// Contains returns true iif StartTime ≤ t < EndTime.
|
||||
func (sgi *ShardGroupInfo) Contains(t time.Time) bool {
|
||||
return !t.Before(sgi.StartTime) && t.Before(sgi.EndTime)
|
||||
}
|
||||
|
||||
// Overlaps returns whether the shard group contains data for the time range between min and max
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package meta_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/influxdb"
|
||||
"github.com/influxdata/influxdb/pkg/testing/assert"
|
||||
"github.com/influxdata/influxql"
|
||||
|
||||
"github.com/influxdata/influxdb/services/meta"
|
||||
|
@ -327,3 +329,27 @@ func TestUserInfo_AuthorizeDatabase(t *testing.T) {
|
|||
t.Fatalf("expected admin to be authorized but it wasn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShardGroupInfo_Contains(t *testing.T) {
|
||||
sgi := &meta.ShardGroupInfo{StartTime: time.Unix(10, 0), EndTime: time.Unix(20, 0)}
|
||||
|
||||
tests := []struct {
|
||||
ts time.Time
|
||||
exp bool
|
||||
}{
|
||||
{time.Unix(0, 0), false},
|
||||
{time.Unix(9, 0), false},
|
||||
{time.Unix(10, 0), true},
|
||||
{time.Unix(11, 0), true},
|
||||
{time.Unix(15, 0), true},
|
||||
{time.Unix(19, 0), true},
|
||||
{time.Unix(20, 0), false},
|
||||
{time.Unix(21, 0), false},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("ts=%d", test.ts.Unix()), func(t *testing.T) {
|
||||
got := sgi.Contains(test.ts)
|
||||
assert.Equal(t, got, test.exp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue