@@ -189,14 +189,15 @@ describe('pubsub base protocol', () => {
189
189
expect ( pubsubB . peers . size ) . to . be . eql ( 1 )
190
190
} )
191
191
192
- it ( 'should not create a new stream if onConnect is called twice ' , async ( ) => {
192
+ it ( 'should use the latest connection if onConnect is called more than once ' , async ( ) => {
193
193
const onConnectA = registrarRecordA [ protocol ] . onConnect
194
194
const handlerB = registrarRecordB [ protocol ] . handler
195
195
196
196
// Notice peers of connection
197
197
const [ c0 , c1 ] = ConnectionPair ( )
198
+ const [ c2 ] = ConnectionPair ( )
198
199
199
- const spyNewStream = sinon . spy ( c0 , 'newStream' )
200
+ sinon . spy ( c0 , 'newStream' )
200
201
201
202
await onConnectA ( peerIdB , c0 )
202
203
await handlerB ( {
@@ -206,10 +207,25 @@ describe('pubsub base protocol', () => {
206
207
remotePeer : peerIdA
207
208
}
208
209
} )
209
- expect ( spyNewStream ) . to . have . property ( 'callCount' , 1 )
210
+ expect ( c0 . newStream ) . to . have . property ( 'callCount' , 1 )
210
211
211
- await onConnectA ( peerIdB , c0 )
212
- expect ( spyNewStream ) . to . have . property ( 'callCount' , 1 )
212
+ sinon . spy ( pubsubA , '_removePeer' )
213
+
214
+ sinon . spy ( c2 , 'newStream' )
215
+
216
+ await onConnectA ( peerIdB , c2 )
217
+ expect ( c2 . newStream ) . to . have . property ( 'callCount' , 1 )
218
+ expect ( pubsubA . _removePeer ) . to . have . property ( 'callCount' , 0 )
219
+
220
+ // Verify the first stream was closed
221
+ const { stream : firstStream } = await c0 . newStream . returnValues [ 0 ]
222
+ try {
223
+ await firstStream . sink ( [ 'test' ] )
224
+ } catch ( err ) {
225
+ expect ( err ) . to . exist ( )
226
+ return
227
+ }
228
+ expect . fail ( 'original stream should have ended' )
213
229
} )
214
230
215
231
it ( 'should handle newStream errors in onConnect' , async ( ) => {
0 commit comments