1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
- const auto = require ( 'async/auto' )
5
4
const multiaddr = require ( 'multiaddr' )
6
5
const 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' )
11
7
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
12
8
13
9
module . exports = ( createCommon , options ) => {
@@ -20,27 +16,14 @@ module.exports = (createCommon, options) => {
20
16
21
17
let ipfsA
22
18
let ipfsB
23
- let ipfsFactory
24
19
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 ] )
41
24
} )
42
25
43
- after ( ( done ) => common . teardown ( done ) )
26
+ after ( ( ) => common . teardown ( ) )
44
27
45
28
it ( 'should list peers this node is connected to' , ( done ) => {
46
29
ipfsA . swarm . peers ( ( err , peers ) => {
@@ -119,44 +102,20 @@ module.exports = (createCommon, options) => {
119
102
}
120
103
}
121
104
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 ( ) => {
127
106
const config = getConfig ( [ '/ip4/127.0.0.1/tcp/0' ] )
128
107
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 )
157
116
} )
158
117
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 ( ) => {
160
119
// TODO: Change to port 0, needs: https://github.com/ipfs/interface-ipfs-core/issues/152
161
120
const configA = getConfig ( [
162
121
'/ip4/127.0.0.1/tcp/16543' ,
@@ -166,35 +125,14 @@ module.exports = (createCommon, options) => {
166
125
'/ip4/127.0.0.1/tcp/26545' ,
167
126
'/ip4/127.0.0.1/tcp/26546'
168
127
] )
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 )
198
136
} )
199
137
} )
200
138
}
0 commit comments