Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Dec 17, 2023
1 parent 9da16d1 commit dba5e37
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/multistream-select/src/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
sentProtocol = true
sendingProtocol = false
doneSendingProtocol.resolve()

// read the negotiation response but don't block more sending
negotiate()
.catch(err => {
options.log.error('could not finish optimistic protocol negotiation of %s', protocol, err)

Check warning on line 189 in packages/multistream-select/src/select.ts

View check run for this annotation

Codecov / codecov/patch

packages/multistream-select/src/select.ts#L189

Added line #L189 was not covered by tests
})
} else {
yield buf
}
Expand Down
41 changes: 36 additions & 5 deletions packages/multistream-select/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Dialer and Listener integration', () => {
expect(new Uint8ArrayList(...output[0]).slice()).to.eql(new Uint8ArrayList(...input).slice())
})

it('should handle and lazySelect', async () => {
it('should handle and optimistically select', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

Expand All @@ -113,7 +113,7 @@ describe('Dialer and Listener integration', () => {
expect(new Uint8ArrayList(...dialerOut).slice()).to.eql(new Uint8ArrayList(...input).slice())
})

it('should handle and lazySelect that fails', async () => {
it('should handle and optimistically select that fails', async () => {
const protocol = '/echo/1.0.0'
const otherProtocol = '/echo/2.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()
Expand All @@ -134,7 +134,7 @@ describe('Dialer and Listener integration', () => {
.to.eventually.be.rejected.with.property('code', 'ERR_UNSUPPORTED_PROTOCOL')
})

it('should handle and lazySelect only by reading', async () => {
it('should handle and optimistically select only by reading', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

Expand Down Expand Up @@ -162,7 +162,38 @@ describe('Dialer and Listener integration', () => {
expect(new Uint8ArrayList(...dialerOut).slice()).to.eql(new Uint8ArrayList(...input).slice())
})

it('should handle and lazySelect only by reading that fails', async () => {
it('should handle and optimistically select only by writing', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

const dialerSelection = await mss.select(pair[0], [protocol], {
log: logger('mss:dialer')
})
expect(dialerSelection.protocol).to.equal(protocol)

// ensure stream is usable after selection
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]

const [listenerOut] = await Promise.all([
// the listener handles the incoming stream
mss.handle(pair[1], protocol, {
log: logger('mss:listener')
}).then(async result => {
// the listener reads from the incoming stream
return pipe(result.stream, async source => all(source))
}),
Promise.resolve().then(async () => {
// the dialer just writes to the stream
await pair[0].sink(async function * () {
yield * input
}())
})
])

expect(new Uint8ArrayList(...listenerOut).slice()).to.eql(new Uint8ArrayList(...input).slice())
})

it('should handle and optimistically select only by reading that fails', async () => {
const protocol = '/echo/1.0.0'
const otherProtocol = '/echo/2.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()
Expand All @@ -183,7 +214,7 @@ describe('Dialer and Listener integration', () => {
.to.eventually.be.rejected.with.property('code', 'ERR_UNSUPPORTED_PROTOCOL')
})

it('should abort an unhandled lazySelect', async () => {
it('should abort an unhandled optimistically select', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

Expand Down

0 comments on commit dba5e37

Please sign in to comment.