History

Probe test http servers for readiness before use

Rhys Sullivan committed 4 months ago

Commit e8df02c1 parent 78d99181 file · +12 −1Browse files

Unified · Split

packages/core/sdk/src/testing.ts+12−1
@@ -1,5 +1,6 @@
1+import { createConnection } from "node:net";
12 import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2−import { Context, Data, Effect, Layer, Predicate, Scope as EffectScope } from "effect";
3+import { Context, Data, Effect, Layer, Predicate, Schedule, Scope as EffectScope } from "effect";
34 import {
45 HttpClient,
56 HttpRouter,
@@ -109,6 +110,16 @@ const makeTestHttpServer = ( · 103 unmodified lines
109110 return yield* new TestHttpServerAddressError({ address });
110111 }
111112 const client = Context.get(context, HttpClient.HttpClient);
113+ // Under a loaded machine the listen callback can fire before the socket
114+ // reliably accepts; probe at the TCP level (no HTTP request, so handlers
115+ // that record requests never see it) until a connect succeeds.
116+ yield* Effect.callback<void, TestHttpServerServeError>((resume) => {
117+ const socket = createConnection({ host: "127.0.0.1", port: address.port }, () => {
118+ socket.end();
119+ resume(Effect.void);
120+ });
121+ socket.on("error", (cause) => resume(Effect.fail(new TestHttpServerServeError({ cause }))));
122+ }).pipe(Effect.retry(Schedule.both(Schedule.spaced("10 millis"), Schedule.recurs(100))));
112123 const baseUrl = `http://127.0.0.1:${address.port}`;
113124 return {
114125 baseUrl,