@@ -180,6 +180,52 @@ describe('dial queue', () => {
180180 await expect ( dialer . dial ( peerId ) ) . to . eventually . equal ( connection )
181181 } )
182182
183+ it ( 'should look up peer routing after user-supplied multiaddrs are exhausted' , async ( ) => {
184+ const peerId = peerIdFromPrivateKey ( await generateKeyPair ( 'Ed25519' ) )
185+ const connection = stubInterface < Connection > ( )
186+ const provided1 = multiaddr ( `/ip4/127.0.0.1/tcp/1231/p2p/${ peerId } ` )
187+ const provided2 = multiaddr ( `/ip4/127.0.0.1/tcp/1232/p2p/${ peerId } ` )
188+ const routed = multiaddr ( '/ip4/127.0.0.1/tcp/4001' )
189+ const routedWithPeer = routed . encapsulate ( `/p2p/${ peerId } ` )
190+
191+ let dialAttempts = 0
192+
193+ components . peerRouting . findPeer . callsFake ( async ( id ) => {
194+ expect ( id . equals ( peerId ) ) . to . equal ( true )
195+ expect ( dialAttempts ) . to . equal ( 2 )
196+
197+ return {
198+ id : peerId ,
199+ multiaddrs : [ routed ]
200+ }
201+ } )
202+
203+ const actions : Record < string , ( ) => Promise < Connection > > = {
204+ [ provided1 . toString ( ) ] : async ( ) => Promise . reject ( new Error ( 'dial failure' ) ) ,
205+ [ provided2 . toString ( ) ] : async ( ) => Promise . reject ( new Error ( 'dial failure' ) ) ,
206+ [ routedWithPeer . toString ( ) ] : async ( ) => Promise . resolve ( connection )
207+ }
208+
209+ components . transportManager . dialTransportForMultiaddr . returns ( stubInterface < Transport > ( ) )
210+ components . transportManager . dial . callsFake ( async ma => {
211+ dialAttempts ++
212+ const action = actions [ ma . toString ( ) ]
213+
214+ if ( action != null ) {
215+ return action ( )
216+ }
217+
218+ throw new Error ( `No action found for multiaddr ${ ma . toString ( ) } ` )
219+ } )
220+
221+ dialer = new DialQueue ( components )
222+
223+ await expect ( dialer . dial ( [ provided1 , provided2 ] ) ) . to . eventually . equal ( connection )
224+
225+ expect ( components . peerRouting . findPeer ) . to . have . property ( 'callCount' , 1 )
226+ expect ( components . transportManager . dial . getCalls ( ) . map ( c => c . args [ 0 ] . toString ( ) ) ) . to . include ( routedWithPeer . toString ( ) )
227+ } )
228+
183229 it ( 'should end when a single multiaddr dials succeeds even when a final dial fails' , async ( ) => {
184230 const connection = stubInterface < Connection > ( )
185231 const deferredConn = pDefer < Connection > ( )
0 commit comments