-
Notifications
You must be signed in to change notification settings - Fork 216
test: validate sleep() works correctly inside loops #1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b38d7ac
0070fa2
375404e
c7488bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
||
| expect(returnValue.timestamps).toHaveLength(3); | ||
VaguelySerious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(returnValue.totalElapsed).toBeGreaterThan(5_000); | ||
| } | ||
| ); | ||
|
|
||
| test( | ||
| 'sleepWithSequentialStepsWorkflow - sequential steps work with concurrent sleep (control)', | ||
| { timeout: 60_000 }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
||
| 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.