Skip to content

Commit

Permalink
Port abort-event-emitter to node:test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Dec 30, 2023
1 parent 8d2965b commit 5fe2d7e
Showing 1 changed file with 85 additions and 51 deletions.
136 changes: 85 additions & 51 deletions test/abort-event-emitter.js → test/node-test/abort-event-emitter.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,70 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const EventEmitter = require('events')
const { Client, errors } = require('..')
const { Client, errors } = require('../..')
const { createServer } = require('http')
const { createReadStream } = require('fs')
const { Readable } = require('stream')
const { wrapWithAsyncIterable } = require('./utils/async-iterators')
const { tspl } = require('@matteo.collina/tspl')
const { wrapWithAsyncIterable } = require('../utils/async-iterators')
const { promiseWithResolvers } = require('../utils/promise')
const { ttype } = require('../utils/node-test')

test('Abort before sending request (no body)', (t) => {
t.plan(4)
test('Abort before sending request (no body)', async (t) => {
const p = tspl(t, { plan: 4 })

let count = 0
const server = createServer((req, res) => {
if (count === 1) {
t.fail('The second request should never be executed')
p.fail('The second request should never be executed')
}
count += 1
res.end('hello')
})

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()

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

client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
p.ifError(err)
const bufs = []
response.body.on('data', (buf) => {
bufs.push(buf)
})
response.body.on('end', () => {
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
resolve()
})
})

const body = new Readable({ read () { } })
body.on('error', (err) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
})
client.request({
path: '/',
method: 'GET',
signal: ee,
body
}, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
})

ee.emit('abort')
})

await promise
})

test('Abort before sending request (no body) async iterator', (t) => {
t.plan(3)
test('Abort before sending request (no body) async iterator', async (t) => {
const p = tspl(t, { plan: 3 })

let count = 0
const server = createServer((req, res) => {
Expand All @@ -67,21 +75,24 @@ test('Abort before sending request (no body) async iterator', (t) => {
res.end('hello')
})

t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()

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

client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
p.ifError(err)
const bufs = []
response.body.on('data', (buf) => {
bufs.push(buf)
})
response.body.on('end', () => {
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
resolve()
})
})

Expand All @@ -92,36 +103,43 @@ test('Abort before sending request (no body) async iterator', (t) => {
signal: ee,
body
}, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
})

ee.emit('abort')
})

await promise
})

test('Abort while waiting response (no body)', (t) => {
t.plan(1)
test('Abort while waiting response (no body)', async (t) => {
const p = tspl(t, { plan: 1 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
ee.emit('abort')
res.setHeader('content-type', 'text/plain')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()

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', signal: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})

await promise
})

test('Abort while waiting response (write headers started) (no body)', (t) => {
t.plan(1)
test('Abort while waiting response (write headers started) (no body)', async (t) => {
const p = tspl(t, { plan: 1 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
Expand All @@ -130,64 +148,74 @@ test('Abort while waiting response (write headers started) (no body)', (t) => {
ee.emit('abort')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
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', signal: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})

await promise
})

test('Abort while waiting response (write headers and write body started) (no body)', (t) => {
t.plan(2)
test('Abort while waiting response (write headers and write body started) (no body)', async (t) => {
const p = tspl(t, { plan: 2 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.write('hello')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
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', signal: ee }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
ee.emit('abort')
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
})
await promise
})

function waitingWithBody (body, type) {
test(`Abort while waiting response (with body ${type})`, (t) => {
t.plan(1)
test(`Abort while waiting response (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 1 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
ee.emit('abort')
res.setHeader('content-type', 'text/plain')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
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: 'POST', body, signal: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
await promise
})
}

Expand All @@ -197,8 +225,8 @@ waitingWithBody(new Uint8Array([42]), 'Uint8Array')
waitingWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

function writeHeadersStartedWithBody (body, type) {
test(`Abort while waiting response (write headers started) (with body ${type})`, (t) => {
t.plan(1)
test(`Abort while waiting response (write headers started) (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 1 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
Expand All @@ -207,16 +235,19 @@ function writeHeadersStartedWithBody (body, type) {
ee.emit('abort')
res.end('hello world')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
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: 'POST', body, signal: ee }, (err, response) => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
await promise
})
}

Expand All @@ -226,30 +257,33 @@ writeHeadersStartedWithBody(new Uint8Array([42]), 'Uint8Array')
writeHeadersStartedWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')

function writeBodyStartedWithBody (body, type) {
test(`Abort while waiting response (write headers and write body started) (with body ${type})`, (t) => {
t.plan(2)
test(`Abort while waiting response (write headers and write body started) (with body ${type})`, async (t) => {
const p = tspl(t, { plan: 2 })

const ee = new EventEmitter()
const server = createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.write('hello')
})
t.teardown(server.close.bind(server))
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
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: 'POST', body, signal: ee }, (err, response) => {
t.error(err)
p.ifError(err)
response.body.on('data', () => {
ee.emit('abort')
})
response.body.on('error', err => {
t.type(err, errors.RequestAbortedError)
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
})
await promise
})
}

Expand Down

0 comments on commit 5fe2d7e

Please sign in to comment.