-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathgraceful-shutdown.e2e-spec.ts
66 lines (53 loc) · 1.87 KB
/
graceful-shutdown.e2e-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { ShutdownSignal } from '@nestjs/common';
import { type NestApplicationContext } from '@nestjs/core';
import * as request from 'supertest';
import { bootstrapTestingModule } from './helper';
import { sleep } from '../lib/utils';
describe('Graceful shutdown', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should gracefully shutdown the application', async () => {
jest.spyOn(global, 'setTimeout');
const setHealthEndpoint = bootstrapTestingModule({
gracefulShutdownTimeoutMs: 64,
}).setHealthEndpoint;
const app = await setHealthEndpoint(({ healthCheck }) =>
healthCheck.check([]),
).start();
const { status } = await request(app.getHttpServer()).get('/health');
expect(status).toBe(200);
let isClosed = false;
(app.close as NestApplicationContext['close'])(ShutdownSignal.SIGTERM).then(
() => {
isClosed = true;
},
);
await sleep(16);
// 1. setTimeout is called by the `GracefulShutdownService`
// 2. setTimeout is called above
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(isClosed).toBe(false);
await sleep(16);
expect(isClosed).toBe(false);
await sleep(16);
expect(isClosed).toBe(false);
await sleep(64);
expect(isClosed).toBe(true);
});
it('should not delay the shutdown if the application if the timeout is 0', async () => {
jest.spyOn(global, 'setTimeout');
const setHealthEndpoint = bootstrapTestingModule({
gracefulShutdownTimeoutMs: 0,
}).setHealthEndpoint;
const app = await setHealthEndpoint(({ healthCheck }) =>
healthCheck.check([]),
).start();
const { status } = await request(app.getHttpServer()).get('/health');
expect(status).toBe(200);
await (app.close as NestApplicationContext['close'])(
ShutdownSignal.SIGTERM,
);
expect(setTimeout).not.toHaveBeenCalled();
});
});