-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add e2e test for graceful shutdown (#2450)
* test: add e2e test for graceful shutdown Co-authored-by: François <[email protected]> --------- Co-authored-by: François <[email protected]>
- Loading branch information
1 parent
d383828
commit 198080e
Showing
4 changed files
with
97 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters