11/* eslint-env mocha */
22'use strict'
33
4- const auto = require ( 'async/auto' )
54const multiaddr = require ( 'multiaddr' )
65const PeerId = require ( 'peer-id' )
7- const os = require ( 'os' )
8- const path = require ( 'path' )
9- const hat = require ( 'hat' )
10- const { spawnNodesWithId } = require ( '../utils/spawn' )
6+ const delay = require ( 'delay' )
117const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
128
139module . exports = ( createCommon , options ) => {
@@ -20,27 +16,14 @@ module.exports = (createCommon, options) => {
2016
2117 let ipfsA
2218 let ipfsB
23- let ipfsFactory
2419
25- before ( function ( done ) {
26- // CI takes longer to instantiate the daemon, so we need to increase the
27- // timeout for the before step
28- this . timeout ( 100 * 1000 )
29-
30- common . setup ( ( err , factory ) => {
31- expect ( err ) . to . not . exist ( )
32- ipfsFactory = factory
33-
34- spawnNodesWithId ( 2 , factory , ( err , nodes ) => {
35- expect ( err ) . to . not . exist ( )
36- ipfsA = nodes [ 0 ]
37- ipfsB = nodes [ 1 ]
38- ipfsA . swarm . connect ( ipfsB . peerId . addresses [ 0 ] , done )
39- } )
40- } )
20+ before ( async ( ) => {
21+ ipfsA = await common . setup ( )
22+ ipfsB = await common . setup ( )
23+ await ipfsA . swarm . connect ( ipfsB . peerId . addresses [ 0 ] )
4124 } )
4225
43- after ( ( done ) => common . teardown ( done ) )
26+ after ( ( ) => common . teardown ( ) )
4427
4528 it ( 'should list peers this node is connected to' , ( done ) => {
4629 ipfsA . swarm . peers ( ( err , peers ) => {
@@ -119,44 +102,20 @@ module.exports = (createCommon, options) => {
119102 }
120103 }
121104
122- function getRepoPath ( ) {
123- return path . join ( os . tmpdir ( ) , '.ipfs-' + hat ( ) )
124- }
125-
126- it ( 'should list peers only once' , ( done ) => {
105+ it ( 'should list peers only once' , async ( ) => {
127106 const config = getConfig ( [ '/ip4/127.0.0.1/tcp/0' ] )
128107
129- auto ( {
130- nodeA : ( cb ) => ipfsFactory . spawnNode ( getRepoPath ( ) , config , cb ) ,
131- nodeB : [ 'nodeA' , ( _ , cb ) => {
132- ipfsFactory . spawnNode ( getRepoPath ( ) , config , cb )
133- } ] ,
134- nodeBAddress : [ 'nodeB' , ( res , cb ) => {
135- res . nodeB . id ( ( err , info ) => {
136- if ( err ) return cb ( err )
137- cb ( null , info . addresses [ 0 ] )
138- } )
139- } ] ,
140- connectA2B : [ 'nodeA' , 'nodeBAddress' , ( res , cb ) => {
141- res . nodeA . swarm . connect ( res . nodeBAddress , cb )
142- } ] ,
143- // time for identify
144- wait : [ 'connectA2B' , ( _ , cb ) => setTimeout ( cb , 1000 ) ] ,
145- nodeAPeers : [ 'nodeA' , 'wait' , ( res , cb ) => {
146- res . nodeA . swarm . peers ( cb )
147- } ] ,
148- nodeBPeers : [ 'nodeB' , 'wait' , ( res , cb ) => {
149- res . nodeB . swarm . peers ( cb )
150- } ]
151- } , ( err , res ) => {
152- expect ( err ) . to . not . exist ( )
153- expect ( res . nodeAPeers ) . to . have . length ( 1 )
154- expect ( res . nodeBPeers ) . to . have . length ( 1 )
155- done ( )
156- } )
108+ const nodeA = await common . setup ( { } , { config } )
109+ const nodeB = await common . setup ( { } , { config } )
110+ await nodeA . swarm . connect ( nodeB . peerId . addresses [ 0 ] )
111+ await delay ( 1000 )
112+ const peersA = await nodeA . swarm . peers ( )
113+ const peersB = await nodeB . swarm . peers ( )
114+ expect ( peersA ) . to . have . length ( 1 )
115+ expect ( peersB ) . to . have . length ( 1 )
157116 } )
158117
159- it ( 'should list peers only once even if they have multiple addresses' , ( done ) => {
118+ it ( 'should list peers only once even if they have multiple addresses' , async ( ) => {
160119 // TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
161120 const configA = getConfig ( [
162121 '/ip4/127.0.0.1/tcp/16543' ,
@@ -166,35 +125,14 @@ module.exports = (createCommon, options) => {
166125 '/ip4/127.0.0.1/tcp/26545' ,
167126 '/ip4/127.0.0.1/tcp/26546'
168127 ] )
169-
170- auto ( {
171- nodeA : ( cb ) => ipfsFactory . spawnNode ( getRepoPath ( ) , configA , cb ) ,
172- nodeB : [ 'nodeA' , ( _ , cb ) => {
173- ipfsFactory . spawnNode ( getRepoPath ( ) , configB , cb )
174- } ] ,
175- nodeBAddress : [ 'nodeB' , ( res , cb ) => {
176- res . nodeB . id ( ( err , info ) => {
177- if ( err ) return cb ( err )
178- cb ( null , info . addresses [ 0 ] )
179- } )
180- } ] ,
181- connectA2B : [ 'nodeA' , 'nodeBAddress' , ( res , cb ) => {
182- res . nodeA . swarm . connect ( res . nodeBAddress , cb )
183- } ] ,
184- // time for identify
185- wait : [ 'connectA2B' , ( _ , cb ) => setTimeout ( cb , 1000 ) ] ,
186- nodeAPeers : [ 'nodeA' , 'wait' , ( res , cb ) => {
187- res . nodeA . swarm . peers ( cb )
188- } ] ,
189- nodeBPeers : [ 'nodeB' , 'wait' , ( res , cb ) => {
190- res . nodeB . swarm . peers ( cb )
191- } ]
192- } , ( err , res ) => {
193- expect ( err ) . to . not . exist ( )
194- expect ( res . nodeAPeers ) . to . have . length ( 1 )
195- expect ( res . nodeBPeers ) . to . have . length ( 1 )
196- done ( )
197- } )
128+ const nodeA = await common . setup ( { } , { configA } )
129+ const nodeB = await common . setup ( { } , { configB } )
130+ await nodeA . swarm . connect ( nodeB . peerId . addresses [ 0 ] )
131+ await delay ( 1000 )
132+ const peersA = await nodeA . swarm . peers ( )
133+ const peersB = await nodeB . swarm . peers ( )
134+ expect ( peersA ) . to . have . length ( 1 )
135+ expect ( peersB ) . to . have . length ( 1 )
198136 } )
199137 } )
200138}
0 commit comments