forked from mtusi2026/mtusi2026lab6
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.
Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
(OTEL_*) из skeleton.
92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { ServiceUnavailableException } from '@nestjs/common';
|
|
import { HealthService } from './health.service.js';
|
|
|
|
describe('HealthService', () => {
|
|
it('reports liveness and readiness when dependencies are healthy', async () => {
|
|
const database = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const redis = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const service = new HealthService(
|
|
database as never,
|
|
redis as never,
|
|
{ getOrThrow: () => 50 } as never,
|
|
);
|
|
expect(service.getLiveness()).toEqual({ status: 'ok' });
|
|
await expect(service.getReadiness()).resolves.toEqual({ status: 'ok' });
|
|
});
|
|
|
|
it('rejects readiness when a dependency is unavailable', async () => {
|
|
const database = {
|
|
health: vi.fn().mockRejectedValue(new Error('down')),
|
|
};
|
|
const redis = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const service = new HealthService(
|
|
database as never,
|
|
redis as never,
|
|
{ getOrThrow: () => 50 } as never,
|
|
);
|
|
await expect(service.getReadiness()).rejects.toBeInstanceOf(
|
|
ServiceUnavailableException,
|
|
);
|
|
});
|
|
|
|
it('rejects readiness when Redis alone is unavailable', async () => {
|
|
const database = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const redis = {
|
|
health: vi.fn().mockRejectedValue(new Error('down')),
|
|
};
|
|
const service = new HealthService(
|
|
database as never,
|
|
redis as never,
|
|
{ getOrThrow: () => 50 } as never,
|
|
);
|
|
await expect(service.getReadiness()).rejects.toBeInstanceOf(
|
|
ServiceUnavailableException,
|
|
);
|
|
});
|
|
|
|
it('keeps liveness healthy while draining', async () => {
|
|
const service = new HealthService(
|
|
{ health: vi.fn() } as never,
|
|
{ health: vi.fn() } as never,
|
|
{ getOrThrow: () => 50 } as never,
|
|
);
|
|
service.setDraining();
|
|
expect(service.getLiveness()).toEqual({ status: 'ok' });
|
|
await expect(service.getReadiness()).rejects.toThrow(
|
|
'Service is shutting down',
|
|
);
|
|
});
|
|
|
|
it('answers not-ready during drain without probing dependencies', async () => {
|
|
const database = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const redis = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const service = new HealthService(
|
|
database as never,
|
|
redis as never,
|
|
{ getOrThrow: () => 50 } as never,
|
|
);
|
|
service.setDraining();
|
|
await expect(service.getReadiness()).rejects.toThrow(
|
|
'Service is shutting down',
|
|
);
|
|
// Дрейн обязан отвечать сразу: проба LB не должна ждать зависимости,
|
|
// когда реплика уже выходит из ротации.
|
|
expect(database.health).not.toHaveBeenCalled();
|
|
expect(redis.health).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('fails readiness within the budget when a dependency hangs', async () => {
|
|
const database = { health: vi.fn(() => new Promise<void>(() => {})) };
|
|
const redis = { health: vi.fn().mockResolvedValue(undefined) };
|
|
const service = new HealthService(
|
|
database as never,
|
|
redis as never,
|
|
{ getOrThrow: () => 20 } as never,
|
|
);
|
|
await expect(service.getReadiness()).rejects.toBeInstanceOf(
|
|
ServiceUnavailableException,
|
|
);
|
|
});
|
|
});
|