test(bus): migrate bus tests to Effect runner (#27131)
This commit is contained in:
parent
3e2ec192cf
commit
fec78154b5
1 changed files with 185 additions and 165 deletions
|
|
@ -1,220 +1,240 @@
|
||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { afterEach, describe, expect } from "bun:test"
|
||||||
import { Schema } from "effect"
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||||
|
import { Deferred, Effect, Layer, Schema } from "effect"
|
||||||
import { Bus } from "../../src/bus"
|
import { Bus } from "../../src/bus"
|
||||||
import { BusEvent } from "../../src/bus/bus-event"
|
import { BusEvent } from "../../src/bus/bus-event"
|
||||||
import { Instance } from "../../src/project/instance"
|
import { disposeAllInstances, provideInstance, tmpdirScoped } from "../fixture/fixture"
|
||||||
import { WithInstance } from "../../src/project/with-instance"
|
import { testEffect } from "../lib/effect"
|
||||||
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
|
||||||
|
|
||||||
const TestEvent = {
|
const TestEvent = {
|
||||||
Ping: BusEvent.define("test.ping", Schema.Struct({ value: Schema.Number })),
|
Ping: BusEvent.define("test.ping", Schema.Struct({ value: Schema.Number })),
|
||||||
Pong: BusEvent.define("test.pong", Schema.Struct({ message: Schema.String })),
|
Pong: BusEvent.define("test.pong", Schema.Struct({ message: Schema.String })),
|
||||||
}
|
}
|
||||||
|
|
||||||
function withInstance(directory: string, fn: () => Promise<void>) {
|
const it = testEffect(Layer.mergeAll(Bus.layer, CrossSpawnSpawner.defaultLayer))
|
||||||
return WithInstance.provide({ directory, fn })
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("Bus", () => {
|
describe("Bus", () => {
|
||||||
afterEach(() => disposeAllInstances())
|
afterEach(() => disposeAllInstances())
|
||||||
|
|
||||||
describe("publish + subscribe", () => {
|
describe("publish + subscribe", () => {
|
||||||
test("subscriber is live immediately after subscribe returns", async () => {
|
it.instance("subscriber is live immediately after subscribe returns", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: number[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const received: number[] = []
|
||||||
|
const done = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
|
||||||
received.push(evt.properties.value)
|
received.push(evt.properties.value)
|
||||||
|
Deferred.doneUnsafe(done, Effect.void)
|
||||||
})
|
})
|
||||||
await Bus.publish(TestEvent.Ping, { value: 42 })
|
yield* bus.publish(TestEvent.Ping, { value: 42 })
|
||||||
await Bun.sleep(10)
|
yield* Deferred.await(done).pipe(Effect.timeout("2 seconds"))
|
||||||
})
|
|
||||||
|
|
||||||
expect(received).toEqual([42])
|
expect(received).toEqual([42])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("subscriber receives matching events", async () => {
|
it.instance("subscriber receives matching events", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: number[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const received: number[] = []
|
||||||
|
const done = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
|
||||||
received.push(evt.properties.value)
|
received.push(evt.properties.value)
|
||||||
|
if (received.length === 2) Deferred.doneUnsafe(done, Effect.void)
|
||||||
})
|
})
|
||||||
// Give the subscriber fiber time to start consuming
|
yield* bus.publish(TestEvent.Ping, { value: 42 })
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 99 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 42 })
|
yield* Deferred.await(done).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bus.publish(TestEvent.Ping, { value: 99 })
|
|
||||||
// Give subscriber time to process
|
|
||||||
await Bun.sleep(10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(received).toEqual([42, 99])
|
expect(received).toEqual([42, 99])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("subscriber does not receive events of other types", async () => {
|
it.instance("subscriber does not receive events of other types", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const pings: number[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const pings: number[] = []
|
||||||
|
const done = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
|
||||||
pings.push(evt.properties.value)
|
pings.push(evt.properties.value)
|
||||||
|
Deferred.doneUnsafe(done, Effect.void)
|
||||||
})
|
})
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Pong, { message: "hello" })
|
||||||
await Bus.publish(TestEvent.Pong, { message: "hello" })
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
yield* Deferred.await(done).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bun.sleep(10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(pings).toEqual([1])
|
expect(pings).toEqual([1])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("publish with no subscribers does not throw", async () => {
|
it.instance("publish with no subscribers does not throw", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
|
const bus = yield* Bus.Service
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
}),
|
||||||
})
|
)
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("unsubscribe", () => {
|
describe("unsubscribe", () => {
|
||||||
test("unsubscribe stops delivery", async () => {
|
it.instance("unsubscribe stops delivery", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: number[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const received: number[] = []
|
||||||
|
const first = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
const unsub = yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
const unsub = Bus.subscribe(TestEvent.Ping, (evt) => {
|
|
||||||
received.push(evt.properties.value)
|
received.push(evt.properties.value)
|
||||||
|
if (evt.properties.value === 1) Deferred.doneUnsafe(first, Effect.void)
|
||||||
})
|
})
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
yield* Deferred.await(first).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bun.sleep(10)
|
yield* Effect.sync(unsub)
|
||||||
unsub()
|
yield* bus.publish(TestEvent.Ping, { value: 2 })
|
||||||
await Bun.sleep(10)
|
yield* Effect.sleep("10 millis")
|
||||||
await Bus.publish(TestEvent.Ping, { value: 2 })
|
|
||||||
await Bun.sleep(10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(received).toEqual([1])
|
expect(received).toEqual([1])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("subscribeAll", () => {
|
describe("subscribeAll", () => {
|
||||||
test("subscribeAll is live immediately after subscribe returns", async () => {
|
it.instance("subscribeAll is live immediately after subscribe returns", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: string[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const received: string[] = []
|
||||||
|
const done = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeAllCallback((evt) => {
|
||||||
Bus.subscribeAll((evt) => {
|
|
||||||
received.push(evt.type)
|
received.push(evt.type)
|
||||||
|
Deferred.doneUnsafe(done, Effect.void)
|
||||||
})
|
})
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
await Bun.sleep(10)
|
yield* Deferred.await(done).pipe(Effect.timeout("2 seconds"))
|
||||||
})
|
|
||||||
|
|
||||||
expect(received).toEqual(["test.ping"])
|
expect(received).toEqual(["test.ping"])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
test("receives all event types", async () => {
|
it.instance("receives all event types", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: string[] = []
|
const bus = yield* Bus.Service
|
||||||
|
const received: string[] = []
|
||||||
|
const done = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeAllCallback((evt) => {
|
||||||
Bus.subscribeAll((evt) => {
|
|
||||||
received.push(evt.type)
|
received.push(evt.type)
|
||||||
|
if (received.length === 2) Deferred.doneUnsafe(done, Effect.void)
|
||||||
})
|
})
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
yield* bus.publish(TestEvent.Pong, { message: "hi" })
|
||||||
await Bus.publish(TestEvent.Pong, { message: "hi" })
|
yield* Deferred.await(done).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bun.sleep(10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(received).toContain("test.ping")
|
expect(received).toContain("test.ping")
|
||||||
expect(received).toContain("test.pong")
|
expect(received).toContain("test.pong")
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("multiple subscribers", () => {
|
describe("multiple subscribers", () => {
|
||||||
test("all subscribers for same event type are called", async () => {
|
it.instance("all subscribers for same event type are called", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const a: number[] = []
|
const bus = yield* Bus.Service
|
||||||
const b: number[] = []
|
const a: number[] = []
|
||||||
|
const b: number[] = []
|
||||||
|
const doneA = yield* Deferred.make<void>()
|
||||||
|
const doneB = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
|
||||||
a.push(evt.properties.value)
|
a.push(evt.properties.value)
|
||||||
|
Deferred.doneUnsafe(doneA, Effect.void)
|
||||||
})
|
})
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
b.push(evt.properties.value)
|
b.push(evt.properties.value)
|
||||||
|
Deferred.doneUnsafe(doneB, Effect.void)
|
||||||
})
|
})
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 7 })
|
||||||
await Bus.publish(TestEvent.Ping, { value: 7 })
|
yield* Deferred.await(doneA).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bun.sleep(10)
|
yield* Deferred.await(doneB).pipe(Effect.timeout("2 seconds"))
|
||||||
})
|
|
||||||
|
|
||||||
expect(a).toEqual([7])
|
expect(a).toEqual([7])
|
||||||
expect(b).toEqual([7])
|
expect(b).toEqual([7])
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("instance isolation", () => {
|
describe("instance isolation", () => {
|
||||||
test("events in one directory do not reach subscribers in another", async () => {
|
it.live("events in one directory do not reach subscribers in another", () =>
|
||||||
await using tmpA = await tmpdir()
|
Effect.gen(function* () {
|
||||||
await using tmpB = await tmpdir()
|
const tmpA = yield* tmpdirScoped()
|
||||||
const receivedA: number[] = []
|
const tmpB = yield* tmpdirScoped()
|
||||||
const receivedB: number[] = []
|
const receivedA: number[] = []
|
||||||
|
const receivedB: number[] = []
|
||||||
|
const doneA = yield* Deferred.make<void>()
|
||||||
|
const doneB = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmpA.path, async () => {
|
yield* Effect.gen(function* () {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
const bus = yield* Bus.Service
|
||||||
receivedA.push(evt.properties.value)
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
})
|
receivedA.push(evt.properties.value)
|
||||||
await Bun.sleep(10)
|
Deferred.doneUnsafe(doneA, Effect.void)
|
||||||
})
|
})
|
||||||
|
}).pipe(provideInstance(tmpA))
|
||||||
|
|
||||||
await withInstance(tmpB.path, async () => {
|
yield* Effect.gen(function* () {
|
||||||
Bus.subscribe(TestEvent.Ping, (evt) => {
|
const bus = yield* Bus.Service
|
||||||
receivedB.push(evt.properties.value)
|
yield* bus.subscribeCallback(TestEvent.Ping, (evt) => {
|
||||||
})
|
receivedB.push(evt.properties.value)
|
||||||
await Bun.sleep(10)
|
Deferred.doneUnsafe(doneB, Effect.void)
|
||||||
})
|
})
|
||||||
|
}).pipe(provideInstance(tmpB))
|
||||||
|
|
||||||
await withInstance(tmpA.path, async () => {
|
yield* Effect.gen(function* () {
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
const bus = yield* Bus.Service
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
})
|
}).pipe(provideInstance(tmpA))
|
||||||
|
|
||||||
await withInstance(tmpB.path, async () => {
|
yield* Effect.gen(function* () {
|
||||||
await Bus.publish(TestEvent.Ping, { value: 2 })
|
const bus = yield* Bus.Service
|
||||||
await Bun.sleep(10)
|
yield* bus.publish(TestEvent.Ping, { value: 2 })
|
||||||
})
|
}).pipe(provideInstance(tmpB))
|
||||||
|
|
||||||
expect(receivedA).toEqual([1])
|
yield* Deferred.await(doneA).pipe(Effect.timeout("2 seconds"))
|
||||||
expect(receivedB).toEqual([2])
|
yield* Deferred.await(doneB).pipe(Effect.timeout("2 seconds"))
|
||||||
})
|
|
||||||
|
expect(receivedA).toEqual([1])
|
||||||
|
expect(receivedB).toEqual([2])
|
||||||
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("instance disposal", () => {
|
describe("instance disposal", () => {
|
||||||
test("InstanceDisposed is delivered to wildcard subscribers before stream ends", async () => {
|
it.live("InstanceDisposed is delivered to wildcard subscribers before stream ends", () =>
|
||||||
await using tmp = await tmpdir()
|
Effect.gen(function* () {
|
||||||
const received: string[] = []
|
const tmp = yield* tmpdirScoped()
|
||||||
|
const received: string[] = []
|
||||||
|
const seen = yield* Deferred.make<void>()
|
||||||
|
const disposed = yield* Deferred.make<void>()
|
||||||
|
|
||||||
await withInstance(tmp.path, async () => {
|
yield* Effect.gen(function* () {
|
||||||
Bus.subscribeAll((evt) => {
|
const bus = yield* Bus.Service
|
||||||
received.push(evt.type)
|
yield* bus.subscribeAllCallback((evt) => {
|
||||||
})
|
received.push(evt.type)
|
||||||
await Bun.sleep(10)
|
if (evt.type === TestEvent.Ping.type) Deferred.doneUnsafe(seen, Effect.void)
|
||||||
await Bus.publish(TestEvent.Ping, { value: 1 })
|
if (evt.type === Bus.InstanceDisposed.type) Deferred.doneUnsafe(disposed, Effect.void)
|
||||||
await Bun.sleep(10)
|
})
|
||||||
})
|
yield* bus.publish(TestEvent.Ping, { value: 1 })
|
||||||
|
yield* Deferred.await(seen).pipe(Effect.timeout("2 seconds"))
|
||||||
|
}).pipe(provideInstance(tmp))
|
||||||
|
|
||||||
// disposeAllInstances triggers the finalizer which publishes InstanceDisposed
|
yield* Effect.promise(disposeAllInstances)
|
||||||
await disposeAllInstances()
|
yield* Deferred.await(disposed).pipe(Effect.timeout("2 seconds"))
|
||||||
await Bun.sleep(50)
|
|
||||||
|
|
||||||
expect(received).toContain("test.ping")
|
expect(received).toContain("test.ping")
|
||||||
expect(received).toContain(Bus.InstanceDisposed.type)
|
expect(received).toContain(Bus.InstanceDisposed.type)
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue