@@ -302,4 +302,52 @@ describe('delegated-routing-v1-http-api-client', () => {
302302 const receivedRecord = new Uint8Array ( await res . arrayBuffer ( ) )
303303 expect ( marshalIPNSRecord ( record ) ) . to . equalBytes ( receivedRecord )
304304 } )
305+
306+ it ( 'should deduplicate concurrent requests to the same URL' , async ( ) => {
307+ const cid = CID . parse ( 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' )
308+ const providers = [ {
309+ Protocol : 'transport-bitswap' ,
310+ Schema : 'bitswap' ,
311+ Metadata : 'gBI=' ,
312+ ID : ( await generateKeyPair ( 'Ed25519' ) ) . publicKey . toString ( ) ,
313+ Addrs : [ '/ip4/41.41.41.41/tcp/1234' ]
314+ } ]
315+
316+ // load providers for the router to fetch
317+ await fetch ( `${ process . env . ECHO_SERVER } /add-providers/${ cid . toString ( ) } ` , {
318+ method : 'POST' ,
319+ body : providers . map ( prov => JSON . stringify ( prov ) ) . join ( '\n' )
320+ } )
321+
322+ // Reset call count before our test
323+ await fetch ( `${ process . env . ECHO_SERVER } /reset-call-count` )
324+
325+ // Make multiple concurrent requests
326+ const results = await Promise . all ( [
327+ all ( client . getProviders ( cid ) ) ,
328+ all ( client . getProviders ( cid ) ) ,
329+ all ( client . getProviders ( cid ) ) ,
330+ all ( client . getProviders ( cid ) )
331+ ] )
332+
333+ // Get the number of times the server was called
334+ const callCountRes = await fetch ( `${ process . env . ECHO_SERVER } /get-call-count` )
335+ const callCount = parseInt ( await callCountRes . text ( ) , 10 )
336+
337+ // Verify server was only called once
338+ expect ( callCount ) . to . equal ( 1 )
339+
340+ // Verify all results are the same
341+ console . log ( '-------' , results )
342+ results . forEach ( resultProviders => {
343+ expect ( resultProviders . map ( prov => ( {
344+ id : prov . ID . toString ( ) ,
345+ // eslint-disable-next-line max-nested-callbacks
346+ addrs : prov . Addrs ?. map ( ma => ma . toString ( ) )
347+ } ) ) ) . to . deep . equal ( providers . map ( prov => ( {
348+ id : prov . ID ,
349+ addrs : prov . Addrs
350+ } ) ) )
351+ } )
352+ } )
305353} )
0 commit comments