mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
1051 lines
32 KiB
TypeScript
1051 lines
32 KiB
TypeScript
import * as Cause from "effect/Cause"
|
|
import * as Context from "effect/Context"
|
|
import * as Data from "effect/Data"
|
|
import * as Effect from "effect/Effect"
|
|
import * as Either from "effect/Either"
|
|
import * as Exit from "effect/Exit"
|
|
import * as Fiber from "effect/Fiber"
|
|
import { pipe } from "effect/Function"
|
|
import * as Option from "effect/Option"
|
|
import * as Scope from "effect/Scope"
|
|
import * as TestClock from "effect/TestClock"
|
|
import { assert, describe, it } from "./utils/extend.js"
|
|
|
|
class ATag extends Context.Tag("ATag")<ATag, "A">() {}
|
|
|
|
describe.concurrent("Effect", () => {
|
|
it("runPromise", async () => {
|
|
const result = await Effect.runPromise(Effect.succeed(1))
|
|
assert.strictEqual(result, 1)
|
|
})
|
|
|
|
it("acquireUseRelease interrupt", async () => {
|
|
let acquire = false
|
|
let use = false
|
|
let release = false
|
|
const fiber = Effect.acquireUseRelease(
|
|
Effect.sync(() => {
|
|
acquire = true
|
|
return 123
|
|
}).pipe(Effect.delay(100)),
|
|
() =>
|
|
Effect.sync(() => {
|
|
use = true
|
|
}),
|
|
(_) =>
|
|
Effect.sync(() => {
|
|
assert.strictEqual(_, 123)
|
|
release = true
|
|
})
|
|
).pipe(Effect.runFork)
|
|
fiber.unsafeInterrupt()
|
|
const result = await Effect.runPromise(Fiber.await(fiber))
|
|
assert.deepStrictEqual(result, Exit.failCause(Cause.interrupt()))
|
|
assert.isTrue(acquire)
|
|
assert.isFalse(use)
|
|
assert.isTrue(release)
|
|
})
|
|
|
|
it("acquireUseRelease uninterruptible", async () => {
|
|
let acquire = false
|
|
let use = false
|
|
let release = false
|
|
const fiber = Effect.acquireUseRelease(
|
|
Effect.sync(() => {
|
|
acquire = true
|
|
return 123
|
|
}).pipe(Effect.delay(100)),
|
|
(_) =>
|
|
Effect.sync(() => {
|
|
use = true
|
|
return _
|
|
}),
|
|
(_) =>
|
|
Effect.sync(() => {
|
|
assert.strictEqual(_, 123)
|
|
release = true
|
|
})
|
|
).pipe(Effect.uninterruptible, Effect.runFork)
|
|
fiber.unsafeInterrupt()
|
|
const result = await Effect.runPromise(Fiber.await(fiber))
|
|
assert.deepStrictEqual(result, Exit.failCause(Cause.interrupt()))
|
|
assert.isTrue(acquire)
|
|
assert.isTrue(use)
|
|
assert.isTrue(release)
|
|
})
|
|
|
|
it("Context.Tag", () =>
|
|
Effect.service(ATag).pipe(
|
|
Effect.tap((_) => Effect.sync(() => assert.strictEqual(_, "A"))),
|
|
Effect.provideService(ATag, "A"),
|
|
Effect.runPromise
|
|
))
|
|
|
|
describe("fromOption", () => {
|
|
it("from a some", () =>
|
|
Option.some("A").pipe(
|
|
Effect.fromOption,
|
|
Effect.tap((_) => assert.strictEqual(_, "A")),
|
|
Effect.runPromise
|
|
))
|
|
|
|
it("from a none", () =>
|
|
Option.none().pipe(
|
|
Effect.fromOption,
|
|
Effect.flip,
|
|
Effect.tap((error) => assert.ok(error instanceof Cause.NoSuchElementError)),
|
|
Effect.runPromise
|
|
))
|
|
})
|
|
|
|
describe("fromEither", () => {
|
|
it("from a right", () =>
|
|
Either.right("A").pipe(
|
|
Effect.fromEither,
|
|
Effect.tap((_) => Effect.sync(() => assert.strictEqual(_, "A"))),
|
|
Effect.runPromise
|
|
))
|
|
|
|
it("from a left", () =>
|
|
Either.left("error").pipe(
|
|
Effect.fromEither,
|
|
Effect.flip,
|
|
Effect.tap((error) => Effect.sync(() => assert.strictEqual(error, "error"))),
|
|
Effect.runPromise
|
|
))
|
|
})
|
|
|
|
describe("gen", () => {
|
|
it("gen", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.succeed(1)
|
|
assert.strictEqual(result, 1)
|
|
return result
|
|
}).pipe(Effect.runPromise).then((_) => assert.deepStrictEqual(_, 1)))
|
|
|
|
it("gen with context", () =>
|
|
Effect.gen({ a: 1, b: 2 }, function*() {
|
|
const result = yield* Effect.succeed(this.a)
|
|
assert.strictEqual(result, 1)
|
|
return result + this.b
|
|
}).pipe(Effect.runPromise).then((_) => assert.deepStrictEqual(_, 3)))
|
|
})
|
|
|
|
describe("forEach", () => {
|
|
it("sequential", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.forEach([1, 2, 3], (_) => Effect.succeed(_))
|
|
assert.deepStrictEqual(results, [1, 2, 3])
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it("unbounded", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.forEach([1, 2, 3], (_) => Effect.succeed(_), { concurrency: "unbounded" })
|
|
assert.deepStrictEqual(results, [1, 2, 3])
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it("bounded", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.forEach([1, 2, 3, 4, 5], (_) => Effect.succeed(_), { concurrency: 2 })
|
|
assert.deepStrictEqual(results, [1, 2, 3, 4, 5])
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it.effect("inherit unbounded", () =>
|
|
Effect.gen(function*() {
|
|
const handle = yield* Effect.forEach([1, 2, 3], (_) => Effect.succeed(_).pipe(Effect.delay(50)), {
|
|
concurrency: "inherit"
|
|
}).pipe(
|
|
Effect.withConcurrency("unbounded"),
|
|
Effect.fork
|
|
)
|
|
yield* TestClock.adjust(90)
|
|
assert.deepStrictEqual(handle.unsafePoll(), Exit.succeed([1, 2, 3]))
|
|
}))
|
|
|
|
it.effect("sequential interrupt", () =>
|
|
Effect.gen(function*() {
|
|
const done: Array<number> = []
|
|
const fiber = yield* Effect.forEach([1, 2, 3, 4, 5, 6], (i) =>
|
|
Effect.sync(() => {
|
|
done.push(i)
|
|
return i
|
|
}).pipe(Effect.delay(300))).pipe(Effect.fork)
|
|
yield* TestClock.adjust(800)
|
|
yield* Fiber.interrupt(fiber)
|
|
const result = yield* Fiber.await(fiber)
|
|
assert.isTrue(Exit.hasInterrupt(result))
|
|
assert.deepStrictEqual(done, [1, 2])
|
|
}))
|
|
|
|
it.effect("unbounded interrupt", () =>
|
|
Effect.gen(function*() {
|
|
const done: Array<number> = []
|
|
const fiber = yield* Effect.forEach([1, 2, 3], (i) =>
|
|
Effect.sync(() => {
|
|
done.push(i)
|
|
return i
|
|
}).pipe(Effect.delay(150)), { concurrency: "unbounded" }).pipe(Effect.fork)
|
|
yield* TestClock.adjust(50)
|
|
yield* Fiber.interrupt(fiber)
|
|
const result = yield* Fiber.await(fiber)
|
|
assert.isTrue(Exit.hasInterrupt(result))
|
|
assert.deepStrictEqual(done, [])
|
|
}))
|
|
|
|
it.effect("bounded interrupt", () =>
|
|
Effect.gen(function*() {
|
|
const done: Array<number> = []
|
|
const fiber = yield* Effect.forEach([1, 2, 3, 4, 5, 6], (i) =>
|
|
Effect.sync(() => {
|
|
done.push(i)
|
|
return i
|
|
}).pipe(Effect.delay(200)), { concurrency: 2 }).pipe(Effect.fork)
|
|
yield* TestClock.adjust(350)
|
|
yield* Fiber.interrupt(fiber)
|
|
const result = yield* Fiber.await(fiber)
|
|
assert.isTrue(Exit.hasInterrupt(result))
|
|
assert.deepStrictEqual(done, [1, 2])
|
|
}))
|
|
|
|
it("unbounded fail", () =>
|
|
Effect.gen(function*() {
|
|
const done: Array<number> = []
|
|
const handle = yield* Effect.forEach([1, 2, 3, 4, 5], (i) =>
|
|
Effect.suspend(() => {
|
|
done.push(i)
|
|
return i === 3 ? Effect.fail("error") : Effect.succeed(i)
|
|
}).pipe(Effect.delay(i * 100)), {
|
|
concurrency: "unbounded"
|
|
}).pipe(Effect.fork)
|
|
const result = yield* Fiber.await(handle)
|
|
assert.deepStrictEqual(result, Exit.fail("error"))
|
|
assert.deepStrictEqual(done, [1, 2, 3])
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it("length = 0", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.forEach([], (_) => Effect.succeed(_))
|
|
assert.deepStrictEqual(results, [])
|
|
}).pipe(Effect.runPromise))
|
|
})
|
|
|
|
describe("all", () => {
|
|
it("tuple", () =>
|
|
Effect.gen(function*() {
|
|
const results = (yield* Effect.all([
|
|
Effect.succeed(1),
|
|
Effect.succeed(2),
|
|
Effect.succeed(3)
|
|
])) satisfies [
|
|
number,
|
|
number,
|
|
number
|
|
]
|
|
assert.deepStrictEqual(results, [1, 2, 3])
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it("record", () =>
|
|
Effect.gen(function*() {
|
|
const results = (yield* Effect.all({
|
|
a: Effect.succeed(1),
|
|
b: Effect.succeed("2"),
|
|
c: Effect.succeed(true)
|
|
})) satisfies {
|
|
a: number
|
|
b: string
|
|
c: boolean
|
|
}
|
|
assert.deepStrictEqual(results, {
|
|
a: 1,
|
|
b: "2",
|
|
c: true
|
|
})
|
|
}).pipe(Effect.runPromise))
|
|
|
|
it.effect("record discard", () =>
|
|
Effect.gen(function*() {
|
|
const results = (yield* Effect.all({
|
|
a: Effect.succeed(1),
|
|
b: Effect.succeed("2"),
|
|
c: Effect.succeed(true)
|
|
}, { discard: true })) satisfies void
|
|
assert.deepStrictEqual(results, void 0)
|
|
}))
|
|
|
|
it.effect("iterable", () =>
|
|
Effect.gen(function*() {
|
|
const results = (yield* Effect.all(
|
|
new Set([
|
|
Effect.succeed(1),
|
|
Effect.succeed(2),
|
|
Effect.succeed(3)
|
|
])
|
|
)) satisfies Array<number>
|
|
assert.deepStrictEqual(results, [1, 2, 3])
|
|
}))
|
|
})
|
|
|
|
describe("filter", () => {
|
|
it.live("odd numbers", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.filter([1, 2, 3, 4, 5], (_) => Effect.succeed(_ % 2 === 1))
|
|
assert.deepStrictEqual(results, [1, 3, 5])
|
|
}))
|
|
|
|
it.live("iterable", () =>
|
|
Effect.gen(function*() {
|
|
const results = yield* Effect.filter(new Set([1, 2, 3, 4, 5]), (_) => Effect.succeed(_ % 2 === 1))
|
|
assert.deepStrictEqual(results, [1, 3, 5])
|
|
}))
|
|
})
|
|
|
|
describe("acquireRelease", () => {
|
|
it("releases on interrupt", () =>
|
|
Effect.gen(function*() {
|
|
let release = false
|
|
const fiber = yield* Effect.acquireRelease(
|
|
Effect.delay(Effect.succeed("foo"), 100),
|
|
() =>
|
|
Effect.sync(() => {
|
|
release = true
|
|
})
|
|
).pipe(Effect.scoped, Effect.fork)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
fiber.unsafeInterrupt()
|
|
yield* Fiber.await(fiber)
|
|
assert.strictEqual(release, true)
|
|
}).pipe(Effect.runPromise))
|
|
})
|
|
|
|
it.effect("raceAll", () =>
|
|
Effect.gen(function*() {
|
|
const interrupted: Array<number> = []
|
|
const fiber = yield* Effect.raceAll([500, 300, 200, 0, 100].map((ms) =>
|
|
(ms === 0 ? Effect.fail("boom") : Effect.succeed(ms)).pipe(
|
|
Effect.delay(ms),
|
|
Effect.onInterrupt(
|
|
Effect.sync(() => {
|
|
interrupted.push(ms)
|
|
})
|
|
)
|
|
)
|
|
)).pipe(Effect.fork)
|
|
yield* TestClock.adjust("500 millis")
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.strictEqual(result, 100)
|
|
assert.deepStrictEqual(interrupted, [500, 300, 200])
|
|
}))
|
|
|
|
it.effect.skip("raceAllFirst", () =>
|
|
Effect.gen(function*() {
|
|
const interrupted: Array<number> = []
|
|
const fiber = yield* Effect.raceAllFirst([500, 300, 200, 0, 100].map((ms) =>
|
|
(ms === 0 ? Effect.fail("boom") : Effect.succeed(ms)).pipe(
|
|
Effect.delay(ms),
|
|
Effect.onInterrupt(
|
|
Effect.sync(() => {
|
|
interrupted.push(ms)
|
|
})
|
|
)
|
|
)
|
|
)).pipe(Effect.exit, Effect.fork)
|
|
yield* TestClock.adjust("500 millis")
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, Exit.fail("boom"))
|
|
assert.deepStrictEqual(interrupted, [500, 300, 200, 100])
|
|
}))
|
|
|
|
describe("repeat", () => {
|
|
it.effect("is stack safe", () =>
|
|
Effect.void.pipe(
|
|
Effect.repeat({ times: 10_000 })
|
|
))
|
|
|
|
it.effect("is interruptible", () =>
|
|
Effect.gen(function*() {
|
|
const fiber = yield* Effect.void.pipe(
|
|
Effect.forever,
|
|
Effect.timeoutOption(50),
|
|
Effect.fork
|
|
)
|
|
yield* TestClock.adjust(50)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, Option.none())
|
|
}))
|
|
|
|
it("works with runSync", () => {
|
|
const result = Effect.succeed(123).pipe(
|
|
Effect.repeat({ times: 1000 }),
|
|
Effect.runSync
|
|
)
|
|
assert.deepStrictEqual(result, 123)
|
|
})
|
|
|
|
// it.effect("scheduleRecurs", () =>
|
|
// Effect.gen(function*() {
|
|
// let count = 0
|
|
// yield* Effect.sync(() => count++).pipe(
|
|
// Effect.repeat({
|
|
// schedule: Effect.scheduleRecurs(3)
|
|
// })
|
|
// )
|
|
// assert.deepStrictEqual(count, 4)
|
|
// }))
|
|
})
|
|
|
|
describe("retry", () => {
|
|
it.live("nothing on success", () =>
|
|
Effect.gen(function*() {
|
|
let count = 0
|
|
yield* Effect.sync(() => count++).pipe(
|
|
Effect.retry({ times: 10000 })
|
|
)
|
|
assert.strictEqual(count, 1)
|
|
}))
|
|
|
|
it.effect("initial + retries", () =>
|
|
Effect.gen(function*() {
|
|
let count = 0
|
|
const error = yield* Effect.failSync(() => ++count).pipe(
|
|
Effect.retry({ times: 2 }),
|
|
Effect.flip
|
|
)
|
|
assert.strictEqual(error, 3)
|
|
}))
|
|
|
|
it.effect("predicate", () =>
|
|
Effect.gen(function*() {
|
|
let count = 0
|
|
const error = yield* Effect.failSync(() => ++count).pipe(
|
|
Effect.retry({ while: (i) => i < 3 }),
|
|
Effect.flip
|
|
)
|
|
assert.strictEqual(error, 3)
|
|
}))
|
|
})
|
|
|
|
describe("timeoutOption", () => {
|
|
it.live("timeout a long computation", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.sleep(60_000),
|
|
Effect.andThen(Effect.succeed(true)),
|
|
Effect.timeoutOption(10)
|
|
)
|
|
assert.deepStrictEqual(result, Option.none())
|
|
}))
|
|
it.live("timeout a long computation with a failure", () =>
|
|
Effect.gen(function*() {
|
|
const error = new Error("boom")
|
|
const result = yield* pipe(
|
|
Effect.sleep(5000),
|
|
Effect.andThen(Effect.succeed(true)),
|
|
Effect.timeoutOrElse({
|
|
onTimeout: () => Effect.die(error),
|
|
duration: 10
|
|
}),
|
|
Effect.sandbox,
|
|
Effect.flip
|
|
)
|
|
assert.deepStrictEqual(result, Cause.die(error))
|
|
}))
|
|
it.effect("timeout repetition of uninterruptible effect", () =>
|
|
Effect.gen(function*() {
|
|
const fiber = yield* pipe(
|
|
Effect.void,
|
|
Effect.uninterruptible,
|
|
Effect.forever,
|
|
Effect.timeoutOption(10),
|
|
Effect.fork
|
|
)
|
|
yield* TestClock.adjust(10)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, Option.none())
|
|
}))
|
|
it.effect("timeout in uninterruptible region", () =>
|
|
Effect.gen(function*() {
|
|
yield* Effect.void.pipe(Effect.timeoutOption(20_000), Effect.uninterruptible)
|
|
}), { timeout: 1000 })
|
|
})
|
|
|
|
describe("timeout", () => {
|
|
it.live("timeout a long computation", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.sleep(60_000),
|
|
Effect.andThen(Effect.succeed(true)),
|
|
Effect.timeout(10),
|
|
Effect.flip
|
|
)
|
|
assert.deepStrictEqual(result, new Cause.TimeoutError())
|
|
}))
|
|
})
|
|
|
|
describe("interruption", () => {
|
|
it.effect("sync forever is interruptible", () =>
|
|
Effect.gen(function*() {
|
|
const fiber = yield* pipe(Effect.succeed(1), Effect.forever, Effect.fork)
|
|
yield* Fiber.interrupt(fiber)
|
|
assert(Exit.hasInterrupt(fiber.unsafePoll()!))
|
|
}))
|
|
|
|
it.effect("interrupt of never is interrupted with cause", () =>
|
|
Effect.gen(function*() {
|
|
const fiber = yield* Effect.fork(Effect.never)
|
|
yield* Fiber.interrupt(fiber)
|
|
assert(Exit.hasInterrupt(fiber.unsafePoll()!))
|
|
}))
|
|
|
|
it.effect("catch + ensuring + interrupt", () =>
|
|
Effect.gen(function*() {
|
|
let catchFailure = false
|
|
let ensuring = false
|
|
const handle = yield* Effect.never.pipe(
|
|
Effect.catchCause((_) =>
|
|
Effect.sync(() => {
|
|
catchFailure = true
|
|
})
|
|
),
|
|
Effect.ensuring(Effect.sync(() => {
|
|
ensuring = true
|
|
})),
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(handle)
|
|
assert.isFalse(catchFailure)
|
|
assert.isTrue(ensuring)
|
|
}))
|
|
|
|
it.effect("run of interruptible", () =>
|
|
Effect.gen(function*() {
|
|
let recovered = false
|
|
const fiber = yield* Effect.never.pipe(
|
|
Effect.interruptible,
|
|
Effect.exit,
|
|
Effect.flatMap((result) =>
|
|
Effect.sync(() => {
|
|
recovered = result._tag === "Failure" && Cause.isInterruptedOnly(result.cause)
|
|
})
|
|
),
|
|
Effect.uninterruptible,
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.isTrue(recovered)
|
|
}))
|
|
|
|
it.effect("alternating interruptibility", () =>
|
|
Effect.gen(function*() {
|
|
let counter = 0
|
|
const fiber = yield* Effect.never.pipe(
|
|
Effect.interruptible,
|
|
Effect.exit,
|
|
Effect.andThen(Effect.sync(() => {
|
|
counter++
|
|
})),
|
|
Effect.uninterruptible,
|
|
Effect.interruptible,
|
|
Effect.exit,
|
|
Effect.andThen(Effect.sync(() => {
|
|
counter++
|
|
})),
|
|
Effect.uninterruptible,
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.strictEqual(counter, 2)
|
|
}))
|
|
|
|
it.live("acquireUseRelease use inherits interrupt status", () =>
|
|
Effect.gen(function*() {
|
|
let ref = false
|
|
const fiber = yield* Effect.acquireUseRelease(
|
|
Effect.succeed(123),
|
|
(_) =>
|
|
Effect.sync(() => {
|
|
ref = true
|
|
}).pipe(
|
|
Effect.delay(10)
|
|
),
|
|
() => Effect.void
|
|
).pipe(
|
|
Effect.uninterruptible,
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.isTrue(ref)
|
|
}))
|
|
|
|
it.live("async can be uninterruptible", () =>
|
|
Effect.gen(function*() {
|
|
let ref = false
|
|
const fiber = yield* Effect.sleep(10).pipe(
|
|
Effect.andThen(() => {
|
|
ref = true
|
|
}),
|
|
Effect.uninterruptible,
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.isTrue(ref)
|
|
}))
|
|
|
|
it.live("async cannot resume on interrupt", () =>
|
|
Effect.gen(function*() {
|
|
const fiber = yield* Effect.async<string>((resume) => {
|
|
setTimeout(() => {
|
|
resume(Effect.succeed("foo"))
|
|
}, 10)
|
|
}).pipe(
|
|
Effect.onInterrupt(Effect.sleep(30)),
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.isTrue(Exit.hasInterrupt(fiber.unsafePoll()!))
|
|
}))
|
|
|
|
it.live("closing scope is uninterruptible", () =>
|
|
Effect.gen(function*() {
|
|
let ref = false
|
|
const child = pipe(
|
|
Effect.sleep(10),
|
|
Effect.andThen(() => {
|
|
ref = true
|
|
})
|
|
)
|
|
const fiber = yield* child.pipe(Effect.uninterruptible, Effect.fork)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.isTrue(ref)
|
|
}))
|
|
|
|
it.effect("AbortSignal is aborted", () =>
|
|
Effect.gen(function*() {
|
|
let signal: AbortSignal
|
|
const fiber = yield* Effect.async<void>((_cb, signal_) => {
|
|
signal = signal_
|
|
}).pipe(Effect.fork)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
assert.strictEqual(signal!.aborted, true)
|
|
}))
|
|
})
|
|
|
|
describe("fork", () => {
|
|
it.effect("is interrupted with parent", () =>
|
|
Effect.gen(function*() {
|
|
let child = false
|
|
let parent = false
|
|
const fiber = yield* Effect.never.pipe(
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
child = true
|
|
})),
|
|
Effect.fork,
|
|
Effect.andThen(Effect.never),
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
parent = true
|
|
})),
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(fiber)
|
|
yield* Effect.yieldNow
|
|
assert.isTrue(child)
|
|
assert.isTrue(parent)
|
|
}))
|
|
})
|
|
|
|
describe("forkDaemon", () => {
|
|
it.effect("is not interrupted with parent", () =>
|
|
Effect.gen(function*() {
|
|
let child = false
|
|
let parent = false
|
|
const handle = yield* Effect.never.pipe(
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
child = true
|
|
})),
|
|
Effect.forkDaemon,
|
|
Effect.andThen(Effect.never),
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
parent = true
|
|
})),
|
|
Effect.fork
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* Fiber.interrupt(handle)
|
|
assert.isFalse(child)
|
|
assert.isTrue(parent)
|
|
}))
|
|
})
|
|
|
|
describe("forkIn", () => {
|
|
it.effect("is interrupted when scope is closed", () =>
|
|
Effect.gen(function*() {
|
|
let interrupted = false
|
|
const scope = yield* Scope.make
|
|
yield* Effect.never.pipe(
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
interrupted = true
|
|
})),
|
|
Effect.forkIn(scope)
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* scope.close(Exit.void)
|
|
assert.isTrue(interrupted)
|
|
}))
|
|
})
|
|
|
|
describe("forkScoped", () => {
|
|
it.effect("is interrupted when scope is closed", () =>
|
|
Effect.gen(function*() {
|
|
let interrupted = false
|
|
const scope = yield* Scope.make
|
|
yield* Effect.never.pipe(
|
|
Effect.onInterrupt(Effect.sync(() => {
|
|
interrupted = true
|
|
})),
|
|
Effect.forkScoped,
|
|
Scope.provide(scope)
|
|
)
|
|
yield* Effect.yieldNow
|
|
yield* scope.close(Exit.void)
|
|
assert.isTrue(interrupted)
|
|
}))
|
|
})
|
|
|
|
// describe("do notation", () => {
|
|
// it.effect("works", () =>
|
|
// Effect.succeed(1).pipe(
|
|
// Effect.bindTo("a"),
|
|
// Effect.let("b", ({ a }) => a + 1),
|
|
// Effect.bind("b", ({ b }) => Effect.succeed(b.toString())),
|
|
// Effect.tap((_) => {
|
|
// assert.deepStrictEqual(_, {
|
|
// a: 1,
|
|
// b: "2"
|
|
// })
|
|
// })
|
|
// ))
|
|
// })
|
|
|
|
describe("stack safety", () => {
|
|
it.live("recursion", () => {
|
|
const loop: Effect.Effect<void> = Effect.void.pipe(
|
|
Effect.flatMap((_) => loop)
|
|
)
|
|
return loop.pipe(
|
|
Effect.timeoutOption(50)
|
|
)
|
|
})
|
|
})
|
|
|
|
describe("finalization", () => {
|
|
const ExampleError = new Error("Oh noes!")
|
|
|
|
it.effect("fail ensuring", () =>
|
|
Effect.gen(function*() {
|
|
let finalized = false
|
|
const result = yield* Effect.fail(ExampleError).pipe(
|
|
Effect.ensuring(Effect.sync(() => {
|
|
finalized = true
|
|
})),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.fail(ExampleError))
|
|
assert.isTrue(finalized)
|
|
}))
|
|
|
|
it.effect("fail on error", () =>
|
|
Effect.gen(function*() {
|
|
let finalized = false
|
|
const result = yield* Effect.fail(ExampleError).pipe(
|
|
Effect.onError(() =>
|
|
Effect.sync(() => {
|
|
finalized = true
|
|
})
|
|
),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.fail(ExampleError))
|
|
assert.isTrue(finalized)
|
|
}))
|
|
|
|
it.effect("finalizer errors not caught", () =>
|
|
Effect.gen(function*() {
|
|
const e2 = new Error("e2")
|
|
const e3 = new Error("e3")
|
|
const result = yield* pipe(
|
|
Effect.fail(ExampleError),
|
|
Effect.ensuring(Effect.die(e2)),
|
|
Effect.ensuring(Effect.die(e3)),
|
|
Effect.sandbox,
|
|
Effect.flip,
|
|
Effect.map((cause) => cause)
|
|
)
|
|
assert.deepStrictEqual(result, Cause.die(e3))
|
|
}))
|
|
|
|
it.effect("finalizer errors reported", () =>
|
|
Effect.gen(function*() {
|
|
let reported: Exit.Exit<number> | undefined
|
|
const result = yield* pipe(
|
|
Effect.succeed(42),
|
|
Effect.ensuring(Effect.die(ExampleError)),
|
|
Effect.fork,
|
|
Effect.flatMap((fiber) =>
|
|
pipe(
|
|
Fiber.await(fiber),
|
|
Effect.flatMap((e) =>
|
|
Effect.sync(() => {
|
|
reported = e
|
|
})
|
|
)
|
|
)
|
|
)
|
|
)
|
|
assert.isUndefined(result)
|
|
assert.isFalse(reported !== undefined && Exit.isSuccess(reported))
|
|
}))
|
|
|
|
it.effect("acquireUseRelease usage result", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.acquireUseRelease(
|
|
Effect.void,
|
|
() => Effect.succeed(42),
|
|
() => Effect.void
|
|
)
|
|
assert.strictEqual(result, 42)
|
|
}))
|
|
|
|
it.effect("error in just acquisition", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.acquireUseRelease(
|
|
Effect.fail(ExampleError),
|
|
() => Effect.void,
|
|
() => Effect.void
|
|
),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.fail(ExampleError))
|
|
}))
|
|
|
|
it.effect("error in just release", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.acquireUseRelease(
|
|
Effect.void,
|
|
() => Effect.void,
|
|
() => Effect.die(ExampleError)
|
|
),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.die(ExampleError))
|
|
}))
|
|
|
|
it.effect("error in just usage", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.acquireUseRelease(
|
|
Effect.void,
|
|
() => Effect.fail(ExampleError),
|
|
() => Effect.void
|
|
),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.fail(ExampleError))
|
|
}))
|
|
|
|
it.effect("rethrown caught error in acquisition", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.acquireUseRelease(
|
|
Effect.fail(ExampleError),
|
|
() => Effect.void,
|
|
() => Effect.void
|
|
).pipe(Effect.flip)
|
|
assert.deepEqual(result, ExampleError)
|
|
}))
|
|
|
|
it.effect("rethrown caught error in release", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* pipe(
|
|
Effect.acquireUseRelease(
|
|
Effect.void,
|
|
() => Effect.void,
|
|
() => Effect.die(ExampleError)
|
|
),
|
|
Effect.exit
|
|
)
|
|
assert.deepStrictEqual(result, Exit.die(ExampleError))
|
|
}))
|
|
|
|
it.effect("rethrown caught error in usage", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.acquireUseRelease(
|
|
Effect.void,
|
|
() => Effect.fail(ExampleError),
|
|
() => Effect.void
|
|
).pipe(Effect.exit)
|
|
assert.deepEqual(result, Exit.fail(ExampleError))
|
|
}))
|
|
|
|
it.effect("onResult - ensures that a cleanup function runs when an effect fails", () =>
|
|
Effect.gen(function*() {
|
|
let ref = false
|
|
yield* Effect.die("boom").pipe(
|
|
Effect.onExit((result) =>
|
|
Exit.hasDie(result) ?
|
|
Effect.sync(() => {
|
|
ref = true
|
|
}) :
|
|
Effect.void
|
|
),
|
|
Effect.sandbox,
|
|
Effect.ignore
|
|
)
|
|
assert.isTrue(ref)
|
|
}))
|
|
})
|
|
|
|
describe("error handling", () => {
|
|
class ErrorA extends Data.TaggedError("A") {}
|
|
class ErrorB extends Data.TaggedError("B") {}
|
|
class ErrorC extends Data.Error {}
|
|
|
|
it.effect("catchTag", () =>
|
|
Effect.gen(function*() {
|
|
let error: ErrorA | ErrorB | ErrorC = new ErrorA()
|
|
const effect = Effect.failSync(() => error).pipe(
|
|
Effect.catchTag("A", (_) => Effect.succeed(1)),
|
|
Effect.catchTag("B", (_) => Effect.succeed(2)),
|
|
Effect.orElseSucceed(() => 3)
|
|
)
|
|
assert.strictEqual(yield* effect, 1)
|
|
error = new ErrorB()
|
|
assert.strictEqual(yield* effect, 2)
|
|
error = new ErrorC()
|
|
assert.strictEqual(yield* effect, 3)
|
|
}))
|
|
})
|
|
|
|
describe("zip", () => {
|
|
it.effect("concurrent: false", () => {
|
|
const executionOrder: Array<string> = []
|
|
const task1 = Effect.succeed("a").pipe(Effect.delay(50), Effect.tap(() => executionOrder.push("task1")))
|
|
const task2 = Effect.succeed(1).pipe(Effect.delay(1), Effect.tap(() => executionOrder.push("task2")))
|
|
return Effect.gen(function*() {
|
|
const fiber = yield* Effect.fork(Effect.zip(task1, task2))
|
|
yield* TestClock.adjust(51)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, ["a", 1])
|
|
assert.deepStrictEqual(executionOrder, ["task1", "task2"])
|
|
})
|
|
})
|
|
it.effect("concurrent: true", () => {
|
|
const executionOrder: Array<string> = []
|
|
const task1 = Effect.succeed("a").pipe(Effect.delay(50), Effect.tap(() => executionOrder.push("task1")))
|
|
const task2 = Effect.succeed(1).pipe(Effect.delay(1), Effect.tap(() => executionOrder.push("task2")))
|
|
return Effect.gen(function*() {
|
|
const fiber = yield* Effect.fork(Effect.zip(task1, task2, { concurrent: true }))
|
|
yield* TestClock.adjust(50)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, ["a", 1])
|
|
assert.deepStrictEqual(executionOrder, ["task2", "task1"])
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("zipWith", () => {
|
|
it.effect("concurrent: false", () => {
|
|
const executionOrder: Array<string> = []
|
|
const task1 = Effect.succeed("a").pipe(Effect.delay(50), Effect.tap(() => executionOrder.push("task1")))
|
|
const task2 = Effect.succeed(1).pipe(Effect.delay(1), Effect.tap(() => executionOrder.push("task2")))
|
|
return Effect.gen(function*() {
|
|
const fiber = yield* Effect.fork(Effect.zipWith(task1, task2, (a, b) => a + b))
|
|
yield* TestClock.adjust(51)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, "a1")
|
|
assert.deepStrictEqual(executionOrder, ["task1", "task2"])
|
|
})
|
|
})
|
|
it.effect("concurrent: true", () => {
|
|
const executionOrder: Array<string> = []
|
|
const task1 = Effect.succeed("a").pipe(Effect.delay(50), Effect.tap(() => executionOrder.push("task1")))
|
|
const task2 = Effect.succeed(1).pipe(Effect.delay(1), Effect.tap(() => executionOrder.push("task2")))
|
|
return Effect.gen(function*() {
|
|
const fiber = yield* Effect.fork(Effect.zipWith(task1, task2, (a, b) => a + b, { concurrent: true }))
|
|
yield* TestClock.adjust(50)
|
|
const result = yield* Fiber.join(fiber)
|
|
assert.deepStrictEqual(result, "a1")
|
|
assert.deepStrictEqual(executionOrder, ["task2", "task1"])
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("catchFailure", () => {
|
|
it.effect("first argument as success", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.catchFailure(Effect.succeed(1), () => false, () => Effect.fail("e2"))
|
|
assert.deepStrictEqual(result, 1)
|
|
}))
|
|
it.effect("first argument as failure and predicate return false", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.flip(
|
|
Effect.catchFailure(Effect.fail("e1" as const), () => false, () => Effect.fail("e2" as const))
|
|
)
|
|
assert.deepStrictEqual(result, "e1")
|
|
}))
|
|
it.effect("first argument as failure and predicate return true", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.flip(
|
|
Effect.catchFailure(Effect.fail("e1" as const), () => true, () => Effect.fail("e2" as const))
|
|
)
|
|
assert.deepStrictEqual(result, "e2")
|
|
}))
|
|
})
|
|
|
|
describe("catch", () => {
|
|
it.effect("first argument as success", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.catch(Effect.succeed(1), () => Effect.fail("e2" as const))
|
|
assert.deepStrictEqual(result, 1)
|
|
}))
|
|
it.effect("first argument as failure", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.flip(Effect.catch(Effect.fail("e1" as const), () => Effect.fail("e2" as const)))
|
|
assert.deepStrictEqual(result, "e2")
|
|
}))
|
|
})
|
|
|
|
describe("catchCause", () => {
|
|
it.effect("first argument as success", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.catchCause(Effect.succeed(1), () => Effect.fail("e2" as const))
|
|
assert.deepStrictEqual(result, 1)
|
|
}))
|
|
it.effect("first argument as failure", () =>
|
|
Effect.gen(function*() {
|
|
const result = yield* Effect.flip(
|
|
Effect.catchCause(Effect.fail("e1" as const), () => Effect.fail("e2" as const))
|
|
)
|
|
assert.deepStrictEqual(result, "e2")
|
|
}))
|
|
})
|
|
})
|