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
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const TYPE_HEALTH_CHECK = 'healthCheck'
const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization'

function getSampleInterval (value, eventLoopResolution) {
/* c8 ignore start - @see: https://github.com/fastify/under-pressure/issues/18 */
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved
const defaultValue = monitorEventLoopDelay ? 1000 : 5
/* c8 ignore end */
const sampleInterval = value || defaultValue
return monitorEventLoopDelay ? Math.max(eventLoopResolution, sampleInterval) : sampleInterval
}

async function fastifyUnderPressure (fastify, opts) {
opts = opts || {}

async function fastifyUnderPressure (fastify, opts = {}) {
const resolution = 10
const sampleInterval = getSampleInterval(opts.sampleInterval, resolution)
const maxEventLoopDelay = opts.maxEventLoopDelay || 0
Expand Down Expand Up @@ -51,9 +51,11 @@ async function fastifyUnderPressure (fastify, opts) {
if (monitorEventLoopDelay) {
histogram = monitorEventLoopDelay({ resolution })
histogram.enable()
/* c8 ignore start - @see: https://github.com/fastify/under-pressure/issues/18 */
} else {
lastCheck = now()
}
/* c8 ignore stop */

if (eventLoopUtilization) {
elu = eventLoopUtilization()
Expand Down Expand Up @@ -168,11 +170,13 @@ async function fastifyUnderPressure (fastify, opts) {
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity
histogram.reset()
/* c8 ignore start - @see: https://github.com/fastify/under-pressure/issues/18 */
} else {
const toCheck = now()
eventLoopDelay = Math.max(0, toCheck - lastCheck - sampleInterval)
lastCheck = toCheck
}
/* c8 ignore stop */
}

function updateEventLoopUtilization () {
Expand Down Expand Up @@ -213,11 +217,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,10 +304,12 @@ async function fastifyUnderPressure (fastify, opts) {
}
}

/* c8 ignore start */
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved
function now () {
const ts = process.hrtime()
return (ts[0] * 1e3) + (ts[1] / 1e6)
}
/* c8 ignore end */

module.exports = fp(fastifyUnderPressure, {
// DISABLED UNTIL FINAL fastify@5 RELEASE:
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(), 'App should be under pressure due to failed health check')

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

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

await app.close()
})
Loading