@@ -38,6 +38,12 @@ func simpleStep(_ context.Context) (string, error) {
3838 return "from step" , nil
3939}
4040
41+ func slowWorkflow (dbosCtx DBOSContext , input string ) (string , error ) {
42+ // Simulate a slow workflow that takes time to complete
43+ time .Sleep (500 * time .Millisecond )
44+ return input , nil
45+ }
46+
4147func simpleStepError (_ context.Context ) (string , error ) {
4248 return "" , fmt .Errorf ("step failure" )
4349}
@@ -4523,3 +4529,82 @@ func TestWorkflowIdentity(t *testing.T) {
45234529 assert .Equal (t , []string {"reader" , "writer" }, status .AuthenticatedRoles )
45244530 })
45254531}
4532+
4533+ func TestWorkflowHandleTimeout (t * testing.T ) {
4534+ dbosCtx := setupDBOS (t , true , true )
4535+ RegisterWorkflow (dbosCtx , simpleWorkflow )
4536+
4537+ t .Run ("WorkflowHandleTimeout" , func (t * testing.T ) {
4538+ // Test timeout on workflowHandle (channel-based)
4539+ handle , err := RunWorkflow (dbosCtx , simpleWorkflow , "test" )
4540+ require .NoError (t , err , "failed to start workflow" )
4541+
4542+ // Test with a very short timeout - should timeout
4543+ start := time .Now ()
4544+ _ , err = handle .GetResult (WithHandleTimeout (1 * time .Millisecond ))
4545+ duration := time .Since (start )
4546+
4547+ require .Error (t , err , "expected timeout error" )
4548+ assert .Contains (t , err .Error (), "workflow result timeout" )
4549+ assert .True (t , duration < 100 * time .Millisecond , "timeout should occur quickly" )
4550+ })
4551+
4552+ t .Run ("WorkflowHandleNoTimeout" , func (t * testing.T ) {
4553+ // Test without timeout - should work normally
4554+ handle , err := RunWorkflow (dbosCtx , simpleWorkflow , "test" )
4555+ require .NoError (t , err , "failed to start workflow" )
4556+
4557+ result , err := handle .GetResult ()
4558+ require .NoError (t , err , "GetResult without timeout should succeed" )
4559+ assert .Equal (t , "test" , result )
4560+ })
4561+
4562+ t .Run ("WorkflowHandleGetResultAfterChannelClose" , func (t * testing.T ) {
4563+ // Test getting result after the outcome channel would be closed
4564+ handle , err := RunWorkflow (dbosCtx , simpleWorkflow , "test" )
4565+ require .NoError (t , err , "failed to start workflow" )
4566+
4567+ // Get result first time - this will close the outcome channel
4568+ result1 , err := handle .GetResult ()
4569+ require .NoError (t , err , "first GetResult should succeed" )
4570+ assert .Equal (t , "test" , result1 )
4571+
4572+ // Sleep briefly to ensure channel is closed
4573+ time .Sleep (10 * time .Millisecond )
4574+
4575+ // Get result second time - should fail since channel is closed
4576+ _ , err = handle .GetResult ()
4577+ require .Error (t , err , "second GetResult should fail" )
4578+ assert .Contains (t , err .Error (), "workflow result channel is already closed" )
4579+ })
4580+ }
4581+
4582+ func TestWorkflowPollingHandleTimeout (t * testing.T ) {
4583+ dbosCtx := setupDBOS (t , true , true )
4584+ RegisterWorkflow (dbosCtx , simpleWorkflow )
4585+
4586+ t .Run ("WorkflowPollingHandleTimeout" , func (t * testing.T ) {
4587+ // Test timeout on workflowPollingHandle (database polling)
4588+ handle , err := RunWorkflow (dbosCtx , simpleWorkflow , "test" )
4589+ require .NoError (t , err , "failed to start workflow" )
4590+
4591+ // Test with a very short timeout - should timeout
4592+ start := time .Now ()
4593+ _ , err = handle .GetResult (WithHandleTimeout (1 * time .Millisecond ))
4594+ duration := time .Since (start )
4595+
4596+ require .Error (t , err , "expected timeout error" )
4597+ assert .Contains (t , err .Error (), "workflow result timeout after 1ms" )
4598+ assert .True (t , duration < 100 * time .Millisecond , "timeout should occur quickly" )
4599+ })
4600+
4601+ t .Run ("WorkflowPollingHandleNoTimeout" , func (t * testing.T ) {
4602+ // Test without timeout - should work normally
4603+ handle , err := RunWorkflow (dbosCtx , simpleWorkflow , "test" )
4604+ require .NoError (t , err , "failed to start workflow" )
4605+
4606+ result , err := handle .GetResult ()
4607+ require .NoError (t , err , "GetResult without timeout should succeed" )
4608+ assert .Equal (t , "test" , result )
4609+ })
4610+ }
0 commit comments