@@ -5,19 +5,13 @@ const chai = require('chai')
5
5
const { expect } = chai
6
6
chai . use ( require ( 'dirty-chai' ) )
7
7
const IPFSFactory = require ( 'ipfsd-ctl' )
8
- const async = require ( 'async' )
9
8
const PeerID = require ( 'peer-id' )
10
9
11
10
const DelegatedPeerRouting = require ( '../src' )
12
11
const factory = IPFSFactory . create ( { type : 'go' } )
13
12
14
- function spawnNode ( boostrap , callback ) {
15
- if ( typeof boostrap === 'function' ) {
16
- callback = boostrap
17
- boostrap = [ ]
18
- }
19
-
20
- factory . spawn ( {
13
+ async function spawnNode ( boostrap = [ ] ) {
14
+ const node = await factory . spawn ( {
21
15
// Lock down the nodes so testing can be deterministic
22
16
config : {
23
17
Bootstrap : boostrap ,
@@ -27,15 +21,13 @@ function spawnNode (boostrap, callback) {
27
21
}
28
22
}
29
23
}
30
- } , ( err , node ) => {
31
- if ( err ) return callback ( err )
32
-
33
- node . api . id ( ( err , id ) => {
34
- if ( err ) return callback ( err )
35
-
36
- callback ( null , node , id )
37
- } )
38
24
} )
25
+ const id = await node . api . id ( )
26
+
27
+ return {
28
+ node,
29
+ id
30
+ }
39
31
}
40
32
41
33
describe ( 'DelegatedPeerRouting' , function ( ) {
@@ -47,37 +39,28 @@ describe('DelegatedPeerRouting', function () {
47
39
let bootstrapNode
48
40
let bootstrapId
49
41
50
- before ( ( done ) => {
51
- async . waterfall ( [
52
- // Spawn a "Boostrap" node that doesnt connect to anything
53
- ( cb ) => spawnNode ( cb ) ,
54
- ( ipfsd , id , cb ) => {
55
- bootstrapNode = ipfsd
56
- bootstrapId = id
57
- cb ( )
58
- } ,
59
- // Spawn our local node and bootstrap the bootstrapper node
60
- ( cb ) => spawnNode ( bootstrapId . addresses , cb ) ,
61
- ( ipfsd , id , cb ) => {
62
- nodeToFind = ipfsd
63
- peerIdToFind = id
64
- cb ( )
65
- } ,
66
- // Spawn the delegate node and bootstrap the bootstrapper node
67
- ( cb ) => spawnNode ( bootstrapId . addresses , cb ) ,
68
- ( ipfsd , id , cb ) => {
69
- delegatedNode = ipfsd
70
- cb ( )
71
- }
72
- ] , done )
42
+ before ( async ( ) => {
43
+ // Spawn a "Boostrap" node that doesnt connect to anything
44
+ const bootstrap = await spawnNode ( )
45
+ bootstrapNode = bootstrap . node
46
+ bootstrapId = bootstrap . id
47
+
48
+ // Spawn our local node and bootstrap the bootstrapper node
49
+ const local = await spawnNode ( bootstrapId . addresses )
50
+ nodeToFind = local . node
51
+ peerIdToFind = local . id
52
+
53
+ // Spawn the delegate node and bootstrap the bootstrapper node
54
+ const delegate = await spawnNode ( bootstrapId . addresses )
55
+ delegatedNode = delegate . node
73
56
} )
74
57
75
- after ( ( done ) => {
76
- async . parallel ( [
77
- ( cb ) => nodeToFind . stop ( cb ) ,
78
- ( cb ) => delegatedNode . stop ( cb ) ,
79
- ( cb ) => bootstrapNode . stop ( cb )
80
- ] , done )
58
+ after ( ( ) => {
59
+ return Promise . all ( [
60
+ nodeToFind . stop ( ) ,
61
+ delegatedNode . stop ( ) ,
62
+ bootstrapNode . stop ( )
63
+ ] )
81
64
} )
82
65
83
66
describe ( 'create' , ( ) => {
@@ -119,55 +102,46 @@ describe('DelegatedPeerRouting', function () {
119
102
} )
120
103
121
104
describe ( 'findPeers' , ( ) => {
122
- it ( 'should be able to find peers via the delegate with a peer id string' , ( done ) => {
105
+ it ( 'should be able to find peers via the delegate with a peer id string' , async ( ) => {
123
106
const opts = delegatedNode . apiAddr . toOptions ( )
124
107
const router = new DelegatedPeerRouting ( {
125
108
protocol : 'http' ,
126
109
port : opts . port ,
127
110
host : opts . host
128
111
} )
129
112
130
- router . findPeer ( peerIdToFind . id , ( err , peer ) => {
131
- expect ( err ) . to . not . exist ( )
132
- expect ( peer ) . to . exist ( )
133
- expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
134
- done ( )
135
- } )
113
+ const peer = await router . findPeer ( peerIdToFind . id )
114
+ expect ( peer ) . to . exist ( )
115
+ expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
136
116
} )
137
117
138
- it ( 'should be able to find peers via the delegate with a peerid' , ( done ) => {
118
+ it ( 'should be able to find peers via the delegate with a peerid' , async ( ) => {
139
119
const opts = delegatedNode . apiAddr . toOptions ( )
140
120
const router = new DelegatedPeerRouting ( {
141
121
protocol : 'http' ,
142
122
port : opts . port ,
143
123
host : opts . host
144
124
} )
145
125
146
- router . findPeer ( PeerID . createFromB58String ( peerIdToFind . id ) , ( err , peer ) => {
147
- expect ( err ) . to . not . exist ( )
148
- expect ( peer ) . to . exist ( )
149
- expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
150
- done ( )
151
- } )
126
+ const peer = await router . findPeer ( PeerID . createFromB58String ( peerIdToFind . id ) )
127
+ expect ( peer ) . to . exist ( )
128
+ expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
152
129
} )
153
130
154
- it ( 'should be able to specify a maxTimeout' , ( done ) => {
131
+ it ( 'should be able to specify a maxTimeout' , async ( ) => {
155
132
const opts = delegatedNode . apiAddr . toOptions ( )
156
133
const router = new DelegatedPeerRouting ( {
157
134
protocol : 'http' ,
158
135
port : opts . port ,
159
136
host : opts . host
160
137
} )
161
138
162
- router . findPeer ( PeerID . createFromB58String ( peerIdToFind . id ) , { maxTimeout : 2000 } , ( err , peer ) => {
163
- expect ( err ) . to . not . exist ( )
164
- expect ( peer ) . to . exist ( )
165
- expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
166
- done ( )
167
- } )
139
+ const peer = await router . findPeer ( PeerID . createFromB58String ( peerIdToFind . id ) , { maxTimeout : 2000 } )
140
+ expect ( peer ) . to . exist ( )
141
+ expect ( peer . id . toB58String ( ) ) . to . eql ( peerIdToFind . id )
168
142
} )
169
143
170
- it ( 'should not be able to find peers not on the network' , ( done ) => {
144
+ it ( 'should not be able to find peers not on the network' , async ( ) => {
171
145
const opts = delegatedNode . apiAddr . toOptions ( )
172
146
const router = new DelegatedPeerRouting ( {
173
147
protocol : 'http' ,
@@ -177,11 +151,8 @@ describe('DelegatedPeerRouting', function () {
177
151
178
152
// This is one of the default Bootstrap nodes, but we're not connected to it
179
153
// so we'll test with it.
180
- router . findPeer ( 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' , ( err , peer ) => {
181
- expect ( err ) . to . not . exist ( )
182
- expect ( peer ) . to . not . exist ( )
183
- done ( )
184
- } )
154
+ const peer = await router . findPeer ( 'QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' )
155
+ expect ( peer ) . to . not . exist ( )
185
156
} )
186
157
} )
187
158
} )
0 commit comments