-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload-stat_test.go
80 lines (64 loc) · 2.29 KB
/
load-stat_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"reflect"
"testing"
"time"
)
func TestStatsBookkeeper_AddDoneActions(t *testing.T) {
s := statsBookkeeper{}
now := time.Now()
s.addDoneActions(23, now.Add(-time.Minute*16), now.Add(-time.Minute*14-time.Second*58))
assertDeepEqual(t, s.history, []doneActions{
{23, now.Add(-time.Minute * 16), now.Add(-time.Minute*14 - time.Second*58)},
})
time.Sleep(time.Second * 3)
s.addDoneActions(42, now.Add(-time.Minute*4), now.Add(-time.Minute*3))
assertDeepEqual(t, s.history, []doneActions{
{42, now.Add(-time.Minute * 4), now.Add(-time.Minute * 3)},
})
time.Sleep(time.Second * 1)
s.addDoneActions(777, now.Add(-time.Minute*2), now.Add(-time.Minute))
assertDeepEqual(t, s.history, []doneActions{
{42, now.Add(-time.Minute * 4), now.Add(-time.Minute * 3)},
{777, now.Add(-time.Minute * 2), now.Add(-time.Minute)},
})
}
func TestStatsBookkeeper_QueryLoad(t *testing.T) {
now := time.Now()
assertDeepEqual(t, (&statsBookkeeper{}).queryLoad(), [3]float64{0.0, 0.0, 0.0})
assertDeepEqual(t, (&statsBookkeeper{history: []doneActions{
{22, now.Add(-time.Minute * 17), now.Add(-time.Minute * 16)},
{23, now.Add(-time.Minute * 14), now.Add(-time.Minute * 6)},
{42, now.Add(-time.Minute * 4), now.Add(-time.Minute)},
{777, now.Add(-time.Second * 50), now.Add(-time.Second * 10)},
{778, now.Add(time.Minute), now.Add(time.Minute * 2)},
}}).queryLoad(), [3]float64{777.0, 777.0 + 42.0, 777.0 + 42.0 + 23.0})
{
load15 := (&statsBookkeeper{history: []doneActions{
{23, now.Add(-time.Minute * 24), now.Add(-time.Minute * 6)},
}}).queryLoad()[2]
if !(11 <= load15 && load15 <= 12) {
t.Errorf("11 <= %#v && %#v <= 12", load15, load15)
}
}
{
load15 := (&statsBookkeeper{history: []doneActions{
{23, now.Add(-time.Minute * 14), now.Add(time.Minute * 14)},
}}).queryLoad()[2]
if !(11 <= load15 && load15 <= 12) {
t.Errorf("11 <= %#v && %#v <= 12", load15, load15)
}
}
load15 := (&statsBookkeeper{history: []doneActions{
{23, now.Add(-time.Minute * 45 / 2), now.Add(time.Minute * 15 / 2)},
}}).queryLoad()[2]
if !(11 <= load15 && load15 <= 12) {
t.Errorf("11 <= %#v && %#v <= 12", load15, load15)
}
}
func assertDeepEqual(t *testing.T, a, b interface{}) {
t.Helper()
if !reflect.DeepEqual(a, b) {
t.Errorf("reflect.DeepEqual(%#v, %#v)", a, b)
}
}