-
Notifications
You must be signed in to change notification settings - Fork 9
/
schedule_test.go
135 lines (120 loc) · 3.01 KB
/
schedule_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
package flamingo
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestIntervalSchedule(t *testing.T) {
s := NewIntervalSchedule(4 * time.Second)
now := time.Now()
require.Equal(t, 4*time.Second, s.Next(now).Sub(now))
}
func TestTimeSchedule(t *testing.T) {
s := NewTimeSchedule(15, 30, 25)
now := time.Now()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
d := time.Date(now.Year(), now.Month(), now.Day(), 15, 30, 25, 0, now.Location())
require.Equal(t, d, s.Next(now))
now = time.Date(now.Year(), now.Month(), now.Day(), 16, 0, 0, 0, now.Location())
d = time.Date(now.Year(), now.Month(), now.Day()+1, 15, 30, 25, 0, now.Location())
require.Equal(t, d, s.Next(now))
}
func TestDayTimeSchedule(t *testing.T) {
s := NewDayTimeSchedule([]time.Weekday{
time.Wednesday,
time.Thursday,
time.Friday,
}, 15, 0, 0)
cases := []struct {
now time.Time
nextDay time.Weekday
}{
{
time.Date(2016, time.October, 3, 0, 0, 0, 0, time.Local), // monday
time.Wednesday,
},
{
time.Date(2016, time.October, 4, 0, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 5, 0, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 6, 0, 0, 0, 0, time.Local),
time.Thursday,
},
{
time.Date(2016, time.October, 7, 0, 0, 0, 0, time.Local),
time.Friday,
},
{
time.Date(2016, time.October, 8, 0, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 9, 0, 0, 0, 0, time.Local),
time.Wednesday,
},
}
for _, c := range cases {
nextTime := s.Next(c.now)
require.Equal(t, c.nextDay, nextTime.Weekday())
require.Equal(t, 15, nextTime.Hour())
require.Equal(t, 0, nextTime.Minute())
require.Equal(t, 0, nextTime.Second())
}
}
func TestDayTimeScheduleAfter(t *testing.T) {
s := NewDayTimeSchedule([]time.Weekday{
time.Wednesday,
time.Thursday,
time.Friday,
}, 15, 0, 0)
cases := []struct {
now time.Time
nextDay time.Weekday
}{
{
time.Date(2016, time.October, 3, 16, 0, 0, 0, time.Local), // monday
time.Wednesday,
},
{
time.Date(2016, time.October, 4, 16, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 5, 16, 0, 0, 0, time.Local),
time.Thursday,
},
{
time.Date(2016, time.October, 6, 16, 0, 0, 0, time.Local),
time.Friday,
},
{
time.Date(2016, time.October, 7, 16, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 8, 16, 0, 0, 0, time.Local),
time.Wednesday,
},
{
time.Date(2016, time.October, 9, 16, 0, 0, 0, time.Local),
time.Wednesday,
},
}
for i, c := range cases {
nextTime := s.Next(c.now)
require.Equal(t, c.nextDay, nextTime.Weekday(), fmt.Sprint(i))
require.Equal(t, 15, nextTime.Hour())
require.Equal(t, 0, nextTime.Minute())
require.Equal(t, 0, nextTime.Second())
}
}
func TestDayTimeScheduleNoDays(t *testing.T) {
s := NewDayTimeSchedule(nil, 15, 0, 0)
require.Equal(t, time.Time{}, s.Next(time.Now()))
}