@@ -2,6 +2,7 @@ package dbos
22
33import (
44 "context"
5+ "encoding/gob"
56 "errors"
67 "fmt"
78 "reflect"
@@ -42,11 +43,26 @@ func simpleStepError(_ context.Context) (string, error) {
4243 return "" , fmt .Errorf ("step failure" )
4344}
4445
46+ type stepWithSleepOutput struct {
47+ StepID int
48+ Result string
49+ Error error
50+ }
51+
4552func stepWithSleep (_ context.Context , duration time.Duration ) (string , error ) {
4653 time .Sleep (duration )
4754 return fmt .Sprintf ("from step that slept for %s" , duration ), nil
4855}
4956
57+ func stepWithSleepCustomOutput (_ context.Context , duration time.Duration , stepID int ) (stepWithSleepOutput , error ) {
58+ time .Sleep (duration )
59+ return stepWithSleepOutput {
60+ StepID : stepID ,
61+ Result : fmt .Sprintf ("from step that slept for %s" , duration ),
62+ Error : nil ,
63+ }, nil
64+ }
65+
5066func simpleWorkflowWithStepError (dbosCtx DBOSContext , input string ) (string , error ) {
5167 return RunAsStep (dbosCtx , func (ctx context.Context ) (string , error ) {
5268 return simpleStepError (ctx )
@@ -862,6 +878,10 @@ func TestSteps(t *testing.T) {
862878
863879func TestGoRunningStepsInsideGoRoutines (t * testing.T ) {
864880 dbosCtx := setupDBOS (t , true , true )
881+
882+ // Register custom types for Gob encoding
883+ var stepOutput stepWithSleepOutput
884+ gob .Register (stepOutput )
865885 t .Run ("Go must run steps inside a workflow" , func (t * testing.T ) {
866886 _ , err := Go (dbosCtx , func (ctx context.Context ) (string , error ) {
867887 return stepWithSleep (ctx , 1 * time .Second )
@@ -902,12 +922,12 @@ func TestGoRunningStepsInsideGoRoutines(t *testing.T) {
902922 const numSteps = 100
903923 results := make (chan string , numSteps )
904924 errors := make (chan error , numSteps )
905- var resultChans []<- chan StepOutcome [string ]
925+ var resultChans []<- chan StepOutcome [stepWithSleepOutput ]
906926
907927 goWorkflow := func (dbosCtx DBOSContext , input string ) (string , error ) {
908- for range numSteps {
909- resultChan , err := Go (dbosCtx , func (ctx context.Context ) (string , error ) {
910- return stepWithSleep (ctx , 20 * time .Millisecond )
928+ for i := 0 ; i < numSteps ; i ++ {
929+ resultChan , err := Go (dbosCtx , func (ctx context.Context ) (stepWithSleepOutput , error ) {
930+ return stepWithSleepCustomOutput (ctx , 20 * time .Millisecond , i )
911931 })
912932
913933 if err != nil {
@@ -916,12 +936,13 @@ func TestGoRunningStepsInsideGoRoutines(t *testing.T) {
916936 resultChans = append (resultChans , resultChan )
917937 }
918938
919- for _ , resultChan := range resultChans {
939+ for i , resultChan := range resultChans {
920940 result1 := <- resultChan
921941 if result1 .err != nil {
922- errors <- result1 .err
942+ errors <- result1 .result . Error
923943 }
924- results <- result1 .result
944+ assert .Equal (t , i , result1 .result .StepID , "expected step ID to be %d, got %d" , i , result1 .result .StepID )
945+ results <- result1 .result .Result
925946 }
926947 return "" , nil
927948 }
@@ -933,18 +954,10 @@ func TestGoRunningStepsInsideGoRoutines(t *testing.T) {
933954
934955 close (results )
935956 close (errors )
936-
937957 require .NoError (t , err , "failed to get result from go workflow" )
938958 assert .Equal (t , numSteps , len (results ), "expected %d results, got %d" , numSteps , len (results ))
939959 assert .Equal (t , 0 , len (errors ), "expected no errors, got %d" , len (errors ))
940960
941- // Test step IDs are deterministic and in the order of execution
942- steps , err := GetWorkflowSteps (dbosCtx , handle .GetWorkflowID ())
943- require .NoError (t , err , "failed to get workflow steps" )
944- require .Len (t , steps , numSteps , "expected %d steps, got %d" , numSteps , len (steps ))
945- for i := 0 ; i < numSteps ; i ++ {
946- assert .Equal (t , i , steps [i ].StepID , "expected step ID to be %d, got %d" , i , steps [i ].StepID )
947- }
948961 })
949962}
950963
0 commit comments