@@ -35,7 +35,7 @@ func simpleWorkflowWithStep(dbosCtx DBOSContext, input string) (string, error) {
3535}
3636
3737func slowWorkflow (dbosCtx DBOSContext , sleepTime time.Duration ) (string , error ) {
38- time . Sleep (sleepTime )
38+ Sleep (dbosCtx , sleepTime )
3939 return "done" , nil
4040}
4141
@@ -4534,76 +4534,65 @@ func TestWorkflowHandleTimeout(t *testing.T) {
45344534 RegisterWorkflow (dbosCtx , slowWorkflow )
45354535
45364536 t .Run ("WorkflowHandleTimeout" , func (t * testing.T ) {
4537- // Test timeout on workflowHandle (channel-based)
4538- handle , err := RunWorkflow (dbosCtx , slowWorkflow , 5 * time .Second )
4537+ handle , err := RunWorkflow (dbosCtx , slowWorkflow , 10 * time .Second )
45394538 require .NoError (t , err , "failed to start workflow" )
45404539
4541- // Test with a very short timeout - should timeout
45424540 start := time .Now ()
4543- _ , err = handle .GetResult (WithHandleTimeout (1 * time .Millisecond ))
4541+ _ , err = handle .GetResult (WithHandleTimeout (10 * time .Millisecond ))
45444542 duration := time .Since (start )
45454543
45464544 require .Error (t , err , "expected timeout error" )
45474545 assert .Contains (t , err .Error (), "workflow result timeout" )
45484546 assert .True (t , duration < 100 * time .Millisecond , "timeout should occur quickly" )
4547+ assert .True (t , errors .Is (err , context .DeadlineExceeded ),
4548+ "expected error to be detectable as context.DeadlineExceeded, got: %v" , err )
45494549 })
45504550
4551- t .Run ("WorkflowHandleNoTimeout " , func (t * testing.T ) {
4552- // Test without timeout - should work normally
4553- handle , err := RunWorkflow (dbosCtx , slowWorkflow , 1 * time .Millisecond )
4551+ t .Run ("WorkflowPollingHandleTimeout " , func (t * testing.T ) {
4552+ // Start a workflow that will block on the first signal
4553+ originalHandle , err := RunWorkflow (dbosCtx , slowWorkflow , 10 * time .Second )
45544554 require .NoError (t , err , "failed to start workflow" )
45554555
4556- result , err := handle .GetResult ()
4557- require .NoError (t , err , "GetResult without timeout should succeed" )
4558- assert .Equal (t , "done" , result )
4559- })
4560-
4561- t .Run ("WorkflowHandleGetResultAfterChannelClose" , func (t * testing.T ) {
4562- // Test getting result after the outcome channel would be closed
4563- handle , err := RunWorkflow (dbosCtx , slowWorkflow , 1 * time .Millisecond )
4564- require .NoError (t , err , "failed to start workflow" )
4556+ pollingHandle , err := RetrieveWorkflow [string ](dbosCtx , originalHandle .GetWorkflowID ())
4557+ require .NoError (t , err , "failed to retrieve workflow" )
45654558
4566- // Get result first time - this will close the outcome channel
4567- result1 , err := handle .GetResult ()
4568- require .NoError (t , err , "first GetResult should succeed" )
4569- assert .Equal (t , "done" , result1 )
4559+ _ , ok := pollingHandle .(* workflowPollingHandle [string ])
4560+ require .True (t , ok , "expected polling handle, got %T" , pollingHandle )
45704561
4571- // Sleep briefly to ensure channel is closed
4572- time .Sleep (10 * time .Millisecond )
4562+ _ , err = pollingHandle .GetResult (WithHandleTimeout (10 * time .Millisecond ))
45734563
4574- // Get result second time - should fail since channel is closed
4575- _ , err = handle .GetResult ()
4576- require .Error (t , err , "second GetResult should fail" )
4577- assert .Contains (t , err .Error (), "workflow result channel is already closed" )
4564+ require .Error (t , err , "expected timeout error" )
4565+ assert .True (t , errors .Is (err , context .DeadlineExceeded ),
4566+ "expected error to be detectable as context.DeadlineExceeded, got: %v" , err )
45784567 })
45794568}
45804569
4581- func TestWorkflowPollingHandleTimeout (t * testing.T ) {
4570+ func TestWorkflowHandleContextCancel (t * testing.T ) {
45824571 dbosCtx := setupDBOS (t , true , true )
4583- RegisterWorkflow (dbosCtx , slowWorkflow )
4572+ RegisterWorkflow (dbosCtx , getEventWorkflow )
45844573
4585- t .Run ("WorkflowPollingHandleTimeout" , func (t * testing.T ) {
4586- // Test timeout on workflowPollingHandle (database polling)
4587- handle , err := RunWorkflow (dbosCtx , slowWorkflow , 5 * time .Second )
4574+ t .Run ("WorkflowHandleContextCancel" , func (t * testing.T ) {
4575+ getEventWorkflowStartedSignal .Clear ()
4576+ handle , err := RunWorkflow (dbosCtx , getEventWorkflow , getEventWorkflowInput {
4577+ TargetWorkflowID : "test-workflow-id" ,
4578+ Key : "test-key" ,
4579+ })
45884580 require .NoError (t , err , "failed to start workflow" )
45894581
4590- // Test with a very short timeout - should timeout
4591- start := time .Now ()
4592- _ , err = handle .GetResult (WithHandleTimeout (1 * time .Millisecond ))
4593- duration := time .Since (start )
4582+ resultChan := make (chan error )
4583+ go func () {
4584+ _ , err := handle .GetResult ()
4585+ resultChan <- err
4586+ }()
45944587
4595- require .Error (t , err , "expected timeout error" )
4596- assert .Contains (t , err .Error (), "workflow result timeout after 1ms" )
4597- assert .True (t , duration < 100 * time .Millisecond , "timeout should occur quickly" )
4598- })
4588+ getEventWorkflowStartedSignal .Wait ()
4589+ getEventWorkflowStartedSignal .Clear ()
45994590
4600- t .Run ("WorkflowPollingHandleNoTimeout" , func (t * testing.T ) {
4601- // Test without timeout - should work normally
4602- handle , err := RunWorkflow (dbosCtx , slowWorkflow , 1 * time .Millisecond )
4603- require .NoError (t , err , "failed to start workflow" )
4591+ dbosCtx .Shutdown (1 * time .Second )
46044592
4605- result , err := handle .GetResult ()
4606- require .NoError (t , err , "GetResult without timeout should succeed" )
4607- assert .Equal (t , "done" , result )
4593+ err = <- resultChan
4594+ require .Error (t , err , "expected error from cancelled context" )
4595+ assert .True (t , errors .Is (err , context .Canceled ),
4596+ "expected error to be detectable as context.Canceled, got: %v" , err )
46084597 })
46094598}
0 commit comments