2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-16 00:13:24 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-08-16 00:38:13 +00:00
|
|
|
"bytes"
|
2017-09-01 23:40:27 +00:00
|
|
|
"sync"
|
2015-08-16 00:13:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-08-16 00:38:13 +00:00
|
|
|
|
2017-09-01 23:40:27 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2023-05-01 21:18:34 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-16 00:13:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeTable(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-16 00:13:24 +00:00
|
|
|
tt := NewTimeTable(time.Second, time.Minute)
|
|
|
|
|
|
|
|
index := tt.NearestIndex(time.Now())
|
|
|
|
if index != 0 {
|
|
|
|
t.Fatalf("bad: %v", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
when := tt.NearestTime(1000)
|
|
|
|
if !when.IsZero() {
|
|
|
|
t.Fatalf("bad: %v", when)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Witness some data
|
|
|
|
start := time.Now()
|
|
|
|
plusOne := start.Add(time.Minute)
|
|
|
|
plusTwo := start.Add(2 * time.Minute)
|
|
|
|
plusFive := start.Add(5 * time.Minute)
|
|
|
|
plusThirty := start.Add(30 * time.Minute)
|
|
|
|
plusHour := start.Add(60 * time.Minute)
|
|
|
|
plusHourHalf := start.Add(90 * time.Minute)
|
|
|
|
|
|
|
|
tt.Witness(2, start)
|
|
|
|
tt.Witness(2, start)
|
|
|
|
|
|
|
|
tt.Witness(10, plusOne)
|
|
|
|
tt.Witness(10, plusOne)
|
|
|
|
|
|
|
|
tt.Witness(20, plusTwo)
|
|
|
|
tt.Witness(20, plusTwo)
|
|
|
|
|
|
|
|
tt.Witness(30, plusFive)
|
|
|
|
tt.Witness(30, plusFive)
|
|
|
|
|
|
|
|
tt.Witness(40, plusThirty)
|
|
|
|
tt.Witness(40, plusThirty)
|
|
|
|
|
|
|
|
tt.Witness(50, plusHour)
|
|
|
|
tt.Witness(50, plusHour)
|
|
|
|
|
|
|
|
type tcase struct {
|
|
|
|
when time.Time
|
|
|
|
expectIndex uint64
|
|
|
|
|
|
|
|
index uint64
|
|
|
|
expectWhen time.Time
|
|
|
|
}
|
|
|
|
cases := []tcase{
|
|
|
|
// Exact match
|
|
|
|
{start, 2, 2, start},
|
|
|
|
{plusOne, 10, 10, plusOne},
|
|
|
|
{plusHour, 50, 50, plusHour},
|
|
|
|
|
|
|
|
// Before the newest entry
|
|
|
|
{plusHourHalf, 50, 51, plusHour},
|
|
|
|
|
|
|
|
// After the oldest entry
|
|
|
|
{time.Time{}, 0, 1, time.Time{}},
|
|
|
|
|
|
|
|
// Mid range
|
|
|
|
{start.Add(3 * time.Minute), 20, 25, plusTwo},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
index := tt.NearestIndex(tc.when)
|
|
|
|
if index != tc.expectIndex {
|
|
|
|
t.Fatalf("bad: %v %v", index, tc.expectIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
when := tt.NearestTime(tc.index)
|
|
|
|
if when != tc.expectWhen {
|
|
|
|
t.Fatalf("bad: for %d %v %v", tc.index, when, tc.expectWhen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeTable_SerializeDeserialize(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-16 00:13:24 +00:00
|
|
|
tt := NewTimeTable(time.Second, time.Minute)
|
|
|
|
|
|
|
|
// Witness some data
|
|
|
|
start := time.Now()
|
|
|
|
plusOne := start.Add(time.Minute)
|
|
|
|
plusTwo := start.Add(2 * time.Minute)
|
|
|
|
plusFive := start.Add(5 * time.Minute)
|
|
|
|
plusThirty := start.Add(30 * time.Minute)
|
|
|
|
plusHour := start.Add(60 * time.Minute)
|
|
|
|
|
|
|
|
tt.Witness(2, start)
|
|
|
|
tt.Witness(10, plusOne)
|
|
|
|
tt.Witness(20, plusTwo)
|
|
|
|
tt.Witness(30, plusFive)
|
|
|
|
tt.Witness(40, plusThirty)
|
|
|
|
tt.Witness(50, plusHour)
|
|
|
|
|
2015-08-16 00:38:13 +00:00
|
|
|
var buf bytes.Buffer
|
2015-11-18 23:16:42 +00:00
|
|
|
enc := codec.NewEncoder(&buf, structs.MsgpackHandle)
|
2015-08-16 00:38:13 +00:00
|
|
|
|
|
|
|
err := tt.Serialize(enc)
|
2015-08-16 00:13:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:16:42 +00:00
|
|
|
dec := codec.NewDecoder(&buf, structs.MsgpackHandle)
|
2015-08-16 00:38:13 +00:00
|
|
|
|
2015-08-16 00:13:24 +00:00
|
|
|
tt2 := NewTimeTable(time.Second, time.Minute)
|
2015-08-16 00:38:13 +00:00
|
|
|
err = tt2.Deserialize(dec)
|
2015-08-16 00:13:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-01 23:40:27 +00:00
|
|
|
o := cmp.AllowUnexported(TimeTable{})
|
|
|
|
o2 := cmpopts.IgnoreTypes(sync.RWMutex{})
|
|
|
|
if !cmp.Equal(tt.table, tt2.table, o, o2) {
|
|
|
|
t.Fatalf("bad: %s", cmp.Diff(tt, tt2, o, o2))
|
2015-08-16 00:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 00:18:19 +00:00
|
|
|
|
|
|
|
func TestTimeTable_Overflow(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-16 00:18:19 +00:00
|
|
|
tt := NewTimeTable(time.Second, 3*time.Second)
|
|
|
|
|
|
|
|
// Witness some data
|
|
|
|
start := time.Now()
|
|
|
|
plusOne := start.Add(time.Second)
|
|
|
|
plusTwo := start.Add(2 * time.Second)
|
|
|
|
plusThree := start.Add(3 * time.Second)
|
|
|
|
|
|
|
|
tt.Witness(10, start)
|
|
|
|
tt.Witness(20, plusOne)
|
|
|
|
tt.Witness(30, plusTwo)
|
|
|
|
tt.Witness(40, plusThree)
|
|
|
|
|
|
|
|
if len(tt.table) != 3 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
index := tt.NearestIndex(start)
|
|
|
|
if index != 0 {
|
|
|
|
t.Fatalf("bad: %v %v", index, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
when := tt.NearestTime(15)
|
|
|
|
if !when.IsZero() {
|
|
|
|
t.Fatalf("bad: %v", when)
|
|
|
|
}
|
|
|
|
}
|