Skip to content

Commit 48aaa27

Browse files
committed
recovery tests
1 parent 8e2152e commit 48aaa27

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

dbos/workflows_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,4 +2279,59 @@ func TestWorkflowTimeout(t *testing.T) {
22792279
t.Fatalf("expected workflow status to be WorkflowStatusCancelled, got %v", status.Status)
22802280
}
22812281
})
2282+
2283+
t.Run("RecoverWaitForCancelWorkflow", func(t *testing.T) {
2284+
start := time.Now()
2285+
timeout := 1 * time.Second
2286+
cancelCtx, cancelFunc := WithTimeout(dbosCtx, timeout)
2287+
defer cancelFunc()
2288+
handle, err := RunAsWorkflow(cancelCtx, waitForCancelWorkflow, "recover-wait-for-cancel")
2289+
if err != nil {
2290+
t.Fatalf("failed to start wait for cancel workflow: %v", err)
2291+
}
2292+
2293+
// Recover the pending workflow
2294+
recoveredHandles, err := recoverPendingWorkflows(dbosCtx.(*dbosContext), []string{"local"})
2295+
if err != nil {
2296+
t.Fatalf("failed to recover pending workflows: %v", err)
2297+
}
2298+
if len(recoveredHandles) != 1 {
2299+
t.Fatalf("expected 1 recovered handle, got %d", len(recoveredHandles))
2300+
}
2301+
recoveredHandle := recoveredHandles[0]
2302+
if recoveredHandle.GetWorkflowID() != handle.GetWorkflowID() {
2303+
t.Fatalf("expected recovered handle to have ID %s, got %s", handle.GetWorkflowID(), recoveredHandle.GetWorkflowID())
2304+
}
2305+
2306+
// Wait for the workflow to complete and check the result. Should we AwaitedWorkflowCancelled
2307+
result, err := recoveredHandle.GetResult()
2308+
if result != "" {
2309+
t.Fatalf("expected result to be an empty string, got '%s'", result)
2310+
}
2311+
// Check the error type
2312+
dbosErr, ok := err.(*DBOSError)
2313+
if !ok {
2314+
t.Fatalf("expected error to be of type *DBOSError, got %T", err)
2315+
}
2316+
2317+
if dbosErr.Code != AwaitedWorkflowCancelled {
2318+
t.Fatalf("expected error code to be AwaitedWorkflowCancelled, got %v", dbosErr.Code)
2319+
}
2320+
2321+
// Check the workflow status: should be cancelled
2322+
status, err := recoveredHandle.GetStatus()
2323+
if err != nil {
2324+
t.Fatalf("failed to get recovered workflow status: %v", err)
2325+
}
2326+
if status.Status != WorkflowStatusCancelled {
2327+
t.Fatalf("expected recovered workflow status to be WorkflowStatusCancelled, got %v", status.Status)
2328+
}
2329+
2330+
// Check the deadline on the status was is within an expected range (start time + timeout * .1)
2331+
// XXX this might be flaky and frankly not super useful
2332+
expectedDeadline := start.Add(timeout * 10 / 100)
2333+
if status.Deadline.Before(expectedDeadline) || status.Deadline.After(start.Add(timeout)) {
2334+
t.Fatalf("expected workflow deadline to be within %v and %v, got %v", expectedDeadline, start.Add(timeout), status.Deadline)
2335+
}
2336+
})
22822337
}

0 commit comments

Comments
 (0)