-
Notifications
You must be signed in to change notification settings - Fork 0
/
icy_test.go
57 lines (54 loc) · 1.31 KB
/
icy_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
package main
import (
"testing"
"time"
)
func TestCalcSleep(t *testing.T) {
now := time.Now()
nowFn = func() time.Time { return now }
defer func() { nowFn = time.Now }()
for _, tc := range []struct {
Name string
StartTime time.Time
ServedTime time.Duration
WantSleep time.Duration
}{
{
Name: "Behind by 30 seconds",
StartTime: now.Add(-1 * time.Minute),
ServedTime: 30 * time.Second,
WantSleep: 1 * time.Nanosecond,
},
{
Name: "On time",
StartTime: now.Add(-1 * time.Minute),
ServedTime: 1*time.Minute + 30*time.Second,
WantSleep: 1 * time.Nanosecond,
},
{
Name: "40 seconds ahead",
StartTime: now.Add(-1 * time.Minute),
ServedTime: 1*time.Minute + 40*time.Second,
WantSleep: 10 * time.Second,
},
{
Name: "50 seconds ahead",
StartTime: now.Add(-1 * time.Hour),
ServedTime: 1*time.Hour + 50*time.Second,
WantSleep: 20 * time.Second,
},
{
Name: "Behind by 1 minute",
StartTime: now.Add(-1 * time.Hour),
ServedTime: 1*time.Hour - 1*time.Minute - 30*time.Second,
WantSleep: 1 * time.Nanosecond,
},
} {
t.Run(tc.Name, func(t *testing.T) {
got := calculateIcySleep(tc.StartTime, tc.ServedTime)
if got != tc.WantSleep {
t.Errorf("want sleep %s, got: %s", tc.WantSleep, got)
}
})
}
}