-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan_test.go
88 lines (71 loc) · 1.91 KB
/
plan_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
81
82
83
84
85
86
87
88
package powernap
import (
"math"
"sort"
"testing"
"time"
)
var planTests = []time.Duration{
100 * time.Millisecond,
100 * time.Millisecond,
200 * time.Millisecond,
300 * time.Millisecond,
400 * time.Millisecond,
500 * time.Millisecond,
314 * time.Millisecond,
201 * time.Millisecond,
}
func checkPlanResult(t *testing.T, plan *Plan, res PlanResult) {
t.Helper()
scheduleTotal := 0
for _, s := range plan.schedule {
scheduleTotal += len(s)
}
if scheduleTotal != len(planTests) {
t.Fatalf("number of scheduled items: %d, doesn't match number of tests: %d", scheduleTotal, len(planTests))
}
if len(res.ExecTimes) != len(planTests) {
t.Fatalf("number of recorded executions: %d, doesn't match number of tests: %d", len(res.ExecTimes), len(planTests))
}
sort.Slice(planTests, func(i, j int) bool { return planTests[i] < planTests[j] })
for i, et := range res.ExecTimes {
expected := res.Start.Add(planTests[i])
diff := math.Abs(float64(et.Sub(expected)))
diffRatio := diff / float64(planTests[i])
if diffRatio > 0.01 {
t.Fatalf("expected an error rate of less than 1%%, got: %0.2f", diffRatio*100)
}
}
}
func TestPlan(t *testing.T) {
plan := NewPlan()
for _, d := range planTests {
plan.Schedule(d, func() { /* do nothing */ })
}
res := <-plan.Start()
checkPlanResult(t, plan, res)
}
func TestPlanTight(t *testing.T) {
plan := NewPlan()
for _, d := range planTests {
plan.Schedule(d, func() { /* do nothing */ })
}
res := <-plan.StartTight()
checkPlanResult(t, plan, res)
}
func TestPlanBlocking(t *testing.T) {
plan := NewPlan()
for _, d := range planTests {
plan.Schedule(d, func() { /* do nothing */ })
}
res := plan.StartBlocking()
checkPlanResult(t, plan, res)
}
func TestPlanTightBlocking(t *testing.T) {
plan := NewPlan()
for _, d := range planTests {
plan.Schedule(d, func() { /* do nothing */ })
}
res := plan.StartTightBlocking()
checkPlanResult(t, plan, res)
}