/
tasks_test.go
827 lines (743 loc) · 19.5 KB
/
tasks_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// Copyright 2013 Travis Keep. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or
// at http://opensource.org/licenses/BSD-3-Clause.
package tasks_test
import (
"errors"
"github.com/keep94/gofunctional3/functional"
"github.com/keep94/tasks"
"github.com/keep94/tasks/recurring"
"sync"
"testing"
"time"
)
var (
kNow = time.Date(2013, 9, 12, 17, 21, 0, 0, time.Local)
kSomeError = errors.New("tasks: some error")
)
func TestParallel(t *testing.T) {
testTasks := make([]tasks.Task, 20)
for i := range testTasks {
testTasks[i] = &fakeTask{}
}
e := tasks.Start(tasks.ParallelTasks(testTasks...))
<-e.Done()
// Blocking here is not necessary in production code. Just testing that
// this channel gets closed too.
<-e.Ended()
for _, atask := range testTasks {
ft := atask.(*fakeTask)
if !ft.hasRun() {
t.Error("Expected task to be run.")
}
}
}
func TestParallelEnded(t *testing.T) {
testTasks := make([]tasks.Task, 20)
for i := range testTasks {
testTasks[i] = &fakeTask{runDuration: time.Hour}
}
e := tasks.Start(tasks.ParallelTasks(testTasks...))
e.End()
<-e.Done()
for _, atask := range testTasks {
ft := atask.(*fakeTask)
if !ft.hasRun() {
t.Error("Expected task to be run.")
}
}
}
func TestParallelError(t *testing.T) {
testTasks := make([]tasks.Task, 20)
for i := range testTasks {
if i == 5 {
testTasks[i] = &fakeTask{err: kSomeError}
} else {
testTasks[i] = &fakeTask{}
}
}
e := tasks.Start(tasks.ParallelTasks(testTasks...))
<-e.Done()
if e.Error() != kSomeError {
t.Error("Expected to get an error.")
}
}
func TestParallelZeroOrOne(t *testing.T) {
task := &fakeTask{}
if tasks.ParallelTasks() != tasks.NilTask() {
t.Error("Expected zero parallel tasks to be nil task.")
}
if tasks.ParallelTasks(task) != task {
t.Error("Expected one parallel task to be that task.")
}
}
func TestSeries(t *testing.T) {
// three tasks
testTasks := make([]tasks.Task, 3)
// second task throws an error
for i := range testTasks {
testTasks[i] = &fakeTask{}
}
e := tasks.Start(tasks.SeriesTasks(testTasks...))
<-e.Done()
for i, atask := range testTasks {
ft := atask.(*fakeTask)
if !ft.hasRun() {
t.Errorf("Expected task %d to be run.", i)
}
}
}
func TestSeriesEnded(t *testing.T) {
// two tasks
testTasks := make([]tasks.Task, 2)
for i := range testTasks {
testTasks[i] = &fakeTask{runDuration: time.Hour}
}
e := tasks.Start(tasks.SeriesTasks(testTasks...))
e.End()
<-e.Done()
// 2nd task should not be reached.
for i, atask := range testTasks {
ft := atask.(*fakeTask)
if i < 1 {
if !ft.hasRun() {
t.Errorf("Expected task %d to be run.", i)
}
} else {
if ft.hasRun() {
t.Errorf("Expected task %d not to be run.", i)
}
}
}
}
func TestSeriesError(t *testing.T) {
// three tasks
testTasks := make([]tasks.Task, 3)
// second task throws an error
for i := range testTasks {
if i == 1 {
testTasks[i] = &fakeTask{err: kSomeError}
} else {
testTasks[i] = &fakeTask{}
}
}
e := tasks.Start(tasks.SeriesTasks(testTasks...))
<-e.Done()
// First 2 tasks should have been but not 3rd task
for i, atask := range testTasks {
ft := atask.(*fakeTask)
if i < 2 {
if !ft.hasRun() {
t.Errorf("Expected task %d to be run.", i)
}
} else {
if ft.hasRun() {
t.Errorf("Expected task %d not to be run.", i)
}
}
}
}
func TestSeriesZeroOrOne(t *testing.T) {
task := &fakeTask{}
if tasks.SeriesTasks() != tasks.NilTask() {
t.Error("Expected zero series tasks to be nil task.")
}
if tasks.SeriesTasks(task) != task {
t.Error("Expected one series task to be that task.")
}
}
func TestRepeatingTask(t *testing.T) {
verifyRepeatingTask(t, 5)
}
func TestRepeatingTask2(t *testing.T) {
verifyRepeatingTask(t, 1)
}
func TestRepeatingTask3(t *testing.T) {
verifyRepeatingTask(t, 0)
}
func TestNestedRepeatingTask(t *testing.T) {
task := &fakeTask{}
e := tasks.Start(tasks.RepeatingTask(
tasks.RepeatingTask(task, 2), 3))
<-e.Done()
if task.timesRun != 6 {
t.Errorf("Expected 6, got %v", task.timesRun)
}
}
func TestRepeatingTaskEnded(t *testing.T) {
task := &fakeTask{runDuration: time.Hour}
e := tasks.Start(tasks.RepeatingTask(task, 5))
e.End()
<-e.Done()
if task.timesRun != 1 {
t.Errorf("Expected 1, got %v", task.timesRun)
}
}
func TestRepeatingTaskError(t *testing.T) {
task := &fakeTask{err: kSomeError}
e := tasks.Start(tasks.RepeatingTask(task, 5))
<-e.Done()
if task.timesRun != 1 {
t.Errorf("Expected 1, got %v", task.timesRun)
}
}
func TestRepeatingZeroOrOne(t *testing.T) {
task := &fakeTask{}
if tasks.RepeatingTask(task, 0) != tasks.NilTask() {
t.Error("Expect zero repeating task to be nil task.")
}
if tasks.RepeatingTask(task, 1) != task {
t.Error("Expected one repeating task to be that task.")
}
}
func TestEndTask(t *testing.T) {
longTask := &fakeTask{runDuration: time.Hour}
e := tasks.Start(longTask)
if e.IsEnded() {
t.Error("Expected IsEnded() to be false.")
}
if e.IsDone() {
t.Error("Expected IsDone() to be false.")
}
e.End()
if !e.IsEnded() {
t.Error("Expected IsEnded() to be true.")
}
<-e.Done()
if !e.IsDone() {
t.Error("Expected IsDone() to be true.")
}
if !longTask.hasRun() {
t.Error("Expected task to be run.")
}
}
func TestNoError(t *testing.T) {
eTask := tasks.NilTask()
e := tasks.Start(eTask)
<-e.Done()
if e.Error() != nil {
t.Error("Expected no error.")
}
}
func TestNoError2(t *testing.T) {
eTask := tasks.NilTask()
if err := tasks.Run(eTask); err != nil {
t.Error("Expected no error.")
}
}
func TestError(t *testing.T) {
eTask := &fakeTask{err: kSomeError}
e := tasks.Start(eTask)
<-e.Done()
if e.Error() != kSomeError {
t.Error("Expected some error.")
}
}
func TestError2(t *testing.T) {
eTask := &fakeTask{err: kSomeError}
if err := tasks.Run(eTask); err != kSomeError {
t.Error("Expected some error.")
}
}
func TestRecurring(t *testing.T) {
timeTask := &fakeTask{}
r := recurring.Until(
recurring.AtInterval(kNow, time.Hour), kNow.Add(4*time.Hour))
tasks.RunForTesting(
tasks.RecurringTask(timeTask, r), &tasks.ClockForTesting{kNow})
verifyTimes(
t, timeTask.timeStamps,
kNow.Add(time.Hour),
kNow.Add(2*time.Hour),
kNow.Add(3*time.Hour))
}
func TestRecurringCloseStream(t *testing.T) {
task := tasks.NilTask()
r := &testForClose{}
tasks.RunForTesting(
tasks.RecurringTask(task, r), &tasks.ClockForTesting{kNow})
if !r.closeCalled {
t.Error("Expected close to be called.")
}
}
func TestRecurringEnded(t *testing.T) {
tk := tasks.NilTask()
r := recurring.AtInterval(kNow, time.Hour)
e := tasks.Start(tasks.RecurringTask(tk, r))
e.End()
<-e.Done()
}
func TestRecurringOverrun(t *testing.T) {
timeTask := &fakeTask{runDuration: time.Hour}
r := recurring.Until(
recurring.AtInterval(kNow, time.Hour), kNow.Add(5*time.Hour))
tasks.RunForTesting(
tasks.RecurringTask(timeTask, r), &tasks.ClockForTesting{kNow})
verifyTimes(
t, timeTask.timeStamps, kNow.Add(time.Hour), kNow.Add(3*time.Hour))
}
func TestRecurringError(t *testing.T) {
timeTask := &fakeTask{err: kSomeError}
r := recurring.AtInterval(kNow, time.Hour)
tasks.RunForTesting(
tasks.RecurringTask(timeTask, r), &tasks.ClockForTesting{kNow})
verifyTimes(
t, timeTask.timeStamps,
kNow.Add(time.Hour))
}
func TestSingleExecutorStart(t *testing.T) {
task1 := &fakeTask{runDuration: time.Millisecond}
task2 := &fakeTask{runDuration: time.Millisecond}
task3 := &fakeTask{runDuration: time.Millisecond}
se := tasks.NewSingleExecutor()
defer se.Close()
e := se.Start(task1)
if tk, ex := se.Current(); tk.(*fakeTask) != task1 || ex != e {
t.Error("Expect Current to be task 1.")
}
<-e.Done()
e = se.Start(task2)
if tk, ex := se.Current(); tk.(*fakeTask) != task2 || ex != e {
t.Error("Expect Current to be task 2.")
}
<-e.Done()
e = se.Start(task3)
if tk, ex := se.Current(); tk.(*fakeTask) != task3 || ex != e {
t.Error("Expect Current to be task 3.")
}
<-e.Done()
if tk, ex := se.Current(); tk != nil || ex != nil {
t.Error("Expected current task and execution to be nil.")
}
if !task1.hasRun() || !task2.hasRun() || !task3.hasRun() {
t.Error("All three tasks should have run.")
}
}
func TestSingleExecutorForceStart(t *testing.T) {
task1 := &fakeTask{runDuration: time.Hour}
task2 := &fakeTask{runDuration: time.Hour}
task3 := &fakeTask{runDuration: time.Hour}
se := tasks.NewSingleExecutor()
defer se.Close()
e1 := se.Start(task1)
e2 := se.Start(task2)
e3 := se.Start(task3)
e3.End()
<-e1.Done()
<-e2.Done()
<-e3.Done()
if !task1.hasRun() || !task2.hasRun() || !task3.hasRun() {
t.Error("All three tasks should have run.")
}
}
func TestSingleExecutorMultiThread(t *testing.T) {
fakeTasks := make([]*fakeTask, 20)
for i := range fakeTasks {
fakeTasks[i] = &fakeTask{}
}
var wg sync.WaitGroup
wg.Add(len(fakeTasks))
se := tasks.NewSingleExecutor()
defer se.Close()
for i := range fakeTasks {
go func(t tasks.Task) {
e := se.Start(t)
<-e.Done()
wg.Done()
}(fakeTasks[i])
}
wg.Wait()
for i := range fakeTasks {
if fakeTasks[i].timesRun != 1 {
t.Error("Expected each task to be run exactly once.")
}
}
}
func TestSingleExecutorClose(t *testing.T) {
task1 := &fakeTask{runDuration: time.Hour}
se := tasks.NewSingleExecutor()
e := se.Start(task1)
se.Close()
<-e.Done()
}
func TestSingleExecutorPause(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &fakeTask2{runDuration: time.Hour, Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
se.Start(task1)
waitForStarts(starting, 1)
se.Pause()
}
func TestPauseNotSupported(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
e := se.Start(task1)
waitForStarts(starting, 1)
// Since this task doesn't support pause, this Pause() call won't return
// until the task is finished.
se.Pause()
if out := task1.Count(); out != 1 {
t.Errorf("Expected 1, got %d", out)
}
// A paused task can still end on its own. The only
// guarantee is that the paused task won't do any additional work.
<-e.Done()
}
func TestPauseNotSupportedParallel(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &pauseTask{Starting: starting}
ts := make([]tasks.Task, 20)
for i := range ts {
ts[i] = task1
}
se := tasks.NewSingleExecutor()
defer se.Close()
e := se.Start(&taskStruct{tasks.ParallelTasks(ts...)})
waitForStarts(starting, len(ts))
// Since these parallel tasks don't support pause, this Pause() call
// won't return until all the tasks have finished.
se.Pause()
if out := task1.Count(); out != len(ts) {
t.Errorf("Expected %d, got %d", len(ts), out)
}
// A paused task can still end on its own. The only
// guarantee is that the paused task won't do any additional work.
<-e.Done()
}
func TestPauseNotSupportedPauseEarly(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
se.Start(task1)
se.Pause()
// Acknowledge if the task completed while pausing
waitForStarts(starting, task1.Count())
time.Sleep(time.Millisecond)
// No new tasks should start
assertNoStarting(t, starting)
}
func TestPauseNotSupportedParallelPauseEarly(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &pauseTask{Starting: starting}
ts := make([]tasks.Task, 20)
for i := range ts {
ts[i] = task1
}
se := tasks.NewSingleExecutor()
defer se.Close()
se.Start(&taskStruct{tasks.ParallelTasks(ts...)})
se.Pause()
// Acknowledge the tasks that completed while pausing
waitForStarts(starting, task1.Count())
time.Sleep(time.Millisecond)
// No new tasks should start
assertNoStarting(t, starting)
}
func TestPauseNoTasks(t *testing.T) {
se := tasks.NewSingleExecutor()
defer se.Close()
se.Pause()
se.Resume()
}
func TestInterruptWhilePaused(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task1 := &fakeTask2{runDuration: time.Hour, Starting: starting}
task2 := &fakeTask2{runDuration: time.Hour, Starting: starting}
task3 := &fakeTask2{runDuration: time.Hour, Starting: starting}
se := tasks.NewSingleExecutor()
e1 := se.Start(task1)
waitForStarts(starting, 1)
se.Pause()
ctask, _ := se.Current()
if ctask != task1 {
t.Error("Expect current task to be task1")
}
expectFalse(t, "e1.IsDone()", e1.IsDone())
e2 := se.Start(task2)
expectTrue(t, "e1.IsDone()", e1.IsDone())
ctask, _ = se.Current()
if ctask != task2 {
t.Error("Expect current task to be task2")
}
time.Sleep(time.Millisecond)
assertNoStarting(t, starting)
expectFalse(t, "e2.IsDone()", e2.IsDone())
e3 := se.Start(task3)
expectTrue(t, "e2.IsDone()", e2.IsDone())
ctask, _ = se.Current()
if ctask != task3 {
t.Error("Expect current task to be task3")
}
time.Sleep(time.Millisecond)
assertNoStarting(t, starting)
expectFalse(t, "e3.IsDone()", e3.IsDone())
se.Close()
expectTrue(t, "e3.IsDone()", e3.IsDone())
}
func TestPauseParallel(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
ts := make([]tasks.Task, 20)
for i := range ts {
ts[i] = tasks.RepeatingTask(task, 2147483647)
}
// Tasks in executor must support equality
e := se.Start(&taskStruct{tasks.ParallelTasks(ts...)})
waitForStarts(starting, len(ts))
se.Pause()
expected := len(ts)
if out := task.Count(); out != expected {
t.Errorf("Expected count of %d, got %d", expected, out)
expected = out
}
time.Sleep(5 * time.Millisecond)
assertNoStarting(t, starting)
se.Resume()
waitForStarts(starting, len(ts))
se.Pause()
// no-op
se.Pause()
expected += len(ts)
if out := task.Count(); out != expected {
t.Errorf("Expected count of %d, got %d", expected, out)
expected = out
}
time.Sleep(5 * time.Millisecond)
assertNoStarting(t, starting)
e.End()
<-e.Done()
}
func TestPauseSeries(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
ts := make([]tasks.Task, 20)
for i := range ts {
ts[i] = tasks.RepeatingTask(task, 2147483647)
}
// Tasks in executor must support equality
e := se.Start(&taskStruct{tasks.SeriesTasks(ts...)})
waitForStarts(starting, 1)
se.Pause()
expected := 1
if out := task.Count(); out != expected {
t.Errorf("Expected count of %d, got %d", expected, out)
expected = out
}
time.Sleep(5 * time.Millisecond)
assertNoStarting(t, starting)
se.Resume()
se.Resume()
waitForStarts(starting, 1)
se.Pause()
expected += 1
if out := task.Count(); out != expected {
t.Errorf("Expected count of %d, got %d", expected, out)
expected = out
}
time.Sleep(5 * time.Millisecond)
assertNoStarting(t, starting)
se.Resume()
e.End()
<-e.Done()
}
func TestPauseSeriesItself(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
// Tasks in executor must support equality
e := se.Start(&taskStruct{tasks.SeriesTasks(task, task)})
waitForStarts(starting, 1)
se.Pause()
select {
case <-e.Done():
t.Error("Pause not working")
case <-time.After(10 * time.Millisecond):
}
e.End()
<-e.Done()
}
func TestSingleExecutorPauseFromBeginning(t *testing.T) {
starting := make(chan bool, 100)
defer close(starting)
task := &pauseTask{Starting: starting}
se := tasks.NewSingleExecutor()
defer se.Close()
se.Pause()
e := se.Start(&taskStruct{tasks.RepeatingTask(task, 2147483647)})
time.Sleep(5 * time.Millisecond)
assertNoStarting(t, starting)
se.Resume()
waitForStarts(starting, 1)
se.Pause()
expected := 1
if out := task.Count(); out != expected {
t.Errorf("Expected count of %d, got %d", expected, out)
expected = out
}
e.End()
<-e.Done()
}
func TestFakeClock(t *testing.T) {
fakeClock := tasks.NewFakeClock(kNow)
// Read from this to be sure all goroutines are blocked on After
startChannel := make(chan bool, 6)
// Read from this to acknowledge finished goroutines
doneChannel := make(chan bool, 6)
routine := func(blockDuration time.Duration, etime time.Time) {
ch := fakeClock.After(blockDuration)
startChannel <- true
ctime := <-ch
if ctime != etime {
t.Errorf("Expected time %v, got %v", etime, ctime)
}
doneChannel <- true
}
// This one quits immediately
go routine(0, kNow)
// These will have quit after one minute
go routine(45*time.Second, kNow.Add(time.Minute))
go routine(time.Minute, kNow.Add(time.Minute))
// These will have quit after 3 minutes
go routine(2*time.Minute, kNow.Add(3*time.Minute))
go routine(2*time.Minute, kNow.Add(3*time.Minute))
go routine(3*time.Minute, kNow.Add(3*time.Minute))
// Be sure goroutines are blocked on After.
for i := 0; i < 6; i++ {
<-startChannel
}
// First goroutine will be done
<-doneChannel
// Advance by 1 minute this should unblock the next two goroutines
fakeClock.Advance(time.Minute)
<-doneChannel
<-doneChannel
// Advance by 2 more minutes this should unblock the rest
fakeClock.Advance(2 * time.Minute)
<-doneChannel
<-doneChannel
<-doneChannel
}
type pauseTask struct {
Starting chan bool
count int // Number of completed runs.
lock sync.Mutex
}
func (pt *pauseTask) Do(e *tasks.Execution) {
pt.Starting <- true
time.Sleep(time.Millisecond)
pt.add(1)
}
func (pt *pauseTask) add(x int) {
pt.lock.Lock()
defer pt.lock.Unlock()
pt.count += x
}
func (pt *pauseTask) Count() int {
pt.lock.Lock()
defer pt.lock.Unlock()
return pt.count
}
type fakeTask2 struct {
runDuration time.Duration // How long task should take to run.
Starting chan bool
}
func (ft *fakeTask2) Do(e *tasks.Execution) {
ft.Starting <- true
e.Sleep(ft.runDuration)
}
type fakeTask struct {
runDuration time.Duration // How long task should take to run.
err error // the error task is to report.
message string // arbitrary string
timeStamps []time.Time // times when task was started
timesRun int // Number of completed runs.
}
func (ft *fakeTask) Do(e *tasks.Execution) {
ft.timeStamps = append(ft.timeStamps, e.Now())
if ft.err != nil {
e.SetError(ft.err)
}
if ft.runDuration > 0 {
e.Sleep(ft.runDuration)
}
ft.timesRun++
}
func (ft *fakeTask) hasRun() bool {
return ft.timesRun > 0
}
type testForClose struct {
closeCalled bool
}
func (r *testForClose) ForTime(t time.Time) functional.Stream {
return r
}
func (r *testForClose) Next(ptr interface{}) error {
return functional.Done
}
func (r *testForClose) Close() error {
r.closeCalled = true
return nil
}
type taskStruct struct {
tasks.Task
}
func verifyTimes(t *testing.T, actual []time.Time, expected ...time.Time) {
if len(actual) != len(expected) {
t.Errorf("Expected %v timestamps, got %v", len(expected), len(actual))
return
}
for i := range expected {
if expected[i] != actual[i] {
t.Errorf("Expected time %v at %d, got %v", expected[i], i, actual[i])
}
}
}
func verifyRepeatingTask(t *testing.T, n int) {
task := &fakeTask{}
e := tasks.Start(tasks.RepeatingTask(task, n))
<-e.Done()
if task.timesRun != n {
t.Errorf("Expected %d, got %d", n, task.timesRun)
}
}
func expectFalse(t *testing.T, desc string, val bool) {
if val {
t.Errorf("Expected %s to be false.", desc)
}
}
func expectTrue(t *testing.T, desc string, val bool) {
if !val {
t.Errorf("Expected %s to be true.", desc)
}
}
func assertNoStarting(t *testing.T, starting <-chan bool) {
select {
case <-starting:
t.Fatal("Expected no one starting")
default:
}
}
func waitForStarts(starting <-chan bool, count int) {
for i := 0; i < count; i++ {
<-starting
}
}