Skip to content

Commit

Permalink
100% code coverage and fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Nov 27, 2024
1 parent 09e9490 commit 3886bf6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ function createThreadInterceptor (opts) {

try {
handler.onRequestStart(controller, {})
if (controller.aborted) {
handler.onResponseError(controller, controller.reason)
return
}
handler.onResponseStart(
controller,
res.statusCode,
Expand All @@ -117,12 +121,8 @@ function createThreadInterceptor (opts) {
return
}

if (!controller.aborted) {
handler.onResponseData(controller, res.rawPayload)
handler.onResponseEnd(controller, [])
} else {
handler.onResponseError(controller, controller.reason)
}
handler.onResponseData(controller, res.rawPayload)
handler.onResponseEnd(controller, [])
})

return true
Expand Down
3 changes: 2 additions & 1 deletion lib/wrap-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class WrapHandler {
this.#handler.onComplete?.([])
}

// TODO(mcollina): I do not know how to trigger these
/* c8 ignore next 3 */
onResponseError (controller, err) {
process._rawDebug('WrapHandler.onResponseError', err)
this.#handler.onError?.(err)
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,23 @@ test('Get binary file', async (t) => {

deepStrictEqual(read, expected)
})

test('aborting a request', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const abortController = new AbortController()

const agent = new Agent().compose(interceptor)
abortController.abort()

await rejects(request('http://myserver.local', {
dispatcher: agent,
signal: abortController.signal,
}))
})
6 changes: 6 additions & 0 deletions test/fixtures/worker1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fastify = require('fastify')
const { wire } = require('../../')
const fastifyStatic = require('@fastify/static')
const { join } = require('path')
const { setTimeout: sleep } = require('timers/promises')

const app = fastify()

Expand Down Expand Up @@ -47,4 +48,9 @@ app.post('/echo-body', (req, reply) => {
reply.send(req.body)
})

app.get('/long', async (req, reply) => {
await sleep(1000)
return { hello: 'world' }
})

wire({ server: app, port: parentPort })

0 comments on commit 3886bf6

Please sign in to comment.