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 client-connect, client-dispatch, client-errors test to node:test #2591

Merged
merged 11 commits into from
Jan 16, 2024
Prev Previous commit
Leave some tests on tap
  • Loading branch information
sosukesuzuki committed Jan 11, 2024
commit bc3668659331f7b1f8349b2b1f8432efcc764599
38 changes: 38 additions & 0 deletions test/client-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const { test } = require('tap')
const { Client, errors } = require('..')
const http = require('http')
const EE = require('events')
const { kBusy } = require('../lib/core/symbols')

// TODO: move to test/node-test/client-connect.js
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
test('connect aborted after connect', (t) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When porting this test to node:test, the test fails with the message Promise resolution is still pending but the event loop has already resolved. I investigated the cause, but couldn't figure it out, so I'll leave it on tap. I'll investigate further later.

t.plan(3)

const signal = new EE()
const server = http.createServer((req, res) => {
t.fail()
})
server.on('connect', (req, c, firstBodyChunk) => {
signal.emit('abort')
})
t.teardown(server.close.bind(server))

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

client.connect({
path: '/',
signal,
opaque: 'asd'
}, (err, { opaque }) => {
t.equal(opaque, 'asd')
t.type(err, errors.RequestAbortedError)
})
t.equal(client[kBusy], true)
})
})
28 changes: 28 additions & 0 deletions test/client-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const { test } = require('tap')
const { Client } = require('..')
const net = require('net')

// TODO: move to test/node-test/client-connect.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

test('parser error', (t) => {
t.plan(2)

const server = net.createServer()
server.once('connection', (socket) => {
socket.write('asd\n\r213123')
})
t.teardown(server.close.bind(server))

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

client.request({ path: '/', method: 'GET' }, (err) => {
t.ok(err)
client.close((err) => {
t.error(err)
})
})
})
})
33 changes: 0 additions & 33 deletions test/node-test/client-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,36 +291,3 @@ test('connect invalid signal', async (t) => {

await p.completed
})

test('connect aborted after connect', async (t) => {
const p = tspl(t, { plan: 3 })

const signal = new EE()
const server = http.createServer((req, res) => {
p.ok(0)
})
server.on('connect', (req, c, firstBodyChunk) => {
signal.emit('abort')
})
// FIXME: use closeServerAsPromise
t.after(server.close.bind(server))

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

client.connect({
path: '/',
signal,
opaque: 'asd'
}, (err, { opaque }) => {
p.strictEqual(opaque, 'asd')
p.ok(err instanceof errors.RequestAbortedError)
})
p.strictEqual(client[kBusy], true)
})

await p.completed
})
26 changes: 0 additions & 26 deletions test/node-test/client-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { Client, Pool, errors } = require('../..')
const { createServer } = require('http')
const https = require('https')
const pem = require('https-pem')
const net = require('net')
const { Readable } = require('stream')
const { tspl } = require('@matteo.collina/tspl')

Expand Down Expand Up @@ -840,31 +839,6 @@ test('validate request body', async (t) => {
await p.completed
})

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

const server = net.createServer()
server.once('connection', (socket) => {
socket.write('asd\n\r213123')
})
// FIXME: use closeServerAsPromise
t.after(server.close.bind(server))

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

client.request({ path: '/', method: 'GET' }, (err) => {
p.ok(err)
client.close((err) => {
p.ifError(err)
})
})
})

await p.completed
})

function socketFailWrite (type) {
test(`socket fail while writing ${type} request body`, async (t) => {
const p = tspl(t, { plan: 2 })
Expand Down
Loading