forked from mtusi2026/mtusi2026lab3
test: cover persistence failover and graceful draining
Add API integration checks, migration compatibility, rolling restart scenarios and a report verification collector.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { createRequire } from 'node:module';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
const require = createRequire(`${process.cwd()}/package.json`);
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require(`${process.cwd()}/dist/app.module`);
|
||||
const { PrismaService } = require(`${process.cwd()}/dist/prisma/prisma.service`);
|
||||
const { UserService } = require(`${process.cwd()}/dist/User/user.service`);
|
||||
const { PasswordService } = require(`${process.cwd()}/dist/Auth/password.service`);
|
||||
const { createFastifyAdapter } = require(`${process.cwd()}/dist/common/http/fastify-adapter`);
|
||||
process.env.CACHE_URL = '';
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), { logger: false, abortOnError: false });
|
||||
await app.init();
|
||||
require('nestjs-pino').PinoLogger.root.level = 'silent';
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
await server.ready();
|
||||
const prisma = app.get(PrismaService);
|
||||
const passwords = app.get(PasswordService);
|
||||
const originalHash = passwords.hash.bind(passwords);
|
||||
const spans = [];
|
||||
passwords.hash = async (password) => {
|
||||
const start = performance.now();
|
||||
const value = await originalHash(password);
|
||||
spans.push({ phase: 'kdf', ms: Math.round(performance.now() - start) });
|
||||
return value;
|
||||
};
|
||||
app.get(UserService).prisma = prisma.$extends({ query: { user: { async create({ args, query }) {
|
||||
const start = performance.now();
|
||||
const value = await query(args);
|
||||
spans.push({ phase: 'sql_user_create', ms: Math.round(performance.now() - start) });
|
||||
return value;
|
||||
} } } });
|
||||
const ids = [];
|
||||
try {
|
||||
const health = await server.inject({ method: 'GET', url: '/health/ready' });
|
||||
console.log(JSON.stringify({ phase: 'health', status: health.statusCode }));
|
||||
for (let sample = 1; sample <= 3; sample++) {
|
||||
spans.length = 0;
|
||||
const start = performance.now();
|
||||
const response = await server.inject({ method: 'POST', url: '/api/v1/users', payload: {
|
||||
username: `warm_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
display_name: 'Synthetic startup diagnostic', password: 'Synthetic-diagnostic-password-123',
|
||||
} });
|
||||
if (response.statusCode === 201) ids.push(response.json().id);
|
||||
console.log(JSON.stringify({ sample, status: response.statusCode, total_ms: Math.round(performance.now() - start), spans }));
|
||||
}
|
||||
} finally {
|
||||
await prisma.user.deleteMany({ where: { id: { in: ids } } });
|
||||
await app.close();
|
||||
}
|
||||
Reference in New Issue
Block a user