Skip to content

Commit b4fd76d

Browse files
authored
fix: don't sleep after the last retry attempt (#52)
1 parent 6d1c549 commit b4fd76d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/http/index.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ describe("Abort", () => {
5353
expect(bodyResponse.result).toEqual("Abort works!");
5454
});
5555
});
56+
57+
describe.only("retry", () => {
58+
test("should terminate after sleeping 5 times", async () => {
59+
// init a cient which will always get errors
60+
const client = newHttpClient(
61+
{
62+
retries: 5,
63+
backoff: (retryCount) => Math.exp(retryCount) * 50,
64+
},
65+
{
66+
url: "",
67+
token: "non-existent",
68+
}
69+
);
70+
71+
// get should take 4.287 seconds and terminate before the timeout.
72+
const throws = () =>
73+
Promise.race([
74+
client.request({
75+
path: ["upsert"],
76+
body: "wrong-body",
77+
}),
78+
new Promise((r) => setTimeout(r, 4500)),
79+
]);
80+
81+
// if the Promise.race doesn't throw, that means the retries took longer than 4.5s
82+
expect(throws).toThrow("fetch() URL is invalid");
83+
});
84+
});

src/http/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ export class HttpClient implements Requester {
132132
break;
133133
}
134134
error = error_ as Error;
135-
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
135+
if (i < this.retry.attempts) {
136+
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
137+
}
136138
}
137139
}
138140
if (!res) {

0 commit comments

Comments
 (0)