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

fix flakyness of tests #181

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/forkRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fork = require('child_process').fork
const resolve = require('path').resolve

module.exports = function forkRequest (address, delay = 100, cb) {
const childProcess = fork(
resolve(__dirname, 'request.js'),
[address, delay],
{ windowsHide: true }
)

childProcess.on('message', (payload) => {
if (payload.error) {
cb(new Error(payload.error), JSON.parse(payload.response), payload.body)
return
}
cb(null, JSON.parse(payload.response), payload.body)
})
childProcess.on('error', err => {
cb(err)
})
}
51 changes: 16 additions & 35 deletions test/pressurehandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require('tap')
const { promisify } = require('util')
const sget = require('simple-get').concat
const forkRequest = require('./forkRequest')
const Fastify = require('fastify')
const { monitorEventLoopDelay } = require('perf_hooks')
const underPressure = require('../index')
Expand Down Expand Up @@ -120,22 +120,16 @@ test('event loop delay', { skip: !monitorEventLoopDelay }, t => {
})

fastify.get('/', (req, rep) => rep.send('A'))

fastify.listen({ port: 0 }, async (err, address) => {
fastify.listen({ port: 3000 }, async (err, address) => {
t.error(err)
fastify.server.unref()

await wait(500)
process.nextTick(() => block(1500))

sget({
method: 'GET',
url: address + '/'
}, (err, response, body) => {
forkRequest(address, 500, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
t.equal(body, 'B')
fastify.close()
})
process.nextTick(() => block(1500))
})
})

Expand All @@ -158,16 +152,13 @@ test('heap bytes', t => {
t.error(err)
fastify.server.unref()

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))

sget({
method: 'GET',
url: address
}, (err, response, body) => {
forkRequest(address, monitorEventLoopDelay ? 750 : 250, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
})
})

Expand All @@ -190,16 +181,13 @@ test('rss bytes', t => {
t.error(err)
fastify.server.unref()

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))

sget({
method: 'GET',
url: address
}, (err, response, body) => {
forkRequest(address, monitorEventLoopDelay ? 750 : 250, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
})
})

Expand All @@ -222,16 +210,13 @@ test('event loop utilization', { skip: !isSupportedVersion }, t => {
t.error(err)
fastify.server.unref()

process.nextTick(() => block(1000))

sget({
method: 'GET',
url: address
}, (err, response, body) => {
forkRequest(address, 500, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})

process.nextTick(() => block(1000))
})
})

Expand Down Expand Up @@ -267,15 +252,11 @@ test('event loop delay (NaN)', { skip: !isSupportedVersion }, t => {
t.error(err)
fastify.server.unref()

process.nextTick(() => block(1000))

sget({
method: 'GET',
url: address
}, (err, response, body) => {
forkRequest(address, 500, (err, response, body) => {
t.error(err)
t.equal(body.toString(), 'B')
fastify.close()
})
process.nextTick(() => block(1000))
})
})
42 changes: 42 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const sget = require('simple-get').concat
const promisify = require('util').promisify
const wait = promisify(setTimeout)

const address = process.argv[2]
const delay = parseInt(process.argv[3])

// custom stringification to avoid circular reference breaking
function stringifyResponse (response) {
return JSON.stringify({
statusCode: response.statusCode,
headers: response.headers
})
}

async function run () {
await wait(delay)
sget({
method: 'GET',
url: address
}, function (error, response, body) {
if (error instanceof Error) {
process.send({
error: error.message,
response: stringifyResponse(response),
body: body.toString()
})
process.exit(1)
}

process.send({
error: null,
response: stringifyResponse(response),
body: body.toString()
})
process.exit()
})
}

run()
11 changes: 4 additions & 7 deletions test/statusRoute.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { test } = require('tap')
const sget = require('simple-get').concat
const forkRequest = require('./forkRequest')
const Fastify = require('fastify')
const { monitorEventLoopDelay } = require('perf_hooks')
const underPressure = require('../index')
Expand All @@ -24,17 +24,14 @@ test('Expose status route', t => {
t.error(err)
fastify.server.unref()

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))

sget({
method: 'GET',
url: `${address}/status`
}, (err, response, body) => {
forkRequest(`${address}/status`, monitorEventLoopDelay ? 750 : 250, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { status: 'ok' })
fastify.close()
})

process.nextTick(() => block(monitorEventLoopDelay ? 1500 : 500))
})
})

Expand Down
Loading