Skip to content

Commit

Permalink
adds retry
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Oct 13, 2024
1 parent 6ce0247 commit c8e5f05
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions specs/test_dist.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// smoke tests to establish that the NPM code can be used
// & works as expected
import { delay_effect, map, join, run, repeat, fallthrough, every, any, all } from "../dist/delay.mjs"
import { delay_effect, map, join, run, repeat, fallthrough, every, any, all, retry } from "../dist/delay.mjs"
import { get, ok, error, isOk, toList } from "../dist/extras/extras.mjs"
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { test } from "@cross/test"
import { assertEquals, assertLess } from "@std/assert"

test("delay_effect", () => {
let fin = 0
Expand All @@ -29,15 +29,15 @@ test("delay_effect", () => {

test("map", () => {
let d1 = delay_effect(() => ok("HELLO"))
d1 = map(d1, (v) => ok(v + "WORLD"))
d1 = map(d1, (v) => ok(v + "!"))
d1 = map(d1, v => ok(v + "WORLD"))
d1 = map(d1, v => ok(v + "!"))

const res1 = get(run(d1))
assertEquals(res1, "HELLOWORLD!")

let d2 = delay_effect(() => ok("HELLO"))
d2 = map(d2, (v) => ok(v + "WORLD"))
d2 = map(d2, (_) => error("shit!"))
d2 = map(d2, v => ok(v + "WORLD"))
d2 = map(d2, _ => error("shit!"))

const res2 = get(run(d2))
assertEquals(res2, "shit!")
Expand All @@ -61,7 +61,7 @@ test("repeat", () => {

const res = repeat(d, 3)
.toArray()
.map((x) => get(x))
.map(x => get(x))

assertEquals(res, [3, 2, 1])
assertEquals(fin, 3)
Expand All @@ -78,8 +78,7 @@ test("every, any & all", () => {
return error("err!")
})

const res = every(toList([d, d, d]))
.toArray()
const res = every(toList([d, d, d])).toArray()

assertEquals(isOk(res[0]), true)
assertEquals(isOk(res[1]), false)
Expand Down Expand Up @@ -115,3 +114,19 @@ test("fallthrough", () => {
assertEquals(fin, 2)
})

test("retry", () => {
let ts = []
const d = delay_effect(() => {
ts.push(Date.now())
return error(ts.length)
})

assertEquals(ts, [])
const res = get(run(retry(d, 3, 250)))
assertEquals(ts.length, 3)
assertEquals(res, 3)

for(let i = 1; i < ts.length; i++) {
assertLess(Number(ts[i-1]), Number(ts[i]))
}
})

0 comments on commit c8e5f05

Please sign in to comment.