Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Runware/async-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const asyncRetry = async (
maxRetries--;
if (maxRetries > 0) {
await delay(delayInSeconds); // Delay before the next retry
await asyncRetry(apiCall, { ...options, maxRetries });
// Continue loop for next retry attempt
} else {
throw error; // Throw the error if max retries are reached
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Runware/async-retry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test, describe, vi } from "vitest";
import { asyncRetry } from "../../Runware/async-retry";

describe("asyncRetry - bug tests (these fail due to loop+recursion bug)", () => {
test("should properly return result from recursive call", async () => {
let callCount = 0;
const apiCall = vi.fn(async () => {
callCount++;
if (callCount < 3) {
throw new Error("Failed");
}
return "success";
});

// This should succeed on the 3rd attempt
const result = await asyncRetry(apiCall, { maxRetries: 3 });

expect(result).toBe("success");
expect(apiCall).toHaveBeenCalledTimes(3);
});

test("should not continue loop after successful recursive call", async () => {
let callCount = 0;
const apiCall = vi.fn(async () => {
callCount++;
if (callCount === 1) {
throw new Error("First attempt fails");
}
return `success on attempt ${callCount}`;
});

// With maxRetries=2, should succeed on 2nd attempt
const result = await asyncRetry(apiCall, { maxRetries: 2 });

expect(result).toBe("success on attempt 2");
expect(apiCall).toHaveBeenCalledTimes(2);
});
});