22'use strict'
33
44const parallel = require ( 'async/parallel' )
5- const series = require ( 'async/series' )
65const { spawnNodesWithId } = require ( '../utils/spawn' )
76const { waitForPeers, getTopic } = require ( './utils' )
87const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
98const { connect } = require ( '../utils/swarm' )
9+ const delay = require ( '../utils/delay' )
1010
1111module . exports = ( createCommon , options ) => {
1212 const describe = getDescribe ( options )
@@ -19,6 +19,7 @@ module.exports = (createCommon, options) => {
1919 let ipfs1
2020 let ipfs2
2121 let ipfs3
22+ let subscribedTopics = [ ]
2223
2324 before ( function ( done ) {
2425 // CI takes longer to instantiate the daemon, so we need to increase the
@@ -40,6 +41,16 @@ module.exports = (createCommon, options) => {
4041 } )
4142 } )
4243
44+ afterEach ( async ( ) => {
45+ const nodes = [ ipfs1 , ipfs2 , ipfs3 ]
46+ for ( let i = 0 ; i < subscribedTopics . length ; i ++ ) {
47+ const topic = subscribedTopics [ i ]
48+ await Promise . all ( nodes . map ( ipfs => ipfs . pubsub . unsubscribe ( topic ) ) )
49+ }
50+ subscribedTopics = [ ]
51+ await delay ( 100 )
52+ } )
53+
4354 after ( ( done ) => common . teardown ( done ) )
4455
4556 before ( ( done ) => {
@@ -52,94 +63,64 @@ module.exports = (createCommon, options) => {
5263 ] , done )
5364 } )
5465
55- it ( 'should not error when not subscribed to a topic' , ( done ) => {
66+ it ( 'should not error when not subscribed to a topic' , async ( ) => {
5667 const topic = getTopic ( )
57- ipfs1 . pubsub . peers ( topic , ( err , peers ) => {
58- expect ( err ) . to . not . exist ( )
59- // Should be empty() but as mentioned below go-ipfs returns more than it should
60- // expect(peers).to.be.empty()
61-
62- done ( )
63- } )
68+ const peers = await ipfs1 . pubsub . peers ( topic )
69+ expect ( peers ) . to . exist ( )
70+ // Should be empty() but as mentioned below go-ipfs returns more than it should
71+ // expect(peers).to.be.empty()
6472 } )
6573
66- it ( 'should not return extra peers' , ( done ) => {
74+ it ( 'should not return extra peers' , async ( ) => {
6775 // Currently go-ipfs returns peers that have not been
6876 // subscribed to the topic. Enable when go-ipfs has been fixed
69- const sub1 = ( msg ) => { }
70- const sub2 = ( msg ) => { }
71- const sub3 = ( msg ) => { }
77+ const sub1 = ( ) => { }
78+ const sub2 = ( ) => { }
79+ const sub3 = ( ) => { }
7280
7381 const topic = getTopic ( )
7482 const topicOther = topic + 'different topic'
7583
76- series ( [
77- ( cb ) => ipfs1 . pubsub . subscribe ( topic , sub1 , cb ) ,
78- ( cb ) => ipfs2 . pubsub . subscribe ( topicOther , sub2 , cb ) ,
79- ( cb ) => ipfs3 . pubsub . subscribe ( topicOther , sub3 , cb )
80- ] , ( err ) => {
81- expect ( err ) . to . not . exist ( )
82-
83- ipfs1 . pubsub . peers ( topic , ( err , peers ) => {
84- expect ( err ) . to . not . exist ( )
85- expect ( peers ) . to . be . empty ( )
86-
87- parallel ( [
88- ( cb ) => ipfs1 . pubsub . unsubscribe ( topic , sub1 , cb ) ,
89- ( cb ) => ipfs2 . pubsub . unsubscribe ( topicOther , sub2 , cb ) ,
90- ( cb ) => ipfs3 . pubsub . unsubscribe ( topicOther , sub3 , cb )
91- ] , done )
92- } )
93- } )
84+ subscribedTopics = [ topic , topicOther ]
85+
86+ await ipfs1 . pubsub . subscribe ( topic , sub1 )
87+ await ipfs2 . pubsub . subscribe ( topicOther , sub2 )
88+ await ipfs3 . pubsub . subscribe ( topicOther , sub3 )
89+
90+ const peers = await ipfs1 . pubsub . peers ( topic )
91+ expect ( peers ) . to . be . empty ( )
9492 } )
9593
96- it ( 'should return peers for a topic - one peer' , ( done ) => {
94+ it ( 'should return peers for a topic - one peer' , async ( ) => {
9795 // Currently go-ipfs returns peers that have not been
9896 // subscribed to the topic. Enable when go-ipfs has been fixed
99- const sub1 = ( msg ) => { }
100- const sub2 = ( msg ) => { }
101- const sub3 = ( msg ) => { }
97+ const sub1 = ( ) => { }
98+ const sub2 = ( ) => { }
99+ const sub3 = ( ) => { }
102100 const topic = getTopic ( )
103101
104- series ( [
105- ( cb ) => ipfs1 . pubsub . subscribe ( topic , sub1 , cb ) ,
106- ( cb ) => ipfs2 . pubsub . subscribe ( topic , sub2 , cb ) ,
107- ( cb ) => ipfs3 . pubsub . subscribe ( topic , sub3 , cb ) ,
108- ( cb ) => waitForPeers ( ipfs1 , topic , [ ipfs2 . peerId . id ] , 30000 , cb )
109- ] , ( err ) => {
110- expect ( err ) . to . not . exist ( )
111-
112- parallel ( [
113- ( cb ) => ipfs1 . pubsub . unsubscribe ( topic , sub1 , cb ) ,
114- ( cb ) => ipfs2 . pubsub . unsubscribe ( topic , sub2 , cb ) ,
115- ( cb ) => ipfs3 . pubsub . unsubscribe ( topic , sub3 , cb )
116- ] , done )
117- } )
102+ subscribedTopics = [ topic ]
103+
104+ await ipfs1 . pubsub . subscribe ( topic , sub1 )
105+ await ipfs2 . pubsub . subscribe ( topic , sub2 )
106+ await ipfs3 . pubsub . subscribe ( topic , sub3 )
107+
108+ await waitForPeers ( ipfs1 , topic , [ ipfs2 . peerId . id ] , 30000 )
118109 } )
119110
120- it ( 'should return peers for a topic - multiple peers' , ( done ) => {
121- const sub1 = ( msg ) => { }
122- const sub2 = ( msg ) => { }
123- const sub3 = ( msg ) => { }
111+ it ( 'should return peers for a topic - multiple peers' , async ( ) => {
112+ const sub1 = ( ) => { }
113+ const sub2 = ( ) => { }
114+ const sub3 = ( ) => { }
124115 const topic = getTopic ( )
125116
126- series ( [
127- ( cb ) => ipfs1 . pubsub . subscribe ( topic , sub1 , cb ) ,
128- ( cb ) => ipfs2 . pubsub . subscribe ( topic , sub2 , cb ) ,
129- ( cb ) => ipfs3 . pubsub . subscribe ( topic , sub3 , cb ) ,
130- ( cb ) => waitForPeers ( ipfs1 , topic , [
131- ipfs2 . peerId . id ,
132- ipfs3 . peerId . id
133- ] , 30000 , cb )
134- ] , ( err ) => {
135- expect ( err ) . to . not . exist ( )
136-
137- parallel ( [
138- ( cb ) => ipfs1 . pubsub . unsubscribe ( topic , sub1 , cb ) ,
139- ( cb ) => ipfs2 . pubsub . unsubscribe ( topic , sub2 , cb ) ,
140- ( cb ) => ipfs3 . pubsub . unsubscribe ( topic , sub3 , cb )
141- ] , done )
142- } )
117+ subscribedTopics = [ topic ]
118+
119+ await ipfs1 . pubsub . subscribe ( topic , sub1 )
120+ await ipfs2 . pubsub . subscribe ( topic , sub2 )
121+ await ipfs3 . pubsub . subscribe ( topic , sub3 )
122+
123+ await waitForPeers ( ipfs1 , topic , [ ipfs2 . peerId . id , ipfs3 . peerId . id ] , 30000 )
143124 } )
144125 } )
145126}
0 commit comments