Skip to content
Open
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
476 changes: 225 additions & 251 deletions test/node-test/agent.js

Large diffs are not rendered by default.

90 changes: 43 additions & 47 deletions test/node-test/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { createServer } = require('node:http')
const { createHook, executionAsyncId } = require('node:async_hooks')
const { readFile } = require('node:fs')
const { PassThrough } = require('node:stream')
const { tspl } = require('@matteo.collina/tspl')
const { closeServerAsPromise } = require('../utils/node-http')

const transactions = new Map()
Expand Down Expand Up @@ -34,13 +33,13 @@ const hook = createHook({

hook.enable()

test('async hooks', async (t) => {
const p = tspl(t, { plan: 31 })
test('async hooks', (t, done) => {
t.plan(31)

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.setHeader('content-type', 'text/plain')
readFile(__filename, (err, buf) => {
p.ifError(err)
t.assert.ifError(err)
const buf1 = buf.slice(0, buf.length / 2)
const buf2 = buf.slice(buf.length / 2)
// we split the file so that it's received in 2 chunks
Expand All @@ -58,100 +57,99 @@ test('async hooks', async (t) => {
t.after(() => client.destroy.bind(client)())

client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
t.assert.ifError(err)
body.resume()
p.deepStrictEqual(getCurrentTransaction(), null)
t.assert.deepStrictEqual(getCurrentTransaction(), null)

setCurrentTransaction({ hello: 'world2' })

client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
t.assert.ifError(err)
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })

body.once('data', () => {
p.ok(1, 1)
t.assert.ok(1, 1)
body.resume()
})

body.on('end', () => {
p.ok(1, 1)
t.assert.ok(1, 1)
})
})
})

client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
t.assert.ifError(err)
body.resume()
p.deepStrictEqual(getCurrentTransaction(), null)
t.assert.deepStrictEqual(getCurrentTransaction(), null)

setCurrentTransaction({ hello: 'world' })

client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
t.assert.ifError(err)
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })

body.once('data', () => {
p.ok(1)
t.assert.ok(1)
body.resume()
})

body.on('end', () => {
p.ok(1)
t.assert.ok(1)
})
})
})

client.request({ path: '/', method: 'HEAD' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
t.assert.ifError(err)
body.resume()
p.deepStrictEqual(getCurrentTransaction(), null)
t.assert.deepStrictEqual(getCurrentTransaction(), null)

setCurrentTransaction({ hello: 'world' })

client.request({ path: '/', method: 'HEAD' }, (err, { statusCode, headers, body }) => {
p.ifError(err)
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
t.assert.ifError(err)
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })

body.once('data', () => {
p.ok(1)
t.assert.ok(1)
body.resume()
})

body.on('end', () => {
p.ok(1)
t.assert.ok(1)
})
})
})

client.stream({ path: '/', method: 'GET' }, () => {
p.strictEqual(getCurrentTransaction(), null)
t.assert.strictEqual(getCurrentTransaction(), null)
return new PassThrough().resume()
}, (err) => {
p.ifError(err)
p.deepStrictEqual(getCurrentTransaction(), null)
t.assert.ifError(err)
t.assert.deepStrictEqual(getCurrentTransaction(), null)

setCurrentTransaction({ hello: 'world' })

client.stream({ path: '/', method: 'GET' }, () => {
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
return new PassThrough().resume()
}, (err) => {
p.ifError(err)
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
t.assert.ifError(err)
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
done()
})
})
})

await p.completed
})

test('async hooks client is destroyed', async (t) => {
const p = tspl(t, { plan: 7 })
test('async hooks client is destroyed', (t, done) => {
t.plan(7)

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.setHeader('content-type', 'text/plain')
readFile(__filename, (err, buf) => {
p.ifError(err)
t.assert.ifError(err)
res.write('asd')
})
})
Expand All @@ -162,30 +160,29 @@ test('async hooks client is destroyed', async (t) => {
t.after(client.destroy.bind(client))

client.request({ path: '/', method: 'GET' }, (err, { body }) => {
p.ifError(err)
t.assert.ifError(err)
body.resume()
body.on('error', (err) => {
p.ok(err)
t.assert.ok(err)
done()
})
p.deepStrictEqual(getCurrentTransaction(), null)
t.assert.deepStrictEqual(getCurrentTransaction(), null)

setCurrentTransaction({ hello: 'world2' })

client.request({ path: '/', method: 'GET' }, (err) => {
p.strictEqual(err.message, 'The client is destroyed')
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
t.assert.strictEqual(err.message, 'The client is destroyed')
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
})
client.destroy((err) => {
p.ifError(err)
t.assert.ifError(err)
})
})
})

await p.completed
})

test('async hooks pipeline handler', async (t) => {
const p = tspl(t, { plan: 2 })
test('async hooks pipeline handler', (t, done) => {
t.plan(2)

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.end('hello')
Expand All @@ -200,15 +197,14 @@ test('async hooks pipeline handler', async (t) => {

client
.pipeline({ path: '/', method: 'GET' }, ({ body }) => {
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
t.assert.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
return body
})
.on('close', () => {
p.ok(1)
t.assert.ok(1)
done()
})
.resume()
.end()
})

await p.completed
})
63 changes: 28 additions & 35 deletions test/node-test/autoselectfamily.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { Resolver } = require('node:dns')
const dnsPacket = require('dns-packet')
const { createServer } = require('node:http')
const { Client, Agent, request } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

/*
* IMPORTANT
Expand Down Expand Up @@ -63,8 +62,8 @@ function createDnsServer (ipv6Addr, ipv4Addr, cb) {
})
}

test('with autoSelectFamily enable the request succeeds when using request', { skip }, async (t) => {
const p = tspl(t, { plan: 3 })
test('with autoSelectFamily enable the request succeeds when using request', { skip }, (t, done) => {
t.plan(3)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -84,7 +83,7 @@ test('with autoSelectFamily enable the request succeeds when using request', { s
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
p.ifError(err)
t.assert.ifError(err)

let response = Buffer.alloc(0)

Expand All @@ -93,18 +92,17 @@ test('with autoSelectFamily enable the request succeeds when using request', { s
})

body.on('end', () => {
p.strictEqual(statusCode, 200)
p.strictEqual(response.toString('utf-8'), 'hello')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(response.toString('utf-8'), 'hello')
done()
})
})
})
})

await p.completed
})

test('with autoSelectFamily enable the request succeeds when using a client', { skip }, async (t) => {
const p = tspl(t, { plan: 3 })
test('with autoSelectFamily enable the request succeeds when using a client', { skip }, (t, done) => {
t.plan(3)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -125,7 +123,7 @@ test('with autoSelectFamily enable the request succeeds when using a client', {
path: '/',
method: 'GET'
}, (err, { statusCode, body }) => {
p.ifError(err)
t.assert.ifError(err)

let response = Buffer.alloc(0)

Expand All @@ -134,18 +132,17 @@ test('with autoSelectFamily enable the request succeeds when using a client', {
})

body.on('end', () => {
p.strictEqual(statusCode, 200)
p.strictEqual(response.toString('utf-8'), 'hello')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(response.toString('utf-8'), 'hello')
done()
})
})
})
})

await p.completed
})

test('with autoSelectFamily disabled the request fails when using request', { skip }, async (t) => {
const p = tspl(t, { plan: 1 })
test('with autoSelectFamily disabled the request fails when using request', { skip }, (t, done) => {
t.plan(1)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -164,16 +161,15 @@ test('with autoSelectFamily disabled the request fails when using request', { sk
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
t.assert.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
done()
})
})
})

await p.completed
})

test('with autoSelectFamily disabled via Agent.Options["autoSelectFamily"] the request fails when using request', { skip }, async (t) => {
const p = tspl(t, { plan: 1 })
test('with autoSelectFamily disabled via Agent.Options["autoSelectFamily"] the request fails when using request', { skip }, (t, done) => {
t.plan(1)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -192,16 +188,15 @@ test('with autoSelectFamily disabled via Agent.Options["autoSelectFamily"] the r
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
t.assert.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
done()
})
})
})

await p.completed
})

test('with autoSelectFamily disabled the request fails when using a client', { skip }, async (t) => {
const p = tspl(t, { plan: 1 })
test('with autoSelectFamily disabled the request fails when using a client', { skip }, (t, done) => {
t.plan(1)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -221,16 +216,15 @@ test('with autoSelectFamily disabled the request fails when using a client', { s
path: '/',
method: 'GET'
}, (err, { statusCode, body }) => {
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
t.assert.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
done()
})
})
})

await p.completed
})

test('with autoSelectFamily disabled via Client.Options["autoSelectFamily"] the request fails when using a client', { skip }, async (t) => {
const p = tspl(t, { plan: 1 })
test('with autoSelectFamily disabled via Client.Options["autoSelectFamily"] the request fails when using a client', { skip }, (t, done) => {
t.plan(1)

createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand All @@ -250,10 +244,9 @@ test('with autoSelectFamily disabled via Client.Options["autoSelectFamily"] the
path: '/',
method: 'GET'
}, (err, { statusCode, body }) => {
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
t.assert.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
done()
})
})
})

await p.completed
})
Loading
Loading