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

feat: port balanced-pool, ca-fingerprint, client-abort tests to node:test #2584

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
client-abort
  • Loading branch information
sosukesuzuki committed Jan 3, 2024
commit c68370fcf95d63b363b623bda344c9919dfc97a8
103 changes: 57 additions & 46 deletions test/client-abort.js → test/node-test/client-abort.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
'use strict'

const { test } = require('tap')
const { Client, errors } = require('..')
const { test } = require('node:test')
const { Client, errors } = require('../..')
const { createServer } = require('http')
const { Readable } = require('stream')
const { tspl } = require('@matteo.collina/tspl')

class OnAbortError extends Error {}

test('aborted response errors', (t) => {
t.plan(3)
test('aborted response errors', async (t) => {
const p = tspl(t, { plan: 3 })

const server = createServer()
server.once('request', (req, res) => {
// TODO: res.write will cause body to emit 'error' twice
// due to bug in readable-stream.
res.end('asd')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
t.error(err)
p.ifError(err)
body.destroy()
body
.on('error', err => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
.on('close', () => {
t.pass()
p.ok(1)
})
})
})

await p.completed
})

test('aborted req', (t) => {
t.plan(1)
test('aborted req', async (t) => {
const p = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
res.end(Buffer.alloc(4 + 1, 'a'))
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.request({
method: 'POST',
Expand All @@ -59,22 +62,24 @@ test('aborted req', (t) => {
}
})
}, (err) => {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
})
})

await p.completed
})

test('abort', (t) => {
t.plan(2)
test('abort', async (t) => {
const p = tspl(t, { plan: 2 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.dispatch({
method: 'GET',
Expand All @@ -84,37 +89,39 @@ test('abort', (t) => {
setImmediate(abort)
},
onHeaders () {
t.fail()
p.ok(0)
},
onData () {
t.fail()
p.ok(0)
},
onComplete () {
t.fail()
p.ok(0)
},
onError (err) {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
}
})

client.on('disconnect', () => {
t.pass()
p.ok(1)
})
})

await p.completed
})

test('abort pipelined', (t) => {
t.plan(6)
test('abort pipelined', async (t) => {
const p = tspl(t, { plan: 6 })

const server = createServer((req, res) => {
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
pipelining: 2
})
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

let counter = 0
client.dispatch({
Expand All @@ -126,19 +133,19 @@ test('abort pipelined', (t) => {
if (counter++ === 1) {
abort()
}
t.pass()
p.ok(1)
},
onHeaders () {
t.fail()
p.ok(0)
},
onData () {
t.fail()
p.ok(0)
},
onComplete () {
t.fail()
p.ok(0)
},
onError (err) {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
}
})

Expand All @@ -150,36 +157,38 @@ test('abort pipelined', (t) => {
abort()
},
onHeaders () {
t.fail()
p.ok(0)
},
onData () {
t.fail()
p.ok(0)
},
onComplete () {
t.fail()
p.ok(0)
},
onError (err) {
t.type(err, errors.RequestAbortedError)
p.ok(err instanceof errors.RequestAbortedError)
}
})

client.on('disconnect', () => {
t.pass()
p.ok(1)
})
})

await p.completed
})

test('propagate unallowed throws in request.onError', (t) => {
t.plan(2)
test('propagate unallowed throws in request.onError', async (t) => {
const p = tspl(t, { plan: 2 })

const server = createServer((req, res) => {
res.end()
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))
t.after(client.destroy.bind(client))

client.dispatch({
method: 'GET',
Expand All @@ -189,25 +198,27 @@ test('propagate unallowed throws in request.onError', (t) => {
setImmediate(abort)
},
onHeaders () {
t.pass()
p.ok(0)
},
onData () {
t.pass()
p.ok(0)
},
onComplete () {
t.pass()
p.ok(0)
Comment on lines +201 to +207
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really passes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that these functions were not being called even when we was using tap. I think p.ok(0) is better than p.ok(1), because we expect it to never be called.

},
onError () {
throw new OnAbortError('error')
}
})

client.on('error', (err) => {
t.type(err, OnAbortError)
p.ok(err instanceof OnAbortError)
})

client.on('disconnect', () => {
t.pass()
p.ok(1)
})
})

await p.completed
})
Loading