From 149e9d3da032e5040993ab3934357d7ba52a1f3f Mon Sep 17 00:00:00 2001 From: Min Kim Date: Tue, 17 Dec 2024 09:26:33 -0500 Subject: [PATCH] Write some tests --- retry-backoff/retry-backoff.test.ts | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 retry-backoff/retry-backoff.test.ts diff --git a/retry-backoff/retry-backoff.test.ts b/retry-backoff/retry-backoff.test.ts new file mode 100644 index 0000000..fa03d4d --- /dev/null +++ b/retry-backoff/retry-backoff.test.ts @@ -0,0 +1,39 @@ +import { run } from "npm:effection@4.0.0-alpha.3"; +import { describe, it } from "bdd"; +import { expect } from "expect"; +import { useRetryWithBackoff } from "./retry-backoff.ts" + +describe("RetryBackoff", () => { + it("retries operation and returns output if operation finishes on time", async () => { + await run(function* () { + let attempts = 0; + let result = 0; + yield* useRetryWithBackoff(function* () { + if (attempts < 2) { + attempts++; + throw new Error("operation failed"); + } else { + result = 1; + } + }, { timeout: 2_000 }); + expect(attempts).toBe(2); + expect(result).toBe(1); + }) + }); + + it("retries operation and handles timeout when operation exceeds limit", async () => { + await run(function* () { + let attempts = 0; + let result = 0; + yield* useRetryWithBackoff(function* () { + if (attempts < 2) { + attempts++; + throw new Error("operation failed"); + } else { + result = 1; + } + }, { timeout: 500 }); + expect(result).toBe(0); + }) + }); +});