test(lib): promote pollWithTimeout/awaitWithTimeout helpers (#27626)

This commit is contained in:
Kit Langton
2026-05-14 19:45:32 -04:00
committed by GitHub
parent dab567aa2d
commit 4e143e3a3e
4 changed files with 84 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
import { test, type TestOptions } from "bun:test"
import { Cause, Effect, Exit, Layer } from "effect"
import { Cause, Duration, Effect, Exit, Layer } from "effect"
import type * as Scope from "effect/Scope"
import * as TestClock from "effect/testing/TestClock"
import * as TestConsole from "effect/testing/TestConsole"
@@ -109,3 +109,33 @@ export const it = make(testEnv, liveEnv)
export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))
export const awaitWithTimeout = <A, E, R>(
self: Effect.Effect<A, E, R>,
message: string,
duration: Duration.Input = "2 seconds",
) =>
self.pipe(
Effect.timeoutOrElse({
duration,
orElse: () => Effect.fail(new Error(message)),
}),
)
export const pollWithTimeout = <A, E, R>(
self: Effect.Effect<A | undefined, E, R>,
message: string,
duration: Duration.Input = "5 seconds",
) =>
Effect.gen(function* () {
while (true) {
const result = yield* self
if (result !== undefined) return result
yield* Effect.sleep("20 millis")
}
}).pipe(
Effect.timeoutOrElse({
duration,
orElse: () => Effect.fail(new Error(message)),
}),
)