lab3: проба готовности на отдельном соединении, серия роста на блоке why#

This commit is contained in:
2026-10-16 22:09:31 +03:00
parent e42f02c016
commit 46d84ab0aa
107 changed files with 1999 additions and 1880 deletions
+44 -5
View File
@@ -9,6 +9,7 @@ describe('HealthService', () => {
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' });
@@ -22,6 +23,22 @@ describe('HealthService', () => {
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,
@@ -32,6 +49,7 @@ describe('HealthService', () => {
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' });
@@ -40,13 +58,34 @@ describe('HealthService', () => {
);
});
it('keeps the load-balancer check positive while a dependency is down', async () => {
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(
{ health: vi.fn().mockRejectedValue(new Error('down')) } as never,
{ health: vi.fn().mockRejectedValue(new Error('down')) } as never,
database as never,
redis as never,
{ getOrThrow: () => 50 } as never,
);
expect(service.getLbCheck()).toEqual({ status: 'ok' });
service.setDraining();
expect(() => service.getLbCheck()).toThrow('Draining');
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,
);
});
});