test(lib): promote pollWithTimeout/awaitWithTimeout helpers (#27626)
This commit is contained in:
@@ -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)),
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user