-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron_test.go
226 lines (198 loc) · 5.91 KB
/
cron_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package gogadgets_test
import (
"errors"
"math/rand"
"time"
"github.com/cswank/gogadgets"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type fakeAfter struct {
t time.Time
d time.Duration
c chan time.Time
}
func (f *fakeAfter) After(d time.Duration) <-chan time.Time {
f.d = d
f.c = make(chan time.Time)
go f.start()
return f.c
}
func (f *fakeAfter) start() {
time.Sleep(f.d)
f.c <- f.t
}
func init() {
rand.Seed(time.Now().Unix())
}
var _ = Describe("Cron", func() {
var (
out chan gogadgets.Message
in chan gogadgets.Message
c *gogadgets.Cron
fa *fakeAfter
jobs []string
start func() *gogadgets.Message
cronErr error
)
BeforeEach(func() {
cronErr = nil
fa = &fakeAfter{
t: time.Date(2015, 9, 4, 13, 25, 0, 0, time.UTC),
}
jobs = []string{
"25 13 * * * turn on living room light",
"25 14 * * * turn on living room light",
}
out = make(chan gogadgets.Message)
in = make(chan gogadgets.Message)
start = func() *gogadgets.Message {
var err error
c, err = gogadgets.NewCron(
nil,
gogadgets.CronAfter(fa.After),
gogadgets.CronJobs(jobs),
gogadgets.CronSleep(time.Millisecond),
)
var msg *gogadgets.Message
if cronErr != nil {
Expect(err).To(MatchError(cronErr))
} else {
Expect(err).To(BeNil())
go c.Start(out, in)
select {
case m := <-in:
msg = &m
case <-time.After(100 * time.Millisecond):
return nil
}
}
return msg
}
})
Describe("bunk cron jobs", func() {
It("returns an error when there isn't enough stuff in the job", func() {
jobs = []string{"25 start"}
cronErr = errors.New("could not parse job: 25 start")
start()
})
It("returns an error when the numbers aren't numbers", func() {
jobs = []string{"25 * b * * start"}
cronErr = errors.New("could not parse job: 25 * b * * start")
start()
})
})
Describe("when all's good", func() {
It("sends a command when it's time", func() {
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is a weekday specified", func() {
jobs = []string{"25 13 * * 5 turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("does not send a command when the weekday is wrong", func() {
jobs = []string{"25 13 * * 6 turn on living room light"}
msg := start()
Expect(msg).To(BeNil())
})
It("sends a command when there is a range of weekdays specified", func() {
jobs = []string{"25 13 * * 4-6 turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there are several weekdays specified", func() {
jobs = []string{"25 13 * * 2,5,6 turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is lots of extra space", func() {
jobs = []string{
"25 13 * * * turn on living room light",
"25 14 * * * turn on living room light",
}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there are tabs and space", func() {
jobs = []string{
"25 13 * * * turn on living room light",
}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there are just tabs", func() {
jobs = []string{
"25 13 * * * turn on living room light",
}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is a range of minutes", func() {
jobs = []string{"22-26 13 * * * turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is a range of hours", func() {
jobs = []string{"25 12-15 * * * turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is a series of minutes", func() {
jobs = []string{"22,25,28 13 * * * turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("sends a command when there is a series of hours", func() {
jobs = []string{"25 1,13 * * * turn on living room light"}
msg := start()
Expect(msg).ToNot(BeNil())
Expect(msg.Body).To(Equal("turn on living room light"))
Expect(msg.Sender).To(Equal("cron"))
})
It("does not send a command when it's not time", func() {
jobs = []string{"25 14 * * * turn on living room light"}
msg := start()
Expect(msg).To(BeNil())
})
It("does not send a command when the line is commented out", func() {
jobs = []string{"#25 14 * * * turn on living room light"}
msg := start()
Expect(msg).To(BeNil())
})
It("sends a command when there is a range of hours and minutes", func() {
//This doesn't work
// jobs = []string{"24-28 12-15 * * * turn on living room light"}
// c = &gogadgets.Cron{
// After: fa.After,
// Jobs: jobs,
// Sleep: time.Millisecond,
// }
// go c.Start(out, in)
// msg := <-in
// Expect(msg.Body).To(Equal("turn on living room light"))
// Expect(msg.Sender).To(Equal("cron"))
})
})
})