-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker_test.go
1088 lines (888 loc) · 33 KB
/
worker_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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ** goworkerpool.com **********************************************************************************************
// ** github.com/enriquebris/goworkerpool **
// ** v0.10.0 *******************************************************************************************************
package goworkerpool
import (
"testing"
"time"
"github.com/enriquebris/goconcurrentcounter"
"github.com/stretchr/testify/suite"
)
const (
workerID = 100
)
type WorkerTestSuite struct {
suite.Suite
wkr *worker
}
func (suite *WorkerTestSuite) SetupTest() {
suite.wkr = newWorker(workerID)
}
// ***************************************************************************************
// ** Initialization
// ***************************************************************************************
// initialization values
func (suite *WorkerTestSuite) TestInitialization() {
// GetID
suite.Equal(workerID, suite.wkr.GetID())
// channels should be empty and not closed
// tasks channel
select {
case <-suite.wkr.GetTasksChannel():
suite.FailNow("tasks channel should be empty and not closed just after initialization")
default:
}
// actions channel
select {
case <-suite.wkr.GetActionsChannel():
suite.FailNow("actions channel should be empty and not closed just after initialization")
default:
}
// nil ExternalTaskSuccesses at initialization
suite.Nil(suite.wkr.GetExternalTaskSuccesses())
// no external function for actions
suite.Equal(0, len(suite.wkr.externalActions))
// no external function for action
suite.Equal(0, len(suite.wkr.externalActions))
// external feedback function
suite.Nil(suite.wkr.externalFeedbackFunc)
// external preAction func
suite.Nil(suite.wkr.externalPreActionFunc)
// external postAction func
suite.Nil(suite.wkr.externalPostActionFunc)
// external preTask func
suite.Nil(suite.wkr.externalPreTaskFunc)
// external postTask func
suite.Nil(suite.wkr.externalPostTaskFunc)
// error channel
suite.Nil(suite.wkr.GetErrorChannel())
// metrics
// zero tasks successes
suite.Equal(0, suite.wkr.GetTaskSuccesses())
// zero tasks failures
suite.Equal(0, suite.wkr.GetTaskFailures())
// closed
suite.False(suite.wkr.getStatus(WorkerClosed))
}
// verifies: setupInternalFunctionOperations adds internal functions for actionKillWorker && actionLateKillWorker and
// both functions return false
func (suite *WorkerTestSuite) TestSetupInternalFunctionOperations() {
// 2 internal function for actions
suite.Equal(2, len(suite.wkr.internalActions))
// actionKillWorker
f1, ok1 := suite.wkr.internalActions[actionKillWorker]
suite.True(ok1)
// f1 returns false
suite.False(f1())
// actionLateKillWorker
f2, ok2 := suite.wkr.internalActions[actionLateKillWorker]
suite.True(ok2)
// f2 returns false
suite.False(f2())
}
// ***************************************************************************************
// ** GetTasksChannel && GetActionsChannel
// ***************************************************************************************
func (suite *WorkerTestSuite) TestGetTasksChannel() {
suite.NotNil(suite.wkr.GetTasksChannel(), "task channel should not be nil")
}
func (suite *WorkerTestSuite) TestGetActionsChannel() {
suite.NotNil(suite.wkr.GetActionsChannel(), "actions channel should not be nil")
}
// ***************************************************************************************
// ** ErrorChannel
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetErrorChannel() {
var errorChan chan *PoolError
suite.wkr.SetErrorChannel(errorChan)
suite.Equal(errorChan, suite.wkr.GetErrorChannel())
}
// ***************************************************************************************
// ** Close
// ***************************************************************************************
// Close() while Listen not running
func (suite *WorkerTestSuite) TestCloseWithListenNotInProgress() {
suite.False(suite.wkr.getStatus(ListenInProgress))
// close all channels
suite.wkr.Close()
suite.checkClosedChannels()
suite.False(suite.wkr.getStatus(ListenInProgress))
}
// Close() while Listen is running
func (suite *WorkerTestSuite) TestCloseWithListenInProgress() {
suite.wkr.Listen()
listenIsUp := make(chan struct{})
// send a dummy task to be executed by the worker, once the task is processed we
// know the Listen is up
suite.NoError(suite.wkr.ProcessTask(&task{
Code: taskRegular,
Data: nil,
Func: nil,
CallbackFunc: func(data interface{}) {
listenIsUp <- struct{}{}
},
Valid: true,
}))
// wait until the task is processed by Listen, meaning Listen is up
select {
case <-listenIsUp:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for task processing")
}
// close the worker
listenerDoneChan := suite.wkr.Close()
// all channels should be closed
suite.checkClosedChannels()
// wait until Listen is not longer listening
<-listenerDoneChan
// Listen is not running
suite.False(suite.wkr.getStatus(ListenInProgress))
suite.True(suite.wkr.Closed())
}
// checkClosedChannels verifies that channels are closed
func (suite *WorkerTestSuite) checkClosedChannels() {
// close channel
select {
case _, ok := <-suite.wkr.closeChan:
suite.False(ok, "close channel should be closed")
default:
suite.FailNow("unexpected error")
}
// tasks channel
select {
case _, ok := <-suite.wkr.GetTasksChannel():
suite.False(ok, "tasks channel should be closed")
default:
suite.FailNow("unexpected error")
}
// actions channel
select {
case _, ok := <-suite.wkr.GetActionsChannel():
suite.False(ok, "actions channel should be closed")
default:
suite.FailNow("unexpected error")
}
}
// ***************************************************************************************
// ** SetExternalTaskSuccesses && GetExternalTaskSuccesses
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalTaskSuccesses() {
cint := goconcurrentcounter.NewIntMutex(0)
suite.wkr.SetExternalTaskSuccesses(cint)
suite.Equal(cint, suite.wkr.GetExternalTaskSuccesses(), "wrong external task successes")
}
// ***************************************************************************************
// ** SetExternalFunctionForAction
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalFunctionForAction() {
dummyFunc := func() (reEnqueueWorker bool) {
reEnqueueWorker = true
return
}
suite.wkr.SetExternalFunctionForAction("action1", dummyFunc)
f, ok := suite.wkr.externalActions["action1"]
suite.True(ok, "an entry should exists for 'action1'")
// functions return
suite.Equal(dummyFunc(), f())
}
// ***************************************************************************************
// ** SetExternalFeedbackFunction
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalFeedbackFunction() {
dummyIntValue := 0
dummyFunc := func(workerID int, reEnqueueWorker bool) {
dummyIntValue = workerID
}
suite.wkr.SetExternalFeedbackFunction(dummyFunc)
fn := suite.wkr.externalFeedbackFunc
suite.NotNil(fn, "external feedback func should not be nil")
// execute function
fn(100, true)
suite.Equal(100, dummyIntValue)
}
// ***************************************************************************************
// ** SetExternalPreActionFunc
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalPreActionFunc() {
dummyIntValue := 0
dummyFunc := func() {
dummyIntValue = 50
}
suite.wkr.SetExternalPreActionFunc(dummyFunc)
fn := suite.wkr.externalPreActionFunc
suite.NotNil(fn, "external preAction func should not be nil")
// execute function
fn()
suite.Equal(50, dummyIntValue)
}
// ***************************************************************************************
// ** SetExternalPostActionFunc
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalPostActionFunc() {
dummyIntValue := 0
dummyFunc := func() {
dummyIntValue = 50
}
suite.wkr.SetExternalPostActionFunc(dummyFunc)
fn := suite.wkr.externalPostActionFunc
suite.NotNil(fn, "external postAction func should not be nil")
// execute function
fn()
suite.Equal(50, dummyIntValue)
}
// ***************************************************************************************
// ** SetExternalPreTaskFunc
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalPreTaskFunc() {
dummyIntValue := 0
dummyFunc := func() {
dummyIntValue = 50
}
suite.wkr.SetExternalPreTaskFunc(dummyFunc)
fn := suite.wkr.externalPreTaskFunc
suite.NotNil(fn, "external preAction func should not be nil")
// execute function
fn()
suite.Equal(50, dummyIntValue)
}
// ***************************************************************************************
// ** SetExternalPostTaskFunc
// ***************************************************************************************
func (suite *WorkerTestSuite) TestSetExternalPostTaskFunc() {
dummyIntValue := 0
dummyFunc := func() {
dummyIntValue = 50
}
suite.wkr.SetExternalPostTaskFunc(dummyFunc)
fn := suite.wkr.externalPostTaskFunc
suite.NotNil(fn, "external preAction func should not be nil")
// execute function
fn()
suite.Equal(50, dummyIntValue)
}
// ***************************************************************************************
// ** ProcessTask && ProcessAction
// ***************************************************************************************
func (suite *WorkerTestSuite) TestProcessTaskNilTask() {
err := suite.wkr.ProcessTask(nil)
suite.Error(err, "error expected for nil task")
pErr, ok := err.(*PoolError)
suite.True(ok, "expected error's type: *PoolError")
suite.Equal(ErrorWorkerNilTask, pErr.Code())
}
func (suite *WorkerTestSuite) TestProcessTaskRegularTaskNoFunc() {
err := suite.wkr.ProcessTask(&task{
Code: taskRegular,
Func: nil,
})
suite.Error(err, "error expected if regularTask comes without a function to process the data")
if pErr, ok := err.(*PoolError); !ok {
suite.FailNow("expected error's type: *PoolError")
} else {
suite.Equalf(ErrorWorkerNoTaskFunc, pErr.Code(), "expected error's code: %v", taskRegular)
}
}
func (suite *WorkerTestSuite) TestProcessTask() {
cntinue := make(chan struct{})
cntinue2 := make(chan struct{})
dummyTask := &task{}
// fake Listen function
go func() {
cntinue <- struct{}{}
tsk := <-suite.wkr.GetTasksChannel()
suite.Equal(dummyTask, tsk, "received task is not what was sent")
cntinue2 <- struct{}{}
}()
// wait here until the fake Listen ^ is up and running
<-cntinue
err := suite.wkr.ProcessTask(dummyTask)
// wait here until the fake Listen function is done
<-cntinue2
suite.Equal(0, len(suite.wkr.GetTasksChannel()))
suite.NoError(err)
}
func (suite *WorkerTestSuite) TestProcessAction() {
cntinue := make(chan struct{})
cntinue2 := make(chan struct{})
dummyAction := action{}
// fake Listen function
go func() {
cntinue <- struct{}{}
tsk := <-suite.wkr.GetActionsChannel()
suite.Equal(dummyAction, tsk, "received action is not what was sent")
cntinue2 <- struct{}{}
}()
// wait here until the fake Listen ^ is up and running
<-cntinue
suite.wkr.ProcessAction(dummyAction)
suite.Equal(0, len(suite.wkr.GetActionsChannel()))
// wait here until the fake Listen function is done
<-cntinue2
}
// ***************************************************************************************
// ** Listen
// ***************************************************************************************
type externalFeedbackFunctionData struct {
workerID int
reEnqueueWorker bool
}
// Process three different actions in a row:
// 1st action: no external functions, no internal function, worker keep listen for new data
// 2nd action: external actions, no internal function, worker keep listen for new data
// 3rd action: external functions, internal function returning false ==> worker won't be available to process new data
func (suite *WorkerTestSuite) TestListenAction() {
externalFeedbackFunctionChan := make(chan externalFeedbackFunctionData, 2)
// set external feedback function
suite.wkr.SetExternalFeedbackFunction(func(workerID int, reEnqueueWorker bool) {
externalFeedbackFunctionChan <- externalFeedbackFunctionData{workerID, reEnqueueWorker}
})
// start listening
suite.wkr.Listen()
// *************************************************************************
// first action with no external functions to execute, no internal function
// *************************************************************************
// first action without external functions to execute, nothing will happen
suite.wkr.ProcessAction(action{
SendToWorker: true,
Code: "dummyActionCode",
PreExternalFunc: nil,
})
// wait here until the first dummy action gets processed and the listener gets ready for something new
var feedbackData externalFeedbackFunctionData
select {
case feedbackData = <-externalFeedbackFunctionChan:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for an action")
}
suite.Equal(suite.wkr.GetID(), feedbackData.workerID)
suite.True(feedbackData.reEnqueueWorker, "worker should be ready to process new data")
// *************************************************************************
// second action with external functions to execute, no internal function
// *************************************************************************
tmpValue := 0
dummyAction := action{
SendToWorker: true,
Code: "dummyAction",
}
// set external preAction (dummy function updating tmpValue to 100, so it would be easy to verify whether this function was executed)
suite.wkr.SetExternalPreActionFunc(func() { tmpValue = 100 })
// channel to wait until external post action's function gets executed
cntinuePostAction := make(chan struct{}, 2)
// set external post action function
suite.wkr.SetExternalPostActionFunc(func() { cntinuePostAction <- struct{}{} })
// channel to wait until external action's function gets executed
waitForExternalFunc2ndAction := make(chan struct{}, 2)
// set external action function
suite.wkr.SetExternalFunctionForAction(dummyAction.Code, func() (reEnqueueWorker bool) {
defer func() {
waitForExternalFunc2ndAction <- struct{}{}
}()
return true
})
// send the action to be processed
suite.wkr.ProcessAction(dummyAction)
// wait here until second action's external function get executed
select {
case <-waitForExternalFunc2ndAction:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for an action's external function")
}
// verify external preAction (for 2nd action)
suite.Equal(tmpValue, 100)
// wait here until feedback is received (for 2nd action)
select {
case feedbackData = <-externalFeedbackFunctionChan:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for an action")
}
suite.Equal(suite.wkr.GetID(), feedbackData.workerID)
suite.True(feedbackData.reEnqueueWorker, "worker should be ready to process new data")
// wait here until external post action's function get executed
select {
case <-cntinuePostAction:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for a post action's external function")
}
// *************************************************************************
// third action with external functions to execute and internal function
// returning false
// *************************************************************************
// reset tmpValue
tmpValue = 0
waitForActionKillWorkerExternalFunction := make(chan struct{}, 2)
// set external function for third action
suite.wkr.SetExternalFunctionForAction(actionKillWorker, func() (reEnqueueWorker bool) {
defer func() {
waitForActionKillWorkerExternalFunction <- struct{}{}
}()
reEnqueueWorker = true
return
})
// send third action
suite.wkr.ProcessAction(action{
Code: actionKillWorker,
SendToWorker: true,
})
// wait here until third action external's function gets executed
<-waitForActionKillWorkerExternalFunction
// verify external pre action function
suite.Equal(100, tmpValue, "external pre action func should be executed before this point")
// wait here for the external feedback
select {
case feedbackData = <-externalFeedbackFunctionChan:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for an action's external function")
}
// verify feedback data
suite.Equal(suite.wkr.GetID(), feedbackData.workerID)
suite.False(feedbackData.reEnqueueWorker, "reEnqueueWorker should be false because the internal function returned false")
// verify external post action
// wait here until external post action's function get executed
select {
case <-cntinuePostAction:
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for a post action's external function")
}
}
// Process four different regular tasks in a row:
// 1 - task (data + function), no callback, no external functions
// 2 - task (data + function) + callback, no external functions
// 3 - task (data + function returning false, meaning => fail), no callback, no external functions
// 4 - task (data + function) + callback + external functions
func (suite *WorkerTestSuite) TestListenRegularTask() {
// start listening
suite.wkr.Listen()
// set external task successes counter
externalTaskSuccesses := goconcurrentcounter.NewIntMutex(0)
suite.wkr.SetExternalTaskSuccesses(externalTaskSuccesses)
// *************************************************************************
// first task: data + function, no callback, no external functions
// *************************************************************************
// to wait until 1st task's function gets executed
waitForTaskFunction := make(chan struct{}, 2)
// 1st task
tsk := &task{
Code: taskRegular,
Data: 100,
Func: func(data interface{}) bool {
defer func() {
waitForTaskFunction <- struct{}{}
}()
return true
},
}
// send the task
err := suite.wkr.ProcessTask(tsk)
suite.NoError(err, "no error expected for task + function + no callback + no external functions")
// wait for the task's function
select {
case <-waitForTaskFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for task's function execution")
}
// verify the metrics, the task's function returned true so externalTaskSuccesses should reflect it
suite.Equal(1, externalTaskSuccesses.GetValue())
suite.Equal(1, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// *************************************************************************
// second task: data + function + callback, no external functions
// *************************************************************************
// to wait until 2nd task's function gets executed
waitForTask2Function := make(chan struct{}, 2)
// to wait until 2nd task's callback gets executed
waitFor2ndTaskCallback := make(chan struct{}, 2)
// 2nd task
tsk2 := &task{
Code: taskRegular,
Data: 150,
Func: func(data interface{}) bool {
defer func() {
waitForTask2Function <- struct{}{}
}()
return true
},
CallbackFunc: func(data interface{}) {
waitFor2ndTaskCallback <- struct{}{}
},
}
// send the task
err = suite.wkr.ProcessTask(tsk2)
suite.NoError(err, "no error expected for task + function + callback + no external functions")
// wait until 2nd task's function gets executed
select {
case <-waitForTask2Function:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd task's function execution")
}
// wait until 2nd task's callback gets executed
select {
case <-waitFor2ndTaskCallback:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd task's callback execution")
}
// verify 2nd success
suite.Equal(2, externalTaskSuccesses.GetValue())
suite.Equal(2, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// *************************************************************************
// third task: data + function (returning false), no callback, no external
// functions
// *************************************************************************
// to wait until 3rd task's function gets executed
waitForTask3Function := make(chan struct{}, 2)
// 3rd task
tsk3 := &task{
Code: taskRegular,
Data: 200,
Func: func(data interface{}) bool {
defer func() {
waitForTask3Function <- struct{}{}
}()
return false
},
}
// send task
err = suite.wkr.ProcessTask(tsk3)
suite.NoError(err, "no error expected for task + function (returning false) + no callback + no external functions")
select {
case <-waitForTask3Function:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 3rd task's function execution")
}
// verify 3rd fail
suite.Equal(2, externalTaskSuccesses.GetValue())
suite.Equal(2, suite.wkr.GetTaskSuccesses())
// verify that failCounter == 1
suite.Equal(1, suite.wkr.GetTaskFailures())
// *************************************************************************
// fourth task: data + function (returning true) + callback + external
// functions
// *************************************************************************
// to wait until 4th task's function gets executed
waitForTask4Function := make(chan struct{}, 2)
// to wait until 4th task's callback gets executed
waitFor4thTaskCallback := make(chan struct{}, 2)
// to wait until pre-task function gets executed
waitForPreTaskFunction := make(chan struct{}, 2)
// to wait until post-task function gets executed
waitForPostTaskFunction := make(chan struct{}, 2)
// set external functions
// pre-task function
suite.wkr.SetExternalPreTaskFunc(func() {
waitForPreTaskFunction <- struct{}{}
})
// post-task function
suite.wkr.SetExternalPostTaskFunc(func() {
waitForPostTaskFunction <- struct{}{}
})
// 4th task
tsk4 := &task{
Code: taskRegular,
Data: 250,
Func: func(data interface{}) bool {
defer func() {
waitForTask4Function <- struct{}{}
}()
return true
},
CallbackFunc: func(data interface{}) {
waitFor4thTaskCallback <- struct{}{}
},
}
// send 4th task
suite.wkr.ProcessTask(tsk4)
// wait until pre-task function
select {
case <-waitForPreTaskFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 4th task's pre-function execution")
}
// wait until 4th task's function
select {
case <-waitForTask4Function:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 4th task's function execution")
}
// wait until 4th task's callback
select {
case <-waitFor4thTaskCallback:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 4th task's callback execution")
}
// metrics
suite.Equal(3, externalTaskSuccesses.GetValue())
suite.Equal(3, suite.wkr.GetTaskSuccesses())
suite.Equal(1, suite.wkr.GetTaskFailures())
// wait until 4th post-task function
select {
case <-waitForPostTaskFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 4th task's post-task function execution")
}
}
// Process four different late actions in a row:
// 1 - late task action + internal function + external feedback function, no external function
// 2 - late task action + external functions + internal function + callback
// 3 - late task action + external function + internal function returning false + callback
func (suite *WorkerTestSuite) TestListenLateActionTask() {
// start listening
suite.wkr.Listen()
// *************************************************************************
// 1st task: late action + internal function, no external function
// *************************************************************************
waitFor1stInternalFunction := make(chan struct{}, 2)
waitForExternalFeedbackFunction := make(chan externalFeedbackFunctionData, 2)
// set external feedback function
suite.wkr.SetExternalFeedbackFunction(func(workerId int, reEnqueueWorker bool) {
waitForExternalFeedbackFunction <- externalFeedbackFunctionData{workerID, reEnqueueWorker}
})
// set internal function
suite.wkr.setInternalFunctionForAction([]string{"taskLateDummy1"}, func() bool {
defer func() {
waitFor1stInternalFunction <- struct{}{}
}()
return true
})
tsk1 := &task{
Code: taskLateAction,
Data: action{
Code: "taskLateDummy1",
},
}
// process task1 (late-action)
suite.wkr.ProcessTask(tsk1)
// wait until 1st action's internal function gets executed
select {
case <-waitFor1stInternalFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 1st action's internal function function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// wait until external feedback function gets executed for 1st action
select {
case feedback := <-waitForExternalFeedbackFunction:
suite.Equal(suite.wkr.GetID(), feedback.workerID)
suite.True(feedback.reEnqueueWorker)
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 1st action's feedback function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// *************************************************************************
// 2nd task: late task action + external functions, no internal function
// *************************************************************************
tsk2 := &task{
Code: taskLateAction,
Data: action{
Code: "taskLateDummy2",
},
}
waitForExternalPreActionFunction := make(chan struct{}, 2)
waitFor2ndActionInternalFunction := make(chan struct{}, 2)
waitForExternalFunction2ndAction := make(chan struct{}, 2)
waitForExternalPostActionFunction := make(chan struct{}, 2)
// set internal function
suite.wkr.setInternalFunctionForAction([]string{"taskLateDummy2"}, func() bool {
defer func() {
waitFor2ndActionInternalFunction <- struct{}{}
}()
return true
})
// set external functions
// external pre-action func
suite.wkr.SetExternalPreActionFunc(func() {
waitForExternalPreActionFunction <- struct{}{}
})
// external function for action
suite.wkr.SetExternalFunctionForAction("taskLateDummy2", func() (reEnqueueWorker bool) {
defer func() {
waitForExternalFunction2ndAction <- struct{}{}
}()
return true
})
// external post-action func
suite.wkr.SetExternalPostActionFunc(func() {
waitForExternalPostActionFunction <- struct{}{}
})
// process task2 (late-action)
suite.wkr.ProcessTask(tsk2)
// wait for external pre-action function
select {
case <-waitForExternalPreActionFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd action's pre-task function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// wait for internal function for action
select {
case <-waitFor2ndActionInternalFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd action's internal function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// wait for external action for function
select {
case <-waitForExternalFunction2ndAction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd action's function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// wait until external feedback function gets executed for 2nd action
select {
case feedback := <-waitForExternalFeedbackFunction:
suite.Equal(suite.wkr.GetID(), feedback.workerID)
suite.True(feedback.reEnqueueWorker)
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd action's feedback function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// wait until 3rd action's external post-action func gets executed
select {
case <-waitForExternalPostActionFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 2nd action's post-action function execution")
}
// metrics
suite.Equal(0, suite.wkr.GetTaskSuccesses())
suite.Equal(0, suite.wkr.GetTaskFailures())
// *************************************************************************
// 3rd task: late task action + external functions + internal function
// returning false
// *************************************************************************
tsk3 := &task{
Code: taskLateAction,
Data: action{
Code: "taskLateDummy3",
},
}
waitFor3rdActionInternalFunction := make(chan struct{}, 2)
waitForExternalFunction3rdAction := make(chan struct{}, 2)
// set internal function
suite.wkr.setInternalFunctionForAction([]string{"taskLateDummy3"}, func() bool {
defer func() {
waitFor3rdActionInternalFunction <- struct{}{}
}()
return false
})
// set internal function
suite.wkr.setInternalFunctionForAction([]string{"taskLateDummy3"}, func() bool {
defer func() {
waitFor3rdActionInternalFunction <- struct{}{}
}()
return false
})
// set external functions
// external function for action
suite.wkr.SetExternalFunctionForAction("taskLateDummy3", func() (reEnqueueWorker bool) {
defer func() {
waitForExternalFunction3rdAction <- struct{}{}
}()
return true
})
// process task3 (late-action)
suite.wkr.ProcessTask(tsk3)
// wait for external pre-action function
select {
case <-waitForExternalPreActionFunction:
// after 5 seconds it would fail
case <-time.After(5 * time.Second):
suite.FailNow("Too much time waiting for 3rd action's pre-task function execution")
}