@@ -13,6 +13,7 @@ const TCP = require('libp2p-tcp')
13
13
const secio = require ( 'libp2p-secio' )
14
14
const multiplex = require ( 'pull-mplex' )
15
15
const pull = require ( 'pull-stream' )
16
+ const identify = require ( 'libp2p-identify' )
16
17
17
18
const utils = require ( './utils' )
18
19
const createInfos = utils . createInfos
@@ -24,6 +25,7 @@ describe('dialFSM', () => {
24
25
let switchC
25
26
let peerAId
26
27
let peerBId
28
+ let protocol
27
29
28
30
before ( ( done ) => createInfos ( 3 , ( err , infos ) => {
29
31
expect ( err ) . to . not . exist ( )
@@ -76,10 +78,18 @@ describe('dialFSM', () => {
76
78
] , done )
77
79
} )
78
80
81
+ afterEach ( ( ) => {
82
+ switchA . unhandle ( protocol )
83
+ switchB . unhandle ( protocol )
84
+ switchC . unhandle ( protocol )
85
+ protocol = null
86
+ } )
87
+
79
88
it ( 'should emit `error:connection_attempt_failed` when a transport fails to dial' , ( done ) => {
80
- switchC . handle ( '/warn/1.0.0' , ( ) => { } )
89
+ protocol = '/warn/1.0.0'
90
+ switchC . handle ( protocol , ( ) => { } )
81
91
82
- switchA . dialFSM ( switchC . _peerInfo , '/warn/1.0.0' , ( err , connFSM ) => {
92
+ switchA . dialFSM ( switchC . _peerInfo , protocol , ( err , connFSM ) => {
83
93
expect ( err ) . to . not . exist ( )
84
94
connFSM . once ( 'error:connection_attempt_failed' , ( errors ) => {
85
95
expect ( errors ) . to . be . an ( 'array' )
@@ -90,9 +100,10 @@ describe('dialFSM', () => {
90
100
} )
91
101
92
102
it ( 'should emit an `error` event when a it cannot dial a peer' , ( done ) => {
93
- switchC . handle ( '/error/1.0.0' , ( ) => { } )
103
+ protocol = '/error/1.0.0'
104
+ switchC . handle ( protocol , ( ) => { } )
94
105
95
- switchA . dialFSM ( switchC . _peerInfo , '/error/1.0.0' , ( err , connFSM ) => {
106
+ switchA . dialFSM ( switchC . _peerInfo , protocol , ( err , connFSM ) => {
96
107
expect ( err ) . to . not . exist ( )
97
108
connFSM . once ( 'error' , ( err ) => {
98
109
expect ( err ) . to . be . exist ( )
@@ -103,9 +114,10 @@ describe('dialFSM', () => {
103
114
} )
104
115
105
116
it ( 'should emit a `closed` event when closed' , ( done ) => {
106
- switchB . handle ( '/closed/1.0.0' , ( ) => { } )
117
+ protocol = '/closed/1.0.0'
118
+ switchB . handle ( protocol , ( ) => { } )
107
119
108
- switchA . dialFSM ( switchB . _peerInfo , '/closed/1.0.0' , ( err , connFSM ) => {
120
+ switchA . dialFSM ( switchB . _peerInfo , protocol , ( err , connFSM ) => {
109
121
expect ( err ) . to . not . exist ( )
110
122
111
123
connFSM . once ( 'close' , ( ) => {
@@ -120,11 +132,49 @@ describe('dialFSM', () => {
120
132
} )
121
133
} )
122
134
135
+ it ( 'should have the peers protocols once connected' , ( done ) => {
136
+ protocol = '/lscheck/1.0.0'
137
+ switchB . handle ( protocol , ( ) => { } )
138
+
139
+ expect ( 4 ) . checks ( done )
140
+
141
+ switchB . once ( 'peer-mux-established' , ( peerInfo ) => {
142
+ const peerB = switchA . _peerBook . get ( switchB . _peerInfo . id . toB58String ( ) )
143
+ const peerA = switchB . _peerBook . get ( switchA . _peerInfo . id . toB58String ( ) )
144
+ // Verify the dialer knows the receiver's protocols
145
+ expect ( Array . from ( peerB . protocols ) ) . to . eql ( [
146
+ multiplex . multicodec ,
147
+ identify . multicodec ,
148
+ protocol
149
+ ] ) . mark ( )
150
+ // Verify the receiver knows the dialer's protocols
151
+ expect ( Array . from ( peerA . protocols ) ) . to . eql ( [
152
+ multiplex . multicodec ,
153
+ identify . multicodec
154
+ ] ) . mark ( )
155
+
156
+ switchA . hangUp ( switchB . _peerInfo )
157
+ } )
158
+
159
+ switchA . dialFSM ( switchB . _peerInfo , protocol , ( err , connFSM ) => {
160
+ expect ( err ) . to . not . exist ( ) . mark ( )
161
+
162
+ connFSM . once ( 'close' , ( ) => {
163
+ // Just mark that close was called
164
+ expect ( true ) . to . eql ( true ) . mark ( )
165
+ } )
166
+ } )
167
+ } )
168
+
123
169
it ( 'should close when the receiver closes' , ( done ) => {
170
+ protocol = '/closed/1.0.0'
171
+ switchB . handle ( protocol , ( ) => { } )
172
+
124
173
// wait for the expects to happen
125
- expect ( 2 ) . checks ( done )
174
+ expect ( 2 ) . checks ( ( ) => {
175
+ done ( )
176
+ } )
126
177
127
- switchB . handle ( '/closed/1.0.0' , ( ) => { } )
128
178
switchB . on ( 'peer-mux-established' , ( peerInfo ) => {
129
179
if ( peerInfo . id . toB58String ( ) === peerAId ) {
130
180
switchB . removeAllListeners ( 'peer-mux-established' )
@@ -133,7 +183,7 @@ describe('dialFSM', () => {
133
183
}
134
184
} )
135
185
136
- switchA . dialFSM ( switchB . _peerInfo , '/closed/1.0.0' , ( err , connFSM ) => {
186
+ switchA . dialFSM ( switchB . _peerInfo , protocol , ( err , connFSM ) => {
137
187
expect ( err ) . to . not . exist ( )
138
188
139
189
connFSM . once ( 'close' , ( ) => {
@@ -161,8 +211,11 @@ describe('dialFSM', () => {
161
211
} )
162
212
163
213
it ( 'parallel dials to one another should disconnect on hangup' , function ( done ) {
164
- switchA . handle ( '/parallel/1.0.0' , ( _ , conn ) => { pull ( conn , conn ) } )
165
- switchB . handle ( '/parallel/1.0.0' , ( _ , conn ) => { pull ( conn , conn ) } )
214
+ this . timeout ( 10e3 )
215
+ protocol = '/parallel/1.0.0'
216
+
217
+ switchA . handle ( protocol , ( _ , conn ) => { pull ( conn , conn ) } )
218
+ switchB . handle ( protocol , ( _ , conn ) => { pull ( conn , conn ) } )
166
219
167
220
expect ( switchA . connection . getAllById ( peerBId ) ) . to . have . length ( 0 )
168
221
@@ -180,12 +233,12 @@ describe('dialFSM', () => {
180
233
expect ( peerInfo . id . toB58String ( ) ) . to . eql ( peerAId ) . mark ( )
181
234
} )
182
235
183
- switchA . dialFSM ( switchB . _peerInfo , '/parallel/1.0.0' , ( err , connFSM ) => {
236
+ switchA . dialFSM ( switchB . _peerInfo , protocol , ( err , connFSM ) => {
184
237
expect ( err ) . to . not . exist ( )
185
238
// Hold the dial from A, until switch B is done dialing to ensure
186
239
// we have both incoming and outgoing connections
187
240
connFSM . _state . on ( 'DIALING:leave' , ( cb ) => {
188
- switchB . dialFSM ( switchA . _peerInfo , '/parallel/1.0.0' , ( err , connB ) => {
241
+ switchB . dialFSM ( switchA . _peerInfo , protocol , ( err , connB ) => {
189
242
expect ( err ) . to . not . exist ( )
190
243
connB . on ( 'muxed' , cb )
191
244
} )
@@ -201,8 +254,9 @@ describe('dialFSM', () => {
201
254
} )
202
255
203
256
it ( 'parallel dials to one another should disconnect on stop' , ( done ) => {
204
- switchA . handle ( '/parallel/1.0.0' , ( _ , conn ) => { pull ( conn , conn ) } )
205
- switchB . handle ( '/parallel/1.0.0' , ( _ , conn ) => { pull ( conn , conn ) } )
257
+ protocol = '/parallel/1.0.0'
258
+ switchA . handle ( protocol , ( _ , conn ) => { pull ( conn , conn ) } )
259
+ switchB . handle ( protocol , ( _ , conn ) => { pull ( conn , conn ) } )
206
260
207
261
// 4 close checks and 1 hangup check
208
262
expect ( 5 ) . checks ( ( ) => {
0 commit comments