-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
59 lines (50 loc) · 1.32 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"fmt"
"log"
"math/rand"
"reflect"
"testing"
"time"
)
func TestLastChunkTime(t *testing.T) {
ctx := context.Background()
db, err := newDB(t.TempDir() + "/db")
if err != nil {
t.Fatal(err)
}
defer db.Close()
r := &recorder{db: db}
now := time.Now().Truncate(time.Second)
for _, sid := range []int{1, 2} {
// shuffle insertion order, to make sure when we query we truly get the
// latest, and not just the last inserted or something
var idxs []int
for i := 1; i <= 20; i++ {
idxs = append(idxs, i)
}
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd.Shuffle(len(idxs), func(i, j int) { idxs[i], idxs[j] = idxs[j], idxs[i] })
for _, i := range idxs {
if err := r.RecordChunk(ctx, fmt.Sprintf("stream-%d", sid), fmt.Sprintf("chunk-%d", i), 10, now.Add(time.Second*10*time.Duration(i))); err != nil {
t.Fatal(err)
}
}
now = now.Add(time.Hour)
}
mc := newMetricsCollector(db)
lcts, err := mc.lastChunkTimes(ctx)
if err != nil {
log.Fatal(err)
}
if len(lcts) != 2 {
log.Printf("want 2 times, got: %d", len(lcts))
}
if !reflect.DeepEqual(lcts, map[string]time.Time{
"stream-1": now.Add(-2 * time.Hour).Add(20 * 10 * time.Second),
"stream-2": now.Add(-1 * time.Hour).Add(20 * 10 * time.Second),
}) {
t.Errorf("unexpected data: %#v", lcts)
}
}