1
1
// Tideland Go Library - Time Extensions - Unit Tests
2
2
//
3
- // Copyright (C) 2009-2015 Frank Mueller / Tideland / Oldenburg / Germany
3
+ // Copyright (C) 2009-2017 Frank Mueller / Tideland / Oldenburg / Germany
4
4
//
5
5
// All rights reserved. Use of this source code is governed
6
6
// by the new BSD license.
@@ -87,15 +87,20 @@ func TestCrontabKeep(t *testing.T) {
87
87
assert := audit .NewTestingAssertion (t , true )
88
88
89
89
// Create test crontab with job.
90
- c := timex .NewCrontab (10 * time .Millisecond )
91
- j := & cronjob {0 , false , false }
90
+ c := timex .NewCrontab (50 * time .Millisecond )
91
+ j := & cronjob {[]time. Time {} , false , false }
92
92
93
93
c .Add ("keep" , j )
94
- time .Sleep (50 * time .Millisecond )
94
+ time .Sleep (time .Second )
95
95
c .Remove ("keep" )
96
96
c .Stop ()
97
97
98
- assert .Equal (j .counter , 3 , "job counter increased twice" )
98
+ for i := range j .times {
99
+ if i > 0 {
100
+ d := j .times [i ].Sub (j .times [i - 1 ])
101
+ assert .True (d .Seconds () >= 0.05 )
102
+ }
103
+ }
99
104
}
100
105
101
106
// Test crontab removing the job.
@@ -104,14 +109,14 @@ func TestCrontabRemove(t *testing.T) {
104
109
105
110
// Create test crontab with job.
106
111
c := timex .NewCrontab (10 * time .Millisecond )
107
- j := & cronjob {0 , false , false }
112
+ j := & cronjob {[]time. Time {} , false , false }
108
113
109
114
c .Add ("remove" , j )
110
- time .Sleep (250 * time .Millisecond )
115
+ time .Sleep (500 * time .Millisecond )
111
116
c .Remove ("remove" )
112
117
c .Stop ()
113
118
114
- assert .Equal (j .counter , 10 , "job counter increased max ten times" )
119
+ assert .Length (j .times , 10 , "job counter increased max ten times" )
115
120
}
116
121
117
122
// Test crontab removing the job after an error.
@@ -120,14 +125,14 @@ func TestCrontabError(t *testing.T) {
120
125
121
126
// Create test crontab with job.
122
127
c := timex .NewCrontab (10 * time .Millisecond )
123
- j := & cronjob {0 , false , true }
128
+ j := & cronjob {[]time. Time {} , false , true }
124
129
125
130
c .Add ("remove" , j )
126
- time .Sleep (250 * time .Millisecond )
131
+ time .Sleep (500 * time .Millisecond )
127
132
c .Remove ("remove" )
128
133
c .Stop ()
129
134
130
- assert .Equal (j .counter , 5 , "job counter increased max five times" )
135
+ assert .Length (j .times , 5 , "job counter increased max five times" )
131
136
}
132
137
133
138
// TestRetrySuccess tests a successful retry.
@@ -190,9 +195,9 @@ func TestRetryTooOften(t *testing.T) {
190
195
//--------------------
191
196
192
197
type cronjob struct {
193
- counter int
194
- flip bool
195
- fail bool
198
+ times []time. Time
199
+ flip bool
200
+ fail bool
196
201
}
197
202
198
203
func (j * cronjob ) ShallExecute (t time.Time ) bool {
@@ -201,11 +206,11 @@ func (j *cronjob) ShallExecute(t time.Time) bool {
201
206
}
202
207
203
208
func (j * cronjob ) Execute () (bool , error ) {
204
- j .counter ++
205
- if j .fail && j . counter == 5 {
209
+ j .times = append ( j . times , time . Now ())
210
+ if j .fail && len ( j . times ) == 5 {
206
211
return false , errors .New ("failed" )
207
212
}
208
- if j . counter == 10 {
213
+ if len ( j . times ) == 10 {
209
214
return false , nil
210
215
}
211
216
return true , nil
0 commit comments