Skip to content

Commit 7cef987

Browse files
committed
test scheduled workflow
1 parent 713e6d2 commit 7cef987

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

chaos_tests/chaos_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ func TestChaosWorkflow(t *testing.T) {
206206
defer cancel()
207207
PostgresChaosMonkey(t, ctx, &wg)
208208

209+
// Define scheduled workflow that runs every second
210+
scheduledWorkflow := func(ctx dbos.DBOSContext, scheduledTime time.Time) (struct{}, error) {
211+
return struct{}{}, nil
212+
}
213+
209214
// Define step functions
210215
stepOne := func(_ context.Context, x int) (int, error) {
211216
return x + 1, nil
@@ -236,8 +241,10 @@ func TestChaosWorkflow(t *testing.T) {
236241
return x, nil
237242
}
238243

239-
// Register the workflow
244+
// Register the workflows
240245
dbos.RegisterWorkflow(dbosCtx, workflow)
246+
// Register scheduled workflow to run every second for chaos testing
247+
dbos.RegisterWorkflow(dbosCtx, scheduledWorkflow, dbos.WithSchedule("* * * * * *"), dbos.WithWorkflowName("ScheduledChaosTest"))
241248

242249
err := dbosCtx.Launch()
243250
require.NoError(t, err)
@@ -255,6 +262,25 @@ func TestChaosWorkflow(t *testing.T) {
255262
require.NoError(t, err, "failed to get result for workflow %d", i)
256263
assert.Equal(t, i+3, result, "unexpected result for workflow %d", i)
257264
}
265+
266+
// Validate scheduled workflow executions using ListWorkflows
267+
scheduledWorkflows, err := dbos.ListWorkflows(dbosCtx,
268+
dbos.WithName("ScheduledChaosTest"),
269+
dbos.WithStatus([]dbos.WorkflowStatusType{dbos.WorkflowStatusSuccess}),
270+
dbos.WithSortDesc(),
271+
dbos.WithLimit(1),
272+
dbos.WithLoadInput(false),
273+
dbos.WithLoadOutput(false),
274+
)
275+
require.NoError(t, err, "failed to list scheduled workflows")
276+
277+
assert.Equal(t, len(scheduledWorkflows), 1, "Expected exactly one scheduled workflow execution")
278+
279+
// Check the last execution was within 10 seconds -- reasonable for a 1 second schedule and 2 seconds postgres downtime
280+
latestWorkflow := scheduledWorkflows[0] // Sorted descending
281+
timeSinceLastExecution := time.Since(latestWorkflow.CreatedAt)
282+
assert.Less(t, timeSinceLastExecution, 10*time.Second,
283+
"Last scheduled execution was %v ago, expected less than 60 seconds", timeSinceLastExecution)
258284
}
259285

260286
// Test send/recv functionality

0 commit comments

Comments
 (0)