@@ -1606,3 +1606,60 @@ fn get_providers_limit_n_2() {
16061606fn get_providers_limit_n_5 ( ) {
16071607 get_providers_limit :: < 5 > ( ) ;
16081608}
1609+
1610+ // Test that nodes respond with K amount of peers even when replication factor is set lower than K.
1611+ #[ test]
1612+ fn get_closest_peers_should_return_up_to_k_peers ( ) {
1613+ let k_value = K_VALUE . get ( ) ;
1614+
1615+ // Rplication factor should not influence the amount of peers returned in `GetClosestPeers`.
1616+ for replication_factor in 5 ..k_value + 1 {
1617+ // Should be enough nodes for every node to have >= K nodes in their RT.
1618+ let num_of_nodes = 3 * k_value;
1619+
1620+ let mut cfg = Config :: new ( PROTOCOL_NAME ) ;
1621+ cfg. set_replication_factor ( NonZeroUsize :: new ( replication_factor) . unwrap ( ) ) ;
1622+
1623+ let swarms = build_connected_nodes_with_config ( num_of_nodes, replication_factor - 1 , cfg) ;
1624+ let mut swarms = swarms
1625+ . into_iter ( )
1626+ . map ( |( _addr, swarm) | swarm)
1627+ . collect :: < Vec < _ > > ( ) ;
1628+
1629+ // Ask first node to search for a random peer.
1630+ let search_target = PeerId :: random ( ) ;
1631+ swarms[ 0 ] . behaviour_mut ( ) . get_closest_peers ( search_target) ;
1632+
1633+ block_on ( poll_fn ( move |ctx| {
1634+ for swarm in & mut swarms {
1635+ loop {
1636+ match swarm. poll_next_unpin ( ctx) {
1637+ Poll :: Ready ( Some ( SwarmEvent :: Behaviour (
1638+ Event :: OutboundQueryProgressed {
1639+ result : QueryResult :: GetClosestPeers ( Ok ( ok) ) ,
1640+ ..
1641+ } ,
1642+ ) ) ) => {
1643+ assert_eq ! ( & ok. key[ ..] , search_target. to_bytes( ) . as_slice( ) ) ;
1644+ // Verify that we get K_VALUE amount of peers even with lower
1645+ // replication factor.
1646+ assert_eq ! (
1647+ ok. peers. len( ) ,
1648+ k_value,
1649+ "Expected K_VALUE ({}) peers but got {}" ,
1650+ k_value,
1651+ ok. peers. len( )
1652+ ) ;
1653+ return Poll :: Ready ( ( ) ) ;
1654+ }
1655+ // Ignore any other event.
1656+ Poll :: Ready ( Some ( _) ) => ( ) ,
1657+ e @ Poll :: Ready ( _) => panic ! ( "Unexpected return value: {e:?}" ) ,
1658+ Poll :: Pending => break ,
1659+ }
1660+ }
1661+ }
1662+ Poll :: Pending
1663+ } ) )
1664+ }
1665+ }
0 commit comments