Skip to content

Commit ab9d45d

Browse files
committed
test: add more tests for attempt
1 parent edbfad2 commit ab9d45d

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

src/lib/result.test.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ describe("Result", () => {
244244
called++;
245245
return Result.err("error message");
246246
},
247-
{ timeout: 0, backoff: 0 }
247+
{ timeout: 0, backoff: 0 },
248248
);
249249

250250
assert.strictEqual(res.err, true);
@@ -259,13 +259,48 @@ describe("Result", () => {
259259
called++;
260260
return Result.err("error message");
261261
},
262-
{ attempts: 5, timeout: 0, backoff: 0 }
262+
{ attempts: 5, timeout: 0, backoff: 0 },
263263
);
264264

265265
assert.strictEqual(res.err, true);
266266
assert.strictEqual(called, 5);
267267
});
268268

269+
it("should stop retrying after a successful result", async () => {
270+
let called = 0;
271+
272+
const res = await Result.attempt(
273+
async () => {
274+
called++;
275+
if (called < 3) {
276+
return Result.err("temporary error");
277+
}
278+
279+
return Result.ok("done");
280+
},
281+
{ attempts: 5, timeout: 0, backoff: 0 },
282+
);
283+
284+
assert.strictEqual(res.ok, true);
285+
assert.strictEqual(res.val, "done");
286+
assert.strictEqual(called, 3);
287+
});
288+
289+
it("should mark attempted when retries are exhausted", async () => {
290+
let called = 0;
291+
const res = await Result.attempt(
292+
async () => {
293+
called++;
294+
return Result.err("still failing");
295+
},
296+
{ attempts: 2, timeout: 0, backoff: 0 },
297+
);
298+
299+
assert.strictEqual(res.err, true);
300+
assert.strictEqual(res.attempted, true);
301+
assert.strictEqual(called, 2);
302+
});
303+
269304
it("should not re-attempt an Err if it has already been attempted", async () => {
270305
let called = 0;
271306

@@ -279,7 +314,7 @@ describe("Result", () => {
279314

280315
return err;
281316
},
282-
{ timeout: 0, backoff: 0 }
317+
{ timeout: 0, backoff: 0 },
283318
);
284319

285320
assert.strictEqual(res.err, true);
@@ -332,7 +367,7 @@ describe("Result", () => {
332367
assert.deepStrictEqual(result.unwrap(), complexObj);
333368
assert.strictEqual(
334369
result.toString(),
335-
`Ok({"id":1,"name":"test","data":[1,2,3]})`
370+
`Ok({"id":1,"name":"test","data":[1,2,3]})`,
336371
);
337372
});
338373
});

0 commit comments

Comments
 (0)