Skip to content

Commit dba5e37

Browse files
committed
chore: add test
1 parent 9da16d1 commit dba5e37

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

packages/multistream-select/src/select.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
182182
sentProtocol = true
183183
sendingProtocol = false
184184
doneSendingProtocol.resolve()
185+
186+
// read the negotiation response but don't block more sending
187+
negotiate()
188+
.catch(err => {
189+
options.log.error('could not finish optimistic protocol negotiation of %s', protocol, err)
190+
})
185191
} else {
186192
yield buf
187193
}

packages/multistream-select/test/integration.spec.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('Dialer and Listener integration', () => {
8888
expect(new Uint8ArrayList(...output[0]).slice()).to.eql(new Uint8ArrayList(...input).slice())
8989
})
9090

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

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

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

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

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

165-
it('should handle and lazySelect only by reading that fails', async () => {
165+
it('should handle and optimistically select only by writing', async () => {
166+
const protocol = '/echo/1.0.0'
167+
const pair = duplexPair<Uint8ArrayList | Uint8Array>()
168+
169+
const dialerSelection = await mss.select(pair[0], [protocol], {
170+
log: logger('mss:dialer')
171+
})
172+
expect(dialerSelection.protocol).to.equal(protocol)
173+
174+
// ensure stream is usable after selection
175+
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
176+
177+
const [listenerOut] = await Promise.all([
178+
// the listener handles the incoming stream
179+
mss.handle(pair[1], protocol, {
180+
log: logger('mss:listener')
181+
}).then(async result => {
182+
// the listener reads from the incoming stream
183+
return pipe(result.stream, async source => all(source))
184+
}),
185+
Promise.resolve().then(async () => {
186+
// the dialer just writes to the stream
187+
await pair[0].sink(async function * () {
188+
yield * input
189+
}())
190+
})
191+
])
192+
193+
expect(new Uint8ArrayList(...listenerOut).slice()).to.eql(new Uint8ArrayList(...input).slice())
194+
})
195+
196+
it('should handle and optimistically select only by reading that fails', async () => {
166197
const protocol = '/echo/1.0.0'
167198
const otherProtocol = '/echo/2.0.0'
168199
const pair = duplexPair<Uint8ArrayList | Uint8Array>()
@@ -183,7 +214,7 @@ describe('Dialer and Listener integration', () => {
183214
.to.eventually.be.rejected.with.property('code', 'ERR_UNSUPPORTED_PROTOCOL')
184215
})
185216

186-
it('should abort an unhandled lazySelect', async () => {
217+
it('should abort an unhandled optimistically select', async () => {
187218
const protocol = '/echo/1.0.0'
188219
const pair = duplexPair<Uint8ArrayList | Uint8Array>()
189220

0 commit comments

Comments
 (0)