Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/testing/stub-http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'bun:test'
import { HttpClient, HttpClientRequest } from '@effect/platform'
import { Effect } from 'effect'
import { Duration, Effect, Fiber, Option } from 'effect'
import { makeStubHttpClient } from './stub-http-client.ts'

const REALM = 'https://zulip.example.com/api/v1'
Expand Down Expand Up @@ -202,6 +202,29 @@ test('opens no socket — a request to an unroutable host still resolves', () =>
}),
))

test('a hang response captures the request, then never resolves and stays interruptible', () =>
Effect.runPromise(
Effect.gen(function* () {
const stub = yield* makeStubHttpClient
// The long-poll hold: the stub answers instantly for everything else, so
// a terminal hang is what stops an eager consumer draining the sequence.
yield* stub.respondSequence('GET', '/api/v1/events', [{ hang: true }])
const fiber = yield* Effect.fork(
stub.client.execute(HttpClientRequest.get(`${REALM}/events`)),
)
// Let the forked request issue and park on the hang.
yield* Effect.sleep(Duration.millis(10))
const captured = yield* stub.captured
expect(captured).toHaveLength(1)
expect(captured[0]?.url.pathname).toBe('/api/v1/events')
// Still parked — a hang resolves to no Exit.
expect(Option.isNone(yield* fiber.poll)).toBe(true)
// Interrupting unwinds it cleanly (the scope-close path).
const exit = yield* Fiber.interrupt(fiber)
expect(exit._tag).toBe('Failure')
}),
))

test('is a drop-in for the HttpClient.HttpClient service', () =>
Effect.runPromise(
Effect.gen(function* () {
Expand Down
45 changes: 32 additions & 13 deletions packages/testing/stub-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ import {
type HttpClientRequest,
HttpClientResponse,
} from '@effect/platform'
import { Data, Effect, HashMap, Option, Ref } from 'effect'
import { Data, Effect, HashMap, Option, Predicate, Ref } from 'effect'

export type StubResponse = {
/**
* A response that never resolves — the request is captured, then the effect
* parks on `Effect.never`. Models the real Zulip long-poll *holding* the
* connection open: an in-memory stub answers instantly, so without a hold an
* eager consumer (the event-pump's `Stream.runDrain`) would burn through the
* whole response sequence in a hot loop. A fiber blocked on a hang is
* *interrupted* (not errored) when its scope closes — which is exactly the
* scope-close-interrupt path the event-pump tests exercise.
*/
export type StubHang = {
readonly hang: true
}

export type StubBody = {
/** Object → JSON-encoded; string → verbatim; `Uint8Array` → raw bytes. */
readonly body: unknown
/** HTTP status; defaults to 200. */
Expand All @@ -34,6 +47,8 @@ export type StubResponse = {
readonly headers?: Readonly<Record<string, string>>
}

export type StubResponse = StubBody | StubHang

export type CapturedHttpRequest = {
readonly method: string
readonly url: URL
Expand Down Expand Up @@ -87,19 +102,19 @@ const requestBodyInit = (body: HttpBody.HttpBody): string | Uint8Array | FormDat
}
}

const responseBodyInit = (response: StubResponse): string | Uint8Array => {
const responseBodyInit = (response: StubBody): string | Uint8Array => {
if (response.body instanceof Uint8Array) return response.body
if (typeof response.body === 'string') return response.body
return JSON.stringify(response.body)
}

const responseHeaders = (response: StubResponse): Record<string, string> => {
const responseHeaders = (response: StubBody): Record<string, string> => {
const base: Record<string, string> =
response.body instanceof Uint8Array ? {} : { 'content-type': 'application/json' }
return { ...base, ...response.headers }
}

const notFound = (method: string, path: string): StubResponse => ({
const notFound = (method: string, path: string): StubBody => ({
body: { result: 'error', code: 'NO_STUB_HANDLER', msg: `no stub handler for ${method} ${path}` },
status: 404,
})
Expand Down Expand Up @@ -155,14 +170,18 @@ export const makeStubHttpClient: Effect.Effect<StubHttpClient> = Effect.gen(func
const client = HttpClient.make((request, url) =>
capture(request, url).pipe(
Effect.zipRight(nextResponse(request.method, url.pathname)),
Effect.map((response) =>
HttpClientResponse.fromWeb(
request,
new Response(responseBodyInit(response), {
status: response.status ?? 200,
headers: responseHeaders(response),
}),
),
Effect.flatMap((response) =>
Predicate.hasProperty(response, 'hang')
? Effect.never
: Effect.succeed(
HttpClientResponse.fromWeb(
request,
new Response(responseBodyInit(response), {
status: response.status ?? 200,
headers: responseHeaders(response),
}),
),
),
),
),
)
Expand Down
Loading