@@ -23,13 +23,13 @@ package internal
23
23
24
24
import (
25
25
"context"
26
- "sync/atomic"
27
26
"testing"
28
27
"time"
29
28
30
29
"github.com/golang/mock/gomock"
31
30
"github.com/pborman/uuid"
32
31
"github.com/stretchr/testify/suite"
32
+ "go.uber.org/atomic"
33
33
"go.uber.org/cadence/.gen/go/cadence/workflowservicetest"
34
34
m "go.uber.org/cadence/.gen/go/shared"
35
35
"go.uber.org/cadence/internal/common"
@@ -663,16 +663,15 @@ func (s *WorkersTestSuite) createLocalActivityMarkerDataForTest(activityID strin
663
663
}
664
664
665
665
func (s * WorkersTestSuite ) TestLocallyDispatchedActivity () {
666
- activityCalledCount := 0
666
+ activityCalledCount := atomic . NewInt32 ( 0 ) // must be accessed with atomics, worker uses goroutines to run activities
667
667
activitySleep := func (duration time.Duration ) error {
668
668
time .Sleep (duration )
669
- activityCalledCount ++
669
+ activityCalledCount . Add ( 1 )
670
670
return nil
671
671
}
672
672
673
673
doneCh := make (chan struct {})
674
674
675
- isActivityResponseCompleted := false
676
675
workflowFn := func (ctx Context , input []byte ) error {
677
676
ao := ActivityOptions {
678
677
ScheduleToCloseTimeout : 1 * time .Second ,
@@ -736,10 +735,11 @@ func (s *WorkersTestSuite) TestLocallyDispatchedActivity() {
736
735
TaskToken : []byte ("test-token" )}
737
736
return & m.RespondDecisionTaskCompletedResponse {ActivitiesToDispatchLocally : activitiesToDispatchLocally }, nil
738
737
}).Times (1 )
738
+ isActivityResponseCompleted := atomic .NewBool (false )
739
739
s .service .EXPECT ().RespondActivityTaskCompleted (gomock .Any (), gomock .Any (), callOptions ()... ).DoAndReturn (func (ctx context.Context , request * m.RespondActivityTaskCompletedRequest , opts ... yarpc.CallOption ,
740
740
) error {
741
741
defer close (doneCh )
742
- isActivityResponseCompleted = true
742
+ isActivityResponseCompleted . Swap ( true )
743
743
return nil
744
744
}).Times (1 )
745
745
@@ -756,32 +756,37 @@ func (s *WorkersTestSuite) TestLocallyDispatchedActivity() {
756
756
757
757
startWorkerAndWait (s , worker , & doneCh )
758
758
759
- s .True (isActivityResponseCompleted )
760
- s .Equal (1 , activityCalledCount )
759
+ s .True (isActivityResponseCompleted . Load () )
760
+ s .Equal (int32 ( 1 ) , activityCalledCount . Load () )
761
761
}
762
762
763
763
func (s * WorkersTestSuite ) TestMultipleLocallyDispatchedActivity () {
764
- var activityCalledCount uint32 = 0
764
+ activityCalledCount := atomic . NewInt32 ( 0 )
765
765
activitySleep := func (duration time.Duration ) error {
766
766
time .Sleep (duration )
767
- atomic . AddUint32 ( & activityCalledCount , 1 )
767
+ activityCalledCount . Add ( 1 )
768
768
return nil
769
769
}
770
770
771
771
doneCh := make (chan struct {})
772
772
773
- var activityCount uint32 = 5
773
+ var activityCount int32 = 5
774
774
workflowFn := func (ctx Context , input []byte ) error {
775
775
ao := ActivityOptions {
776
776
ScheduleToCloseTimeout : 1 * time .Second ,
777
777
ScheduleToStartTimeout : 1 * time .Second ,
778
778
StartToCloseTimeout : 1 * time .Second ,
779
779
}
780
780
ctx = WithActivityOptions (ctx , ao )
781
- for i := 1 ; i < int (activityCount ); i ++ {
782
- ExecuteActivity (ctx , activitySleep , 500 * time .Millisecond )
781
+
782
+ // start all activities in parallel, and wait for them all to complete.
783
+ var all []Future
784
+ for i := 0 ; i < int (activityCount ); i ++ {
785
+ all = append (all , ExecuteActivity (ctx , activitySleep , 500 * time .Millisecond ))
786
+ }
787
+ for i , f := range all {
788
+ s .NoError (f .Get (ctx , nil ), "activity %v should not have failed" , i )
783
789
}
784
- ExecuteActivity (ctx , activitySleep , 500 * time .Millisecond ).Get (ctx , nil )
785
790
return nil
786
791
}
787
792
@@ -843,15 +848,13 @@ func (s *WorkersTestSuite) TestMultipleLocallyDispatchedActivity() {
843
848
}
844
849
return & m.RespondDecisionTaskCompletedResponse {ActivitiesToDispatchLocally : activitiesToDispatchLocally }, nil
845
850
}).Times (1 )
846
- var activityResponseCompletedCount uint32 = 0
851
+ activityResponseCompletedCount := atomic . NewInt32 ( 0 )
847
852
s .service .EXPECT ().RespondActivityTaskCompleted (gomock .Any (), gomock .Any (), callOptions ()... ).DoAndReturn (func (ctx context.Context , request * m.RespondActivityTaskCompletedRequest , opts ... yarpc.CallOption ,
848
853
) error {
849
- defer func () {
850
- if atomic .LoadUint32 (& activityResponseCompletedCount ) == activityCount {
851
- close (doneCh )
852
- }
853
- }()
854
- atomic .AddUint32 (& activityResponseCompletedCount , 1 )
854
+ counted := activityResponseCompletedCount .Add (1 )
855
+ if counted == activityCount {
856
+ close (doneCh )
857
+ }
855
858
return nil
856
859
}).MinTimes (1 )
857
860
@@ -862,31 +865,33 @@ func (s *WorkersTestSuite) TestMultipleLocallyDispatchedActivity() {
862
865
)
863
866
worker .RegisterActivityWithOptions (activitySleep , RegisterActivityOptions {Name : "activitySleep" })
864
867
s .NotNil (worker .locallyDispatchedActivityWorker )
865
- worker .Start ()
868
+ err := worker .Start ()
869
+ s .NoError (err , "worker failed to start" )
866
870
867
871
// wait for test to complete
868
872
// This test currently never completes, however after the timeout the asserts are true
869
873
// so the test passes, I believe this is an error.
870
874
select {
871
875
case <- doneCh :
872
- break
876
+ s . T (). Log ( "completed" )
873
877
case <- time .After (1 * time .Second ):
878
+ s .T ().Log ("timed out" )
874
879
}
875
880
worker .Stop ()
876
881
877
882
// for currently unbuffered channel at least one activity should be sent
878
- s .True (activityResponseCompletedCount > 0 )
879
- s .True (activityCalledCount > 0 )
883
+ s .True (activityResponseCompletedCount . Load () > 0 )
884
+ s .True (activityCalledCount . Load () > 0 )
880
885
}
881
886
882
887
// wait for test to complete - timeout and fail after 10 seconds to not block execution of other tests
883
888
func startWorkerAndWait (s * WorkersTestSuite , worker * aggregatedWorker , doneCh * chan struct {}) {
884
889
s .T ().Helper ()
885
- worker .Start ()
890
+ err := worker .Start ()
891
+ s .NoError (err , "worker failed to start" )
886
892
// wait for test to complete
887
893
select {
888
894
case <- * doneCh :
889
- return
890
895
case <- time .After (10 * time .Second ):
891
896
s .Fail ("Test timed out" )
892
897
}
0 commit comments