Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage #224

Merged
merged 10 commits into from
Jun 30, 2024
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
disable-coverage: true
jobs: 1
files:
- "test/**/*.test.js"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fastify.register(underPressure, {
})

fastify.register(async function (fastify) {
fastify.get('/, {
fastify.get('/', {
config: {
pressureHandler: (req, rep, type, value) => {
if (type === underPressure.TYPE_HEAP_USED_BYTES) {
Expand Down
45 changes: 11 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ const TYPE_HEALTH_CHECK = 'healthCheck'
const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization'

function getSampleInterval (value, eventLoopResolution) {
const defaultValue = monitorEventLoopDelay ? 1000 : 5
const sampleInterval = value || defaultValue
return monitorEventLoopDelay ? Math.max(eventLoopResolution, sampleInterval) : sampleInterval
}
const sampleInterval = value || 1000

async function fastifyUnderPressure (fastify, opts) {
opts = opts || {}
return Math.max(eventLoopResolution, sampleInterval)
}

async function fastifyUnderPressure (fastify, opts = {}) {
const resolution = 10
const sampleInterval = getSampleInterval(opts.sampleInterval, resolution)
const maxEventLoopDelay = opts.maxEventLoopDelay || 0
Expand All @@ -38,22 +36,16 @@ async function fastifyUnderPressure (fastify, opts) {
const checkMaxEventLoopDelay = maxEventLoopDelay > 0
const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
const checkMaxRssBytes = maxRssBytes > 0
const checkMaxEventLoopUtilization = eventLoopUtilization ? maxEventLoopUtilization > 0 : false
const checkMaxEventLoopUtilization = maxEventLoopUtilization > 0

let heapUsed = 0
let rssBytes = 0
let eventLoopDelay = 0
let lastCheck
let histogram
let elu
let eventLoopUtilized = 0

if (monitorEventLoopDelay) {
histogram = monitorEventLoopDelay({ resolution })
histogram.enable()
} else {
lastCheck = now()
}
const histogram = monitorEventLoopDelay({ resolution })
histogram.enable()

if (eventLoopUtilization) {
elu = eventLoopUtilization()
Expand Down Expand Up @@ -164,15 +156,9 @@ async function fastifyUnderPressure (fastify, opts) {
}

function updateEventLoopDelay () {
if (histogram) {
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity
histogram.reset()
} else {
const toCheck = now()
eventLoopDelay = Math.max(0, toCheck - lastCheck - sampleInterval)
lastCheck = toCheck
}
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity
histogram.reset()
}

function updateEventLoopUtilization () {
Expand Down Expand Up @@ -213,11 +199,7 @@ async function fastifyUnderPressure (fastify, opts) {
return true
}

if (checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization) {
return true
}

return false
return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
}

function onRequest (req, reply, next) {
Expand Down Expand Up @@ -304,11 +286,6 @@ async function fastifyUnderPressure (fastify, opts) {
}
}

function now () {
const ts = process.hrtime()
return (ts[0] * 1e3) + (ts[1] / 1e6)
}

module.exports = fp(fastifyUnderPressure, {
// DISABLED UNTIL FINAL fastify@5 RELEASE:
// fastify: '5.x',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"types": "types/index.d.ts",
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
Expand Down
15 changes: 8 additions & 7 deletions test/test.js → test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,23 @@ test('Custom error instance', t => {
})

test('memoryUsage name space', t => {
t.plan(10)
t.plan(9)

const fastify = Fastify()
fastify.register(underPressure, {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 100000000,
maxRssBytes: 100000000,
maxEventLoopUtilization: 0.85
maxEventLoopUtilization: 0.85,
pressureHandler: (req, rep, type, value) => {
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
}
})

fastify.get('/', (req, reply) => {
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
reply.send({ hello: 'world' })
})

Expand All @@ -268,7 +270,6 @@ test('memoryUsage name space', t => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
t.equal(fastify.isUnderPressure(), true)
fastify.close()
})

Expand Down
31 changes: 31 additions & 0 deletions test/issues/216.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { test } = require('tap')
const Fastify = require('fastify')
const underPressure = require('../../index')

test('should be unhealthy if healthCheck throws an error', async t => {
t.plan(4)

const app = Fastify()
app.register(underPressure, {
healthCheck: async () => { throw new Error('Kaboom!') },
healthCheckInterval: 1000,
exposeStatusRoute: true,
pressureHandler: (req, rep, type) => {
t.equal(type, underPressure.TYPE_HEALTH_CHECK)
rep.status(503).send('unhealthy')
}
})

await app.ready()
t.ok(app.isUnderPressure())

const response = await app.inject({
method: 'GET',
url: '/status'
})

t.equal(response.statusCode, 503)
t.equal(response.body, 'unhealthy')

await app.close()
})