|
1 | 1 | import { expect, test } from "bun:test" |
2 | | -import { Effect } from "effect" |
| 2 | +import { Deferred, Effect, Fiber } from "effect" |
3 | 3 | import { BackgroundServiceState } from "./background-service-state" |
4 | 4 |
|
5 | 5 | test("new consumers receive the latest reconnected service", async () => { |
6 | 6 | const initial = { url: "http://127.0.0.1:4100", password: "first" } |
7 | 7 | const replacement = { url: "http://127.0.0.1:4200", password: "second" } |
8 | 8 | const service = await Effect.runPromise( |
9 | | - BackgroundServiceState.make({ initial: Effect.succeed(initial), reconnect: Effect.succeed(replacement) }), |
| 9 | + Effect.scoped( |
| 10 | + Effect.gen(function* () { |
| 11 | + const state = yield* BackgroundServiceState.make({ |
| 12 | + initial: Effect.succeed(initial), |
| 13 | + reconnect: Effect.succeed(replacement), |
| 14 | + }) |
| 15 | + expect(yield* state.connection).toEqual(initial) |
| 16 | + expect(yield* state.reconnect).toEqual(replacement) |
| 17 | + return yield* state.connection |
| 18 | + }), |
| 19 | + ), |
10 | 20 | ) |
| 21 | + expect(service).toEqual(replacement) |
| 22 | +}) |
| 23 | + |
| 24 | +test("the state is ready before the initial connect finishes and consumers wait for it", async () => { |
| 25 | + const initial = { url: "http://127.0.0.1:4100", password: "first" } |
| 26 | + await Effect.runPromise( |
| 27 | + Effect.scoped( |
| 28 | + Effect.gen(function* () { |
| 29 | + const gate = yield* Deferred.make<void>() |
| 30 | + const state = yield* BackgroundServiceState.make({ |
| 31 | + initial: Deferred.await(gate).pipe(Effect.as(initial)), |
| 32 | + reconnect: Effect.succeed(initial), |
| 33 | + }) |
| 34 | + const consumer = yield* Effect.forkChild(state.connection) |
| 35 | + yield* Effect.sleep("10 millis") |
| 36 | + expect(consumer.pollUnsafe()).toBeUndefined() |
| 37 | + yield* Deferred.succeed(gate, undefined) |
| 38 | + expect(yield* Fiber.join(consumer)).toEqual(initial) |
| 39 | + }), |
| 40 | + ), |
| 41 | + ) |
| 42 | +}) |
11 | 43 |
|
12 | | - expect(await Effect.runPromise(service.connection)).toEqual(initial) |
13 | | - expect(await Effect.runPromise(service.reconnect)).toEqual(replacement) |
14 | | - expect(await Effect.runPromise(service.connection)).toEqual(replacement) |
| 44 | +test("a failed initial connect fails consumers instead of hanging them", async () => { |
| 45 | + const result = await Effect.runPromise( |
| 46 | + Effect.scoped( |
| 47 | + Effect.gen(function* () { |
| 48 | + const state = yield* BackgroundServiceState.make({ |
| 49 | + initial: Effect.fail(new Error("service unavailable")), |
| 50 | + reconnect: Effect.succeed({ url: "http://127.0.0.1:4100", password: "later" }), |
| 51 | + }) |
| 52 | + return yield* state.connection.pipe(Effect.exit) |
| 53 | + }), |
| 54 | + ), |
| 55 | + ) |
| 56 | + expect(result._tag).toBe("Failure") |
15 | 57 | }) |
0 commit comments