@@ -5,75 +5,63 @@ const { expect, assert } = require('aegir/utils/chai')
5
5
const lp = require ( 'it-length-prefixed' )
6
6
const pipe = require ( 'it-pipe' )
7
7
const pDefer = require ( 'p-defer' )
8
- const pWaitFor = require ( 'p-wait-for' )
9
8
const createLibp2pNode = require ( '../utils/create-libp2p-node' )
10
9
const makeBlock = require ( '../utils/make-block' )
11
10
const Network = require ( '../../src/network' )
12
11
const Message = require ( '../../src/types/message' )
13
12
13
+ function createBitswapMock ( ) {
14
+ return {
15
+ _receiveMessage : async ( ) => { } ,
16
+ _receiveError : async ( ) => { } ,
17
+ _onPeerConnected : async ( ) => { } ,
18
+ _onPeerDisconnected : async ( ) => { }
19
+ }
20
+ }
21
+
14
22
describe ( 'network' , ( ) => {
15
23
let p2pA
16
24
let networkA
25
+ let bitswapMockA
17
26
18
27
let p2pB
19
28
let networkB
29
+ let bitswapMockB
20
30
21
31
let p2pC
22
32
let networkC
33
+ let bitswapMockC
23
34
24
35
let blocks
25
36
26
- before ( async ( ) => {
37
+ beforeEach ( async ( ) => {
27
38
[ p2pA , p2pB , p2pC ] = await Promise . all ( [
28
39
createLibp2pNode ( ) ,
29
40
createLibp2pNode ( ) ,
30
41
createLibp2pNode ( )
31
42
] )
32
43
blocks = await makeBlock ( 2 )
33
- } )
34
44
35
- after ( ( ) => {
36
- p2pA . stop ( )
37
- p2pB . stop ( )
38
- p2pC . stop ( )
39
- } )
40
-
41
- const bitswapMockA = {
42
- _receiveMessage : async ( ) => { } ,
43
- _receiveError : async ( ) => { } ,
44
- _onPeerConnected : async ( ) => { } ,
45
- _onPeerDisconnected : async ( ) => { }
46
- }
45
+ bitswapMockA = createBitswapMock ( )
46
+ bitswapMockB = createBitswapMock ( )
47
+ bitswapMockC = createBitswapMock ( )
47
48
48
- const bitswapMockB = {
49
- _receiveMessage : async ( ) => { } ,
50
- _receiveError : async ( ) => { } ,
51
- _onPeerConnected : async ( ) => { } ,
52
- _onPeerDisconnected : async ( ) => { }
53
- }
54
-
55
- const bitswapMockC = {
56
- _receiveMessage : async ( ) => { } ,
57
- _receiveError : async ( ) => { } ,
58
- _onPeerConnected : async ( ) => { } ,
59
- _onPeerDisconnected : async ( ) => { }
60
- }
61
-
62
- it ( 'instantiate the network obj' , ( ) => {
63
49
networkA = new Network ( p2pA , bitswapMockA )
64
50
networkB = new Network ( p2pB , bitswapMockB )
65
51
// only bitswap100
66
52
networkC = new Network ( p2pC , bitswapMockC , { b100Only : true } )
67
53
68
- expect ( networkA ) . to . exist ( )
69
- expect ( networkB ) . to . exist ( )
70
- expect ( networkC ) . to . exist ( )
71
-
72
54
networkA . start ( )
73
55
networkB . start ( )
74
56
networkC . start ( )
75
57
} )
76
58
59
+ afterEach ( ( ) => {
60
+ p2pA . stop ( )
61
+ p2pB . stop ( )
62
+ p2pC . stop ( )
63
+ } )
64
+
77
65
it ( 'connectTo fail' , async ( ) => {
78
66
try {
79
67
await networkA . connectTo ( p2pB . peerId )
@@ -84,31 +72,70 @@ describe('network', () => {
84
72
} )
85
73
86
74
it ( 'onPeerConnected success' , async ( ) => {
87
- var counter = 0
75
+ const p2pAConnected = pDefer ( )
76
+ const p2pBConnected = pDefer ( )
88
77
89
78
bitswapMockA . _onPeerConnected = ( peerId ) => {
90
79
expect ( peerId . toB58String ( ) ) . to . equal ( p2pB . peerId . toB58String ( ) )
91
- counter ++
80
+ p2pBConnected . resolve ( )
92
81
}
93
82
94
83
bitswapMockB . _onPeerConnected = ( peerId ) => {
95
84
expect ( peerId . toB58String ( ) ) . to . equal ( p2pA . peerId . toB58String ( ) )
96
- counter ++
85
+ p2pAConnected . resolve ( )
97
86
}
98
87
99
88
const ma = `${ p2pB . multiaddrs [ 0 ] } /p2p/${ p2pB . peerId . toB58String ( ) } `
100
89
await p2pA . dial ( ma )
101
90
102
- await pWaitFor ( ( ) => counter >= 2 )
103
- bitswapMockA . _onPeerConnected = ( ) => { }
104
- bitswapMockB . _onPeerConnected = ( ) => { }
91
+ await Promise . all ( [
92
+ p2pAConnected ,
93
+ p2pBConnected
94
+ ] )
105
95
} )
106
96
107
97
it ( 'connectTo success' , async ( ) => {
108
98
const ma = `${ p2pB . multiaddrs [ 0 ] } /p2p/${ p2pB . peerId . toB58String ( ) } `
109
99
await networkA . connectTo ( ma )
110
100
} )
111
101
102
+ it ( 'sets up peer handlers for previously connected peers' , async ( ) => {
103
+ let p2pAConnected = pDefer ( )
104
+ let p2pBConnected = pDefer ( )
105
+
106
+ bitswapMockA . _onPeerConnected = ( peerId ) => {
107
+ expect ( peerId . toB58String ( ) ) . to . equal ( p2pB . peerId . toB58String ( ) )
108
+ p2pBConnected . resolve ( )
109
+ }
110
+
111
+ bitswapMockB . _onPeerConnected = ( peerId ) => {
112
+ expect ( peerId . toB58String ( ) ) . to . equal ( p2pA . peerId . toB58String ( ) )
113
+ p2pAConnected . resolve ( )
114
+ }
115
+
116
+ const ma = `${ p2pB . multiaddrs [ 0 ] } /p2p/${ p2pB . peerId . toB58String ( ) } `
117
+ await p2pA . dial ( ma )
118
+
119
+ await Promise . all ( [
120
+ p2pAConnected ,
121
+ p2pBConnected
122
+ ] )
123
+
124
+ networkA . stop ( )
125
+ networkB . stop ( )
126
+
127
+ p2pAConnected = pDefer ( )
128
+ p2pBConnected = pDefer ( )
129
+
130
+ networkA . start ( )
131
+ networkB . start ( )
132
+
133
+ await Promise . all ( [
134
+ p2pAConnected ,
135
+ p2pBConnected
136
+ ] )
137
+ } )
138
+
112
139
const versions = [ {
113
140
num : '1.0.0' , serialize : ( msg ) => msg . serializeToBitswap100 ( )
114
141
} , {
@@ -159,6 +186,10 @@ describe('network', () => {
159
186
msg . addBlock ( b1 )
160
187
msg . addBlock ( b2 )
161
188
189
+ // In a real network scenario, peers will be discovered and their addresses
190
+ // will be added to the addressBook before bitswap kicks in
191
+ p2pA . peerStore . addressBook . set ( p2pB . peerId , p2pB . multiaddrs )
192
+
162
193
bitswapMockB . _receiveMessage = async ( peerId , msgReceived ) => { // eslint-disable-line require-await
163
194
expect ( msg ) . to . eql ( msgReceived )
164
195
bitswapMockB . _receiveMessage = async ( ) => { }
@@ -189,6 +220,10 @@ describe('network', () => {
189
220
msg . addBlock ( b1 )
190
221
msg . addBlock ( b2 )
191
222
223
+ // In a real network scenario, peers will be discovered and their addresses
224
+ // will be added to the addressBook before bitswap kicks in
225
+ p2pA . peerStore . addressBook . set ( p2pC . peerId , p2pC . multiaddrs )
226
+
192
227
bitswapMockC . _receiveMessage = async ( peerId , msgReceived ) => { // eslint-disable-line require-await
193
228
expect ( msg ) . to . eql ( msgReceived )
194
229
bitswapMockC . _receiveMessage = async ( ) => { }
0 commit comments