Skip to content

Commit d43d514

Browse files
committed
test recv context cancellation
1 parent ba5bc97 commit d43d514

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dbos/workflows_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,15 @@ type sendRecvType struct {
14771477
Value string
14781478
}
14791479

1480+
func recvContextCancelWorkflow(ctx DBOSContext, topic string) (string, error) {
1481+
// Try to receive with a 5 second timeout, but context will cancel before that
1482+
msg, err := Recv[string](ctx, topic, 5*time.Second)
1483+
if err != nil {
1484+
return "", err
1485+
}
1486+
return msg, nil
1487+
}
1488+
14801489
func TestSendRecv(t *testing.T) {
14811490
dbosCtx := setupDBOS(t, true, true)
14821491

@@ -1490,6 +1499,7 @@ func TestSendRecv(t *testing.T) {
14901499
RegisterWorkflow(dbosCtx, receiveIdempotencyWorkflow)
14911500
RegisterWorkflow(dbosCtx, durableRecvSleepWorkflow)
14921501
RegisterWorkflow(dbosCtx, workflowThatCallsSendInStep)
1502+
RegisterWorkflow(dbosCtx, recvContextCancelWorkflow)
14931503

14941504
dbosCtx.Launch()
14951505

@@ -1938,6 +1948,27 @@ func TestSendRecv(t *testing.T) {
19381948
require.Equal(t, 3, steps[3].StepID, "expected fourth step ID to be 3")
19391949
require.Equal(t, "DBOS.sleep", steps[3].StepName, "expected fourth step to be sleep")
19401950
})
1951+
1952+
t.Run("RecvContextCancellation", func(t *testing.T) {
1953+
// Create a context with a shorter timeout than the Recv timeout (1s < 5s)
1954+
timeoutCtx, cancel := WithTimeout(dbosCtx, 1*time.Second)
1955+
defer cancel()
1956+
1957+
// Start the workflow with the timeout context
1958+
handle, err := RunWorkflow(timeoutCtx, recvContextCancelWorkflow, "context-cancel-topic")
1959+
require.NoError(t, err, "failed to start recv context cancel workflow")
1960+
1961+
// Get the result - should fail with context deadline exceeded
1962+
result, err := handle.GetResult()
1963+
require.Error(t, err, "expected error from context cancellation")
1964+
require.True(t, errors.Is(err, context.DeadlineExceeded), "expected context.DeadlineExceeded error, got: %v", err)
1965+
require.Equal(t, "", result, "expected empty result when context cancelled")
1966+
1967+
// Verify the workflow status is cancelled
1968+
status, err := handle.GetStatus()
1969+
require.NoError(t, err, "failed to get workflow status")
1970+
require.Equal(t, WorkflowStatusCancelled, status.Status, "expected workflow status to be WorkflowStatusCancelled")
1971+
})
19411972
}
19421973

19431974
var (

0 commit comments

Comments
 (0)