Both were synchronous and left the boot to the first request, with the fix documented as something the caller should remember: `await app.boot()` before `Deno.serve` / `Bun.serve`. That made the correct use of a core helper depend on reading a README, in a framework whose whole argument is that order should not be something you have to get right. The failure it leaves is the bad kind. `app.fetch` boots lazily and the boot memo keeps the *rejection*, so a missing signing key does not fail the deploy: the port binds, every request is answered 500 by the runtime, and none of them reach `onError`. Anything that health-checks by asking whether the process is listening sees a healthy server. `listen()` has always awaited `bootApp()` before binding its socket, with a comment saying it exists so Node and Deno/Bun serve an identical graph. They did not. Now the three helpers agree, and the two adapters return promises. `edgeHandler` is deliberately unchanged: on workerd there is no startup outside a request, so there is no earlier moment to move the failure to. Both new tests were confirmed to fail with the gate commented out — without it `serveBun` resolves instead of rejecting, and serveDeno binds a port that 500s. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
100 lines
3.5 KiB
TypeScript
100 lines
3.5 KiB
TypeScript
// Real Bun integration test for the bounded close() serveBun returns. app.close() cannot do this
|
|
// job on Bun — it returns at its `if (!server)` guard, because a Bun app is served through
|
|
// app.fetch and never through listen() — so the deadline lives on the transport that owns the
|
|
// handle. See issue #15.
|
|
//
|
|
// Run with: npm run test:bun (or: bun test test/bun/)
|
|
import 'reflect-metadata';
|
|
import { test, expect } from 'bun:test';
|
|
import { createApp, Route, Get, Module, Provider, needs } from '../../src/index.ts';
|
|
import { serveBun } from '../../src/bun.ts';
|
|
|
|
@Route('/')
|
|
class Ctl {
|
|
@Get('/hang')
|
|
hang() {
|
|
return new Promise(() => {}); // never resolves — a stuck handler
|
|
}
|
|
@Get('/slow')
|
|
async slow() {
|
|
await Bun.sleep(200); // finite, so draining has something real to wait out
|
|
return { ok: true };
|
|
}
|
|
}
|
|
@Module({ mountpoint: '/', controllers: [Ctl] })
|
|
class M {}
|
|
|
|
test('serveBun close({ timeoutMs }) gives up on a stuck handler and resolves', async () => {
|
|
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
|
|
|
|
fetch(`http://127.0.0.1:${server.port}/hang`).catch(() => {});
|
|
await Bun.sleep(50); // let the request actually reach the handler
|
|
|
|
const start = Date.now();
|
|
await server.close({ timeoutMs: 300 });
|
|
const elapsed = Date.now() - start;
|
|
|
|
// The assertion that matters is the upper bound: without the deadline this never returns.
|
|
expect(elapsed).toBeLessThan(2000);
|
|
expect(elapsed).toBeGreaterThanOrEqual(250);
|
|
});
|
|
|
|
test('serveBun close() drains an in-flight request rather than cutting it', async () => {
|
|
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
|
|
|
|
const inFlight = fetch(`http://127.0.0.1:${server.port}/slow`).then((r) => r.json());
|
|
await Bun.sleep(50); // the request must be *in* the handler before we close, or it never got in
|
|
|
|
await server.close({ timeoutMs: 5000 });
|
|
|
|
expect(await inFlight).toEqual({ ok: true });
|
|
});
|
|
|
|
// D7 of the teardown design: proving the Node path works proves nothing here, because app.close()
|
|
// returns at its no-server guard on Bun. The teardown has to run from the close() Bun actually uses.
|
|
test('serveBun close() runs registered teardown', async () => {
|
|
const closed: string[] = [];
|
|
const app = createApp({
|
|
modules: [M],
|
|
hooks: [{ onShutdown: () => void closed.push('hook') }],
|
|
plugins: [{
|
|
name: 'teardown-probe',
|
|
mount: (api) => api.onShutdown(async () => {
|
|
await Bun.sleep(10);
|
|
closed.push('plugin');
|
|
}),
|
|
}],
|
|
});
|
|
const server = await serveBun(app, { port: 0 });
|
|
|
|
await server.close({ timeoutMs: 2000 });
|
|
|
|
// Reverse registration order: the plugin registered after the hook, so it tears down first.
|
|
expect(closed).toEqual(['plugin', 'hook']);
|
|
});
|
|
|
|
// The boot gate, which is why serveBun is async. Before it, a provider that threw was memoized as a
|
|
// rejection and answered 500 to every request from the runtime, never reaching onError — so the
|
|
// failure looked like a runtime fault at 3am instead of a deploy that refused to start.
|
|
@Provider({ provides: 'key' })
|
|
class BadKey {
|
|
provide(): never {
|
|
throw new Error('no such key file');
|
|
}
|
|
}
|
|
@Route('/')
|
|
class NeedsKey {
|
|
@Get('/signed')
|
|
signed(@needs('key') key: string) {
|
|
return { key };
|
|
}
|
|
}
|
|
@Module({ mountpoint: '/', providers: [BadKey], controllers: [NeedsKey] })
|
|
class KeyModule {}
|
|
|
|
test('serveBun rejects when a provider fails, instead of binding a port that 500s', async () => {
|
|
const app = createApp({ modules: [KeyModule] });
|
|
|
|
await expect(serveBun(app, { port: 0 })).rejects.toThrow(/no such key file/);
|
|
});
|