Skip to content

Commit

Permalink
Use p.completed
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Dec 30, 2023
1 parent 5fe2d7e commit 86d790f
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions test/node-test/abort-event-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { createReadStream } = require('fs')
const { Readable } = require('stream')
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)', async (t) => {
Expand All @@ -25,8 +24,6 @@ test('Abort before sending request (no body)', async (t) => {

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()
Expand All @@ -40,7 +37,6 @@ test('Abort before sending request (no body)', async (t) => {
})
response.body.on('end', () => {
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
resolve()
})
})

Expand All @@ -60,7 +56,7 @@ test('Abort before sending request (no body)', async (t) => {
ee.emit('abort')
})

await promise
await p.completed
})

test('Abort before sending request (no body) async iterator', async (t) => {
Expand All @@ -77,8 +73,6 @@ test('Abort before sending request (no body) async iterator', async (t) => {

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()
Expand All @@ -92,7 +86,6 @@ test('Abort before sending request (no body) async iterator', async (t) => {
})
response.body.on('end', () => {
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
resolve()
})
})

Expand All @@ -109,7 +102,7 @@ test('Abort before sending request (no body) async iterator', async (t) => {
ee.emit('abort')
})

await promise
await p.completed
})

test('Abort while waiting response (no body)', async (t) => {
Expand All @@ -123,19 +116,16 @@ test('Abort while waiting response (no body)', async (t) => {
})
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()

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

client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})

await promise
await p.completed
})

test('Abort while waiting response (write headers started) (no body)', async (t) => {
Expand All @@ -150,18 +140,16 @@ test('Abort while waiting response (write headers started) (no body)', async (t)
})
t.after(server.close.bind(server))

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

client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})

await promise
await p.completed
})

test('Abort while waiting response (write headers and write body started) (no body)', async (t) => {
Expand All @@ -174,7 +162,6 @@ test('Abort while waiting response (write headers and write body started) (no bo
})
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.after(client.destroy.bind(client))
Expand All @@ -186,11 +173,10 @@ test('Abort while waiting response (write headers and write body started) (no bo
})
response.body.on('error', err => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
})
await promise
await p.completed
})

function waitingWithBody (body, type) {
Expand All @@ -205,17 +191,15 @@ function waitingWithBody (body, type) {
})
t.after(server.close.bind(server))

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

client.request({ path: '/', method: 'POST', body, signal: ee }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
await promise
await p.completed
})
}

Expand All @@ -237,17 +221,15 @@ function writeHeadersStartedWithBody (body, type) {
})
t.after(server.close.bind(server))

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

client.request({ path: '/', method: 'POST', body, signal: ee }, (err, response) => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
await promise
await p.completed
})
}

Expand All @@ -267,7 +249,6 @@ function writeBodyStartedWithBody (body, type) {
})
t.after(server.close.bind(server))

const { promise, resolve } = promiseWithResolvers()
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.after(client.destroy.bind(client))
Expand All @@ -279,11 +260,10 @@ function writeBodyStartedWithBody (body, type) {
})
response.body.on('error', err => {
ttype(p, err, errors.RequestAbortedError)
resolve()
})
})
})
await promise
await p.completed
})
}

Expand Down

0 comments on commit 86d790f

Please sign in to comment.