Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
test: add tests for abort after connect (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored and jacobheun committed Jun 11, 2019
1 parent 68e2791 commit a6d6098
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions src/dial-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = (common) => {
expect.fail('Did not throw error attempting to connect to non-existent listener')
})

it('cancel before dialing', async () => {
it('abort before dialing throws AbortError', async () => {
const controller = new AbortController()
controller.abort()
const socket = transport.dial(addrs[0], { signal: controller.signal })
Expand All @@ -68,8 +68,8 @@ module.exports = (common) => {
expect.fail('Did not throw error with code ' + AbortError.code)
})

it('cancel while dialing', async () => {
// Add a delay to connect() so that we can cancel while the dial is in
it('abort while dialing throws AbortError', async () => {
// Add a delay to connect() so that we can abort while the dial is in
// progress
connector.delay(100)

Expand All @@ -88,5 +88,76 @@ module.exports = (common) => {
}
expect.fail('Did not throw error with code ' + AbortError.code)
})

it('abort while reading throws AbortError', async () => {
// Add a delay to the response from the server
async function * delayedResponse (source) {
for await (const val of source) {
await new Promise((resolve) => setTimeout(resolve, 1000))
yield val
}
}
const delayedListener = transport.createListener(async (conn) => {
await pipe(conn, delayedResponse, conn)
})
await delayedListener.listen(addrs[1])

// Create an abort signal and dial the socket
const controller = new AbortController()
const socket = await transport.dial(addrs[1], { signal: controller.signal })

try {
// Set a timeout to abort before the server responds
setTimeout(() => controller.abort(), 100)

// An AbortError should be thrown before the pipe completes
const s = goodbye({ source: ['hey'], sink: collect })
await pipe(s, socket, s)
} catch (err) {
expect(err.code).to.eql(AbortError.code)
expect(err.type).to.eql(AbortError.type)
return
} finally {
await delayedListener.close()
}
expect.fail('Did not throw error with code ' + AbortError.code)
})

it('abort while writing does not throw AbortError', async () => {
// Record values received by the listener
const recorded = []
async function * recorderTransform (source) {
for await (const val of source) {
recorded.push(val)
yield val
}
}
const recordListener = transport.createListener(async (conn) => {
await pipe(conn, recorderTransform, conn)
})
await recordListener.listen(addrs[1])

// Create an abort signal and dial the socket
const controller = new AbortController()
const socket = await transport.dial(addrs[1], { signal: controller.signal })

// Set a timeout to abort before writing has completed
setTimeout(() => controller.abort(), 100)

try {
// The pipe should write to the socket until aborted
await pipe(
async function * () {
yield 'hey'
await new Promise((resolve) => setTimeout(resolve, 200))
yield 'there'
},
socket)
expect(recorded.length).to.eql(1)
expect(recorded[0].toString()).to.eql('hey')
} finally {
await recordListener.close()
}
})
})
}

0 comments on commit a6d6098

Please sign in to comment.