@@ -45,8 +45,6 @@ module.exports = (common) => {
4545 } )
4646
4747 it ( 'existing subscriptions are sent upon peer connection' , async function ( ) {
48- this . timeout ( 10e3 )
49-
5048 await Promise . all ( [
5149 psA . _libp2p . dial ( psB . peerId ) ,
5250 new Promise ( ( resolve ) => psA . once ( 'pubsub:subscription-change' , resolve ) ) ,
@@ -206,7 +204,6 @@ module.exports = (common) => {
206204 } )
207205
208206 it ( 'should receive pubsub messages after a node restart' , async function ( ) {
209- this . timeout ( 10e3 )
210207 const topic = 'test-topic'
211208 const data = uint8ArrayFromString ( 'hey!' )
212209 const psAid = psA . peerId . toB58String ( )
@@ -261,15 +258,40 @@ module.exports = (common) => {
261258
262259 it ( 'should handle quick reconnects with a delayed disconnect' , async ( ) => {
263260 // Subscribe on both
264- const handlerSpy = sinon . spy ( )
261+ let aReceivedFirstMessageFromB = false
262+ let aReceivedSecondMessageFromB = false
263+ let bReceivedFirstMessageFromA = false
264+ let bReceivedSecondMessageFromA = false
265+
266+ const handlerSpyA = ( message ) => {
267+ const data = uint8ArrayToString ( message . data )
268+
269+ if ( data === 'message-from-b-1' ) {
270+ aReceivedFirstMessageFromB = true
271+ }
272+
273+ if ( data === 'message-from-b-2' ) {
274+ aReceivedSecondMessageFromB = true
275+ }
276+ }
277+ const handlerSpyB = ( message ) => {
278+ const data = uint8ArrayToString ( message . data )
279+
280+ if ( data === 'message-from-a-1' ) {
281+ bReceivedFirstMessageFromA = true
282+ }
283+
284+ if ( data === 'message-from-a-2' ) {
285+ bReceivedSecondMessageFromA = true
286+ }
287+ }
288+
265289 const topic = 'reconnect-channel'
266290
267- psA . on ( topic , handlerSpy )
268- psB . on ( topic , handlerSpy )
269- await Promise . all ( [
270- psA . subscribe ( topic ) ,
271- psB . subscribe ( topic )
272- ] )
291+ psA . on ( topic , handlerSpyA )
292+ psB . on ( topic , handlerSpyB )
293+ psA . subscribe ( topic )
294+ psB . subscribe ( topic )
273295
274296 // Create two connections to the remote peer
275297 const originalConnection = await psA . _libp2p . dialer . connectToPeer ( psB . peerId )
@@ -284,10 +306,11 @@ module.exports = (common) => {
284306 } )
285307
286308 // Verify messages go both ways
287- psA . publish ( topic , uint8ArrayFromString ( 'message1' ) )
288- psB . publish ( topic , uint8ArrayFromString ( 'message2' ) )
289- await pWaitFor ( ( ) => handlerSpy . callCount >= 2 )
290- expect ( handlerSpy . args . map ( ( [ message ] ) => uint8ArrayToString ( message . data ) ) ) . to . include . members ( [ 'message1' , 'message2' ] )
309+ psA . publish ( topic , uint8ArrayFromString ( 'message-from-a-1' ) )
310+ psB . publish ( topic , uint8ArrayFromString ( 'message-from-b-1' ) )
311+ await pWaitFor ( ( ) => {
312+ return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
313+ } )
291314
292315 // Disconnect the first connection (this acts as a delayed reconnect)
293316 const psAConnUpdateSpy = sinon . spy ( psA . _libp2p . connectionManager . connections , 'set' )
@@ -296,11 +319,11 @@ module.exports = (common) => {
296319 await pWaitFor ( ( ) => psAConnUpdateSpy . callCount === 1 )
297320
298321 // Verify messages go both ways after the disconnect
299- handlerSpy . resetHistory ( )
300- psA . publish ( topic , uint8ArrayFromString ( 'message3 ' ) )
301- psB . publish ( topic , uint8ArrayFromString ( 'message4' ) )
302- await pWaitFor ( ( ) => handlerSpy . callCount >= 2 )
303- expect ( handlerSpy . args . map ( ( [ message ] ) => uint8ArrayToString ( message . data ) ) ) . to . include . members ( [ 'message3' , 'message4' ] )
322+ psA . publish ( topic , uint8ArrayFromString ( 'message-from-a-2' ) )
323+ psB . publish ( topic , uint8ArrayFromString ( 'message-from-b-2 ' ) )
324+ await pWaitFor ( ( ) => {
325+ return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
326+ } )
304327 } )
305328 } )
306329 } )
0 commit comments