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