File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,10 @@ class PubsubBaseProtocol extends EventEmitter {
196
196
protocols : this . multicodecs
197
197
} ) )
198
198
199
+ if ( peer . isConnected ) {
200
+ return
201
+ }
202
+
199
203
try {
200
204
const { stream } = await conn . newStream ( this . multicodecs )
201
205
peer . attachConnection ( stream )
Original file line number Diff line number Diff line change @@ -189,6 +189,29 @@ 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 ( ) => {
193
+ const onConnectA = registrarRecordA [ protocol ] . onConnect
194
+ const handlerB = registrarRecordB [ protocol ] . handler
195
+
196
+ // Notice peers of connection
197
+ const [ c0 , c1 ] = ConnectionPair ( )
198
+
199
+ const spyNewStream = sinon . spy ( c0 , 'newStream' )
200
+
201
+ await onConnectA ( peerIdB , c0 )
202
+ await handlerB ( {
203
+ protocol,
204
+ stream : c1 . stream ,
205
+ connection : {
206
+ remotePeer : peerIdA
207
+ }
208
+ } )
209
+ expect ( spyNewStream ) . to . have . property ( 'callCount' , 1 )
210
+
211
+ await onConnectA ( peerIdB , c0 )
212
+ expect ( spyNewStream ) . to . have . property ( 'callCount' , 1 )
213
+ } )
214
+
192
215
it ( 'should handle newStream errors in onConnect' , async ( ) => {
193
216
const onConnectA = registrarRecordA [ protocol ] . onConnect
194
217
const handlerB = registrarRecordB [ protocol ] . handler
You can’t perform that action at this time.
0 commit comments