@@ -26,6 +26,25 @@ func TestDefaultTaskScheduler(t *testing.T) {
26
26
"number of scheduled task execution must be 1, actual: %d" , counter )
27
27
}
28
28
29
+ func TestDefaultTaskSchedulerWithTimeOption (t * testing.T ) {
30
+ scheduler := NewDefaultTaskScheduler ()
31
+
32
+ var counter int32
33
+ now := time .Now ()
34
+ starTime := now .Add (time .Second * 1 )
35
+
36
+ task , err := scheduler .Schedule (func (ctx context.Context ) {
37
+ atomic .AddInt32 (& counter , 1 )
38
+ }, WithTime (starTime ))
39
+
40
+ assert .Nil (t , err )
41
+
42
+ <- time .After (2 * time .Second )
43
+ assert .True (t , task .IsCancelled (), "scheduled task must have been cancelled" )
44
+ assert .True (t , counter == 1 ,
45
+ "number of scheduled task execution must be 1, actual: %d" , counter )
46
+ }
47
+
29
48
func TestSimpleTaskScheduler_ScheduleWithoutTask (t * testing.T ) {
30
49
scheduler := NewDefaultTaskScheduler ()
31
50
task , err := scheduler .Schedule (nil )
@@ -81,7 +100,26 @@ func TestSimpleTaskScheduler_WithoutScheduledExecutor(t *testing.T) {
81
100
"number of scheduled task execution must be 1, actual: %d" , counter )
82
101
}
83
102
84
- func TestSimpleTaskScheduler_Schedule_OneShotTask (t * testing.T ) {
103
+ func TestSimpleTaskScheduler_WithoutScheduledExecutorWithTimeOption (t * testing.T ) {
104
+ scheduler := NewSimpleTaskScheduler (nil )
105
+
106
+ var counter int32
107
+ now := time .Now ()
108
+ startTime := now .Add (time .Second * 1 )
109
+
110
+ task , err := scheduler .Schedule (func (ctx context.Context ) {
111
+ atomic .AddInt32 (& counter , 1 )
112
+ }, WithTime (startTime ))
113
+
114
+ assert .Nil (t , err )
115
+
116
+ <- time .After (2 * time .Second )
117
+ assert .True (t , task .IsCancelled (), "scheduled task must have been cancelled" )
118
+ assert .True (t , counter == 1 ,
119
+ "number of scheduled task execution must be 1, actual: %d" , counter )
120
+ }
121
+
122
+ func TestSimpleTaskScheduler_ScheduleOneShotTaskWithStartTimeOption (t * testing.T ) {
85
123
scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
86
124
87
125
var counter int32
@@ -99,6 +137,25 @@ func TestSimpleTaskScheduler_Schedule_OneShotTask(t *testing.T) {
99
137
"number of scheduled task execution must be 1, actual: %d" , counter )
100
138
}
101
139
140
+ func TestSimpleTaskScheduler_ScheduleOneShotTaskWithTimeOption (t * testing.T ) {
141
+ scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
142
+
143
+ var counter int32
144
+ now := time .Now ()
145
+ startTime := now .Add (time .Second * 1 )
146
+
147
+ task , err := scheduler .Schedule (func (ctx context.Context ) {
148
+ atomic .AddInt32 (& counter , 1 )
149
+ }, WithTime (startTime ))
150
+
151
+ assert .Nil (t , err )
152
+
153
+ <- time .After (2 * time .Second )
154
+ assert .True (t , task .IsCancelled (), "scheduled task must have been cancelled" )
155
+ assert .True (t , counter == 1 ,
156
+ "number of scheduled task execution must be 1, actual: %d" , counter )
157
+ }
158
+
102
159
func TestSimpleTaskScheduler_ScheduleWithFixedDelay (t * testing.T ) {
103
160
scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
104
161
@@ -136,6 +193,26 @@ func TestSimpleTaskScheduler_ScheduleWithFixedDelayWithStartTimeOption(t *testin
136
193
"number of scheduled task execution must be between 1 and 3, actual: %d" , counter )
137
194
}
138
195
196
+ func TestSimpleTaskScheduler_ScheduleWithFixedDelayWithTimeOption (t * testing.T ) {
197
+ scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
198
+
199
+ var counter int32
200
+ now := time .Now ()
201
+ startTime := now .Add (time .Second * 1 )
202
+
203
+ task , err := scheduler .ScheduleWithFixedDelay (func (ctx context.Context ) {
204
+ atomic .AddInt32 (& counter , 1 )
205
+ <- time .After (500 * time .Millisecond )
206
+ }, 200 * time .Millisecond , WithTime (startTime ))
207
+
208
+ assert .Nil (t , err )
209
+
210
+ <- time .After (2 * time .Second + 500 * time .Millisecond )
211
+ task .Cancel ()
212
+ assert .True (t , counter >= 1 && counter <= 3 ,
213
+ "number of scheduled task execution must be between 1 and 3, actual: %d" , counter )
214
+ }
215
+
139
216
func TestSimpleTaskScheduler_ScheduleAtFixedRate (t * testing.T ) {
140
217
scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
141
218
@@ -172,6 +249,26 @@ func TestSimpleTaskScheduler_ScheduleAtFixedRateWithStartTimeOption(t *testing.T
172
249
"number of scheduled task execution must be between 5 and 10, actual: %d" , counter )
173
250
}
174
251
252
+ func TestSimpleTaskScheduler_ScheduleAtFixedRateWithTimeOption (t * testing.T ) {
253
+ scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
254
+
255
+ var counter int32
256
+ now := time .Now ()
257
+ startTime := now .Add (time .Second * 1 )
258
+
259
+ task , err := scheduler .ScheduleAtFixedRate (func (ctx context.Context ) {
260
+ atomic .AddInt32 (& counter , 1 )
261
+ <- time .After (500 * time .Millisecond )
262
+ }, 200 * time .Millisecond , WithTime (startTime ))
263
+
264
+ assert .Nil (t , err )
265
+
266
+ <- time .After (3 * time .Second - 50 * time .Millisecond )
267
+ task .Cancel ()
268
+ assert .True (t , counter >= 5 && counter <= 10 ,
269
+ "number of scheduled task execution must be between 5 and 10, actual: %d" , counter )
270
+ }
271
+
175
272
func TestSimpleTaskScheduler_ScheduleWithCron (t * testing.T ) {
176
273
scheduler := NewSimpleTaskScheduler (NewDefaultTaskExecutor ())
177
274
0 commit comments