generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhealth.test.ts
81 lines (76 loc) · 2.52 KB
/
health.test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { createServer } from '../src'
describe('Health checks', () => {
beforeEach(() => {
process.env.LOG_LEVEL = 'silent'
})
test('Default configuration exposes a /_health route', async () => {
const server = createServer()
await server.ready()
const res = await server.inject({ method: 'GET', url: '/_health' })
expect(res.statusCode).toEqual(200)
expect(res.json()).toEqual({ status: 'ok' })
})
test('Custom health check handler is called at startup and every 5 seconds', async () => {
jest.useFakeTimers('legacy')
const healthCheck = jest.fn().mockResolvedValue(true)
const server = createServer({
underPressure: {
healthCheck
}
})
await server.ready()
expect(healthCheck).toHaveBeenCalledTimes(1)
jest.advanceTimersByTime(5000)
expect(healthCheck).toHaveBeenCalledTimes(2)
jest.useRealTimers()
})
test('Custom health check throwing results in 503', async () => {
jest.useFakeTimers('legacy')
const healthCheck = jest
.fn()
.mockResolvedValueOnce(true) // First call passes (setup)
.mockResolvedValueOnce(true) // Second call passes (GET /_health 1)
.mockRejectedValueOnce(false) // Then it fails (GET /_health 2)
const server = createServer({
underPressure: {
healthCheck,
exposeStatusRoute: {
url: '/_health',
routeOpts: {
logLevel: 'silent'
}
}
}
})
await server.ready()
const res1 = await server.inject({ method: 'GET', url: '/_health' })
expect(res1.statusCode).toEqual(200)
expect(res1.json()).toEqual({ status: 'ok' })
jest.advanceTimersByTime(5000)
const res2 = await server.inject({ method: 'GET', url: '/_health' })
expect(res2.statusCode).toEqual(503)
expect(res2.json()).toEqual({
code: 'FST_UNDER_PRESSURE',
error: 'Service Unavailable',
message: 'Service Unavailable',
statusCode: 503
})
jest.useRealTimers()
})
test('Disabled health monitoring', async () => {
process.env.FASTIFY_MICRO_DISABLE_SERVICE_HEALTH_MONITORING = 'true'
jest.useFakeTimers('legacy')
const healthCheck = jest.fn().mockResolvedValue(true)
const server = createServer({
underPressure: {
healthCheck
}
})
await server.ready()
expect(healthCheck).not.toHaveBeenCalled()
jest.advanceTimersByTime(5000)
expect(healthCheck).not.toHaveBeenCalled()
jest.useRealTimers()
process.env.FASTIFY_MICRO_DISABLE_SERVICE_HEALTH_MONITORING = undefined
})
})