Files
iptvnator/apps/xtream-mock-server/src/app/server-lifecycle.ts
T
4gray a2fafcfc08 test(performance): add end-to-end Xtream benchmark harness (#1300)
* docs(performance): plan Xtream benchmark

* feat(xtream-mock-server): add deterministic 100k fixture

* style(xtream-mock-server): apply repository formatting

* fix(xtream-mock-server): harden performance fixture data

* feat(xtream-mock-server): add performance control plane

* docs(performance): correct Xtream capture plan

* fix(xtream-mock-server): harden performance controls

* fix(xtream-mock-server): harden control lifecycle

* feat(performance): add Xtream preload markers

* feat(performance): trace Xtream main phases

* feat(performance): mark Xtream store publications

* feat(performance): trace Xtream database phases

* feat(performance): trace Xtream delete cancellation

* feat(performance): capture Xtream phase attribution

* feat(performance): mark Sources Xtream refresh

* test(performance): define Xtream benchmark evidence contracts

* test(performance): add Xtream benchmark runner

* test(performance): surface failure evidence writes

* test(performance): align database read clock

* test(performance): preserve capture failure contracts
2026-07-28 08:08:07 +02:00

56 lines
1.4 KiB
TypeScript

import type { Server } from 'node:http';
const SHUTDOWN_DEADLINE_MS = 1_000;
export interface XtreamMockShutdownApplication {
shutdown(): unknown;
}
export function createXtreamMockServerShutdown(
server: Server,
application: XtreamMockShutdownApplication
): () => Promise<void> {
let shutdownPromise: Promise<void> | undefined;
return () => {
shutdownPromise ??= beginShutdown(server, application);
return shutdownPromise;
};
}
function beginShutdown(
server: Server,
application: XtreamMockShutdownApplication
): Promise<void> {
try {
application.shutdown();
} catch {
// HTTP shutdown must remain bounded even if an application hook fails.
}
return closeHttpServer(server);
}
function closeHttpServer(server: Server): Promise<void> {
return new Promise((resolve) => {
let settled = false;
const finish = () => {
if (settled) return;
settled = true;
clearTimeout(deadline);
resolve();
};
const deadline = setTimeout(() => {
server.closeAllConnections();
finish();
}, SHUTDOWN_DEADLINE_MS);
deadline.unref();
try {
server.close(() => finish());
server.closeIdleConnections();
server.closeAllConnections();
} catch {
finish();
}
});
}