Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-sleep-in-loop-e2e-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@workflow/core': patch
---

Add e2e test validating that `sleep()` inside a loop with step calls correctly delays each iteration
13 changes: 13 additions & 0 deletions packages/core/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,19 @@ describe('e2e', () => {
}
);

test(
'sleepInLoopWorkflow - sleep inside loop with steps actually delays each iteration',
{ timeout: 60_000 },
async () => {
const run = await start(await e2e('sleepInLoopWorkflow'), []);
const returnValue = await run.returnValue;

// 3 iterations with 3s sleep between each pair = ~6s minimum total elapsed
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment to say "2.5s threshold per sleep to allow jitter" which matches the actual assertion.

expect(returnValue.timestamps).toHaveLength(3);
expect(returnValue.totalElapsed).toBeGreaterThan(5_000);
}
);

test(
'sleepWithSequentialStepsWorkflow - sequential steps work with concurrent sleep (control)',
{ timeout: 60_000 },
Expand Down
36 changes: 36 additions & 0 deletions workbench/example/workflows/99_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,42 @@ async function addNumbers(a: number, b: number) {
return a + b;
}

/**
* Validates that sleep() inside a loop with step calls actually delays
* execution on each iteration (i.e., sleeps are honored on replay, not skipped).
*
* Reproduces the scenario from a user report claiming that:
* for (let i = 0; i < N; i++) {
* await someStep();
* await sleep(duration);
* }
* ...fires all iterations instantly with zero delay.
*/
async function noopStep(iteration: number) {
'use step';
return { iteration, ts: Date.now() };
}

export async function sleepInLoopWorkflow() {
'use workflow';
const iterations = 3;
const sleepMs = 3_000; // 3s per iteration
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — changed to "3s between iterations (2 sleeps total)".

const timestamps: number[] = [];

for (let i = 0; i < iterations; i++) {
const result = await noopStep(i);
timestamps.push(result.ts);
if (i < iterations - 1) {
await sleep(sleepMs);
}
}

const totalElapsed = timestamps[timestamps.length - 1] - timestamps[0];
return { timestamps, totalElapsed };
}

//////////////////////////////////////////////////////////

/**
* Control workflow: sleep + sequential steps (no hooks).
* Proves that void sleep().then() does NOT interfere with sequential steps
Expand Down
Loading