1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-24 20:06:55 +01:00
unleash.unleash/src/test/e2e/ready.e2e.test.ts
Gastón Fournier bcfad0a250
feat: include readiness check option (#10850)
## About the changes
This introduces a new endpoint to allow users to check for readiness of
Unleash to serve traffic, in particular validating that the DB is up.
It's an opt-in feature that has to be enabled with the environment
variable `CHECK_DB_ON_READY=true` or via the configuration option
`checkDbOnReady`.

Closes #10742
2025-10-29 13:11:23 +01:00

103 lines
3.0 KiB
TypeScript

import { setupAppWithCustomConfig } from './helpers/test-helper.js';
import dbInit, { type ITestDb } from './helpers/database-init.js';
let db: ITestDb;
describe('DB is up', () => {
beforeAll(async () => {
db = await dbInit();
});
test('when checkDb is disabled, returns ready', async () => {
const { request } = await setupAppWithCustomConfig(
db.stores,
undefined,
db.rawDatabase,
);
await request
.get('/ready')
.expect('Content-Type', /json/)
.expect(200)
.expect('{"health":"GOOD"}');
});
test('when checkDb is enabled, returns ready', async () => {
const { request } = await setupAppWithCustomConfig(
db.stores,
{ checkDbOnReady: true },
db.rawDatabase,
);
await request.get('/ready').expect(200).expect('{"health":"GOOD"}');
});
test('fails fast when readiness query hangs', async () => {
const { request } = await setupAppWithCustomConfig(
db.stores,
{ checkDbOnReady: true },
db.rawDatabase,
);
const pool = db.rawDatabase.client.pool;
const originalAcquire = pool.acquire.bind(pool);
pool.acquire = () => {
const pending = originalAcquire();
pending.promise = pending.promise.then((conn: any) => {
const originalQuery = conn.query;
conn.query = (queryConfig: any, ...args: any[]) => {
const isSelectOne =
queryConfig?.toUpperCase() === 'SELECT 1';
if (isSelectOne) {
return originalQuery.call(
conn,
'SELECT pg_sleep(1)',
...args,
);
}
return originalQuery.call(conn, queryConfig, ...args);
};
return conn;
});
return pending;
};
try {
await request.get('/ready').expect(503);
} finally {
pool.acquire = originalAcquire;
}
});
});
describe('DB is down', () => {
beforeAll(async () => {
db = await dbInit();
});
test('when checkDb is disabled, returns readiness good', async () => {
const { request } = await setupAppWithCustomConfig(
db.stores,
undefined,
db.rawDatabase,
);
await db.destroy();
await request
.get('/ready')
.expect('Content-Type', /json/)
.expect(200)
.expect('{"health":"GOOD"}');
});
test('when checkDb is enabled, fails readiness check', async () => {
const { request } = await setupAppWithCustomConfig(
db.stores,
{ checkDbOnReady: true },
db.rawDatabase,
);
await db.destroy();
await request.get('/ready').expect(503);
});
});