-
Notifications
You must be signed in to change notification settings - Fork 7
/
time_test.go
179 lines (149 loc) · 3.1 KB
/
time_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package utility
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTimeJitter(t *testing.T) {
assert := assert.New(t)
for i := 0; i < 100; i++ {
t := JitterInterval(time.Second * 15)
assert.True(t >= 15*time.Second)
assert.True(t <= 30*time.Second)
}
}
func TestTimeRoundPartHour(t *testing.T) {
assert := assert.New(t)
// make sure the fixtures work:
for i := 0; i < 24; i++ {
assert.Equal(i, getTimeWithHour(i).Hour())
}
type timeParts struct {
expectedValue int
actualHours int
interval int
}
cases := []timeParts{
{0, 3, 6},
{0, 5, 6},
{0, 6, 10},
{0, 15, 13},
{2, 3, 2},
{4, 4, 2},
{4, 5, 2},
{5, 8, 5},
{5, 9, 5},
{6, 6, 6},
{6, 8, 6},
{10, 10, 5},
{10, 12, 10},
{10, 15, 10},
{10, 18, 10},
{12, 14, 6},
{15, 16, 5},
{18, 23, 6},
{0, 0, -1},
}
for _, c := range cases {
assert.Equal(c.expectedValue, findPartHour(getTimeWithHour(c.actualHours), c.interval).Hour(),
fmt.Sprintf("%+v", c))
}
assert.NotPanics(func() {
findPartHour(getTimeWithHour(0), 0)
})
}
func TestTimeRoundPartMinute(t *testing.T) {
assert := assert.New(t)
// make sure the fixtures work:
for i := 0; i < 60; i++ {
assert.Equal(i, getTimeWithMin(i).Minute())
}
type timeParts struct {
expectedValue int
actualMins int
interval int
}
cases := []timeParts{
{0, 12, 30},
{0, 13, 40},
{0, 48, 40},
{0, 27, 31},
{2, 3, 2},
{4, 4, 2},
{4, 5, 2},
{5, 8, 5},
{5, 9, 5},
{10, 10, 5},
{10, 12, 10},
{10, 15, 10},
{10, 18, 10},
{15, 16, 5},
{20, 20, 20},
{20, 27, 20},
{20, 28, 10},
{24, 25, 2},
{30, 48, 30},
{0, 0, -1},
}
for _, c := range cases {
assert.Equal(c.expectedValue, findPartMin(getTimeWithMin(c.actualMins), c.interval).Minute(),
fmt.Sprintf("%+v", c))
}
assert.NotPanics(func() {
findPartMin(getTimeWithMin(0), 0)
})
}
func TestTimeRoundPartSecond(t *testing.T) {
assert := assert.New(t)
// make sure the fixtures work:
for i := 0; i < 60; i++ {
assert.Equal(i, getTimeWithSec(i).Second())
}
type timeParts struct {
expectedValue int
actualSecs int
interval int
}
cases := []timeParts{
{0, 12, 30},
{0, 13, 40},
{0, 48, 40},
{0, 27, 31},
{2, 3, 2},
{4, 4, 2},
{4, 5, 2},
{5, 8, 5},
{5, 9, 5},
{10, 10, 5},
{10, 12, 10},
{10, 15, 10},
{10, 18, 10},
{15, 16, 5},
{20, 20, 20},
{20, 27, 20},
{20, 28, 10},
{24, 25, 2},
{30, 48, 30},
{0, 0, -1},
}
for _, c := range cases {
assert.Equal(c.expectedValue, findPartSec(getTimeWithSec(c.actualSecs), c.interval).Second(),
fmt.Sprintf("%+v", c))
}
assert.NotPanics(func() {
findPartSec(getTimeWithSec(0), 0)
})
}
func getTimeWithHour(hour int) time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, time.UTC)
}
func getTimeWithMin(min int) time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), min, 0, 0, time.UTC)
}
func getTimeWithSec(sec int) time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), sec, 0, time.UTC)
}