Skip to content

Commit

Permalink
test(kad): test get_closest_with_different_num_results
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Aug 27, 2024
1 parent 12584cd commit 6fbda80
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ where
/// [`Event::OutboundQueryProgressed{QueryResult::GetClosestPeers}`].
///
/// The expected responding peers is specified by `num_results`
/// Note that the result is capped after exceeds K_VALUE
pub fn get_closest_peers_num_results<K>(&mut self, key: K, num_results: NonZeroUsize) -> QueryId
where
K: Into<kbucket::Key<K>> + Into<Vec<u8>> + Clone,
Expand Down
62 changes: 62 additions & 0 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,68 @@ fn unresponsive_not_returned_indirect() {
}))
}

#[test]
// Test the result of get_closest_peers with different num_results
// Note that the result is capped after exceeds K_VALUE
fn get_closest_with_different_num_results() {
let k_value = K_VALUE.get();
for replication_factor in [5, k_value / 2, k_value] {
for num_results in k_value / 2..k_value * 2 {
get_closest_with_different_num_results_inner(num_results, replication_factor)
}
}
}

fn get_closest_with_different_num_results_inner(num_results: usize, replication_factor: usize) {
let k_value = K_VALUE.get();
let num_of_nodes = 3 * k_value;
let mut cfg = Config::new(PROTOCOL_NAME);
cfg.set_replication_factor(NonZeroUsize::new(replication_factor).unwrap());
let swarms = build_connected_nodes_with_config(num_of_nodes, replication_factor - 1, cfg);

let mut swarms = swarms
.into_iter()
.map(|(_addr, swarm)| swarm)
.collect::<Vec<_>>();

// Ask first to search a random value.
let search_target = PeerId::random();
let Some(num_results_nonzero) = std::num::NonZeroUsize::new(num_results) else {
panic!("Unexpected NonZeroUsize val of {num_results}");
};
swarms[0]
.behaviour_mut()
.get_closest_peers_num_results(search_target, num_results_nonzero);

block_on(poll_fn(move |ctx| {
for swarm in &mut swarms {
loop {
match swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(SwarmEvent::Behaviour(Event::OutboundQueryProgressed {
result: QueryResult::GetClosestPeers(Ok(ok)),
..
}))) => {
assert_eq!(&ok.key[..], search_target.to_bytes().as_slice());
if num_results > k_value {
assert_eq!(ok.peers.len(), k_value, "Failed with replication_factor: {replication_factor}, num_results: {num_results}");
} else {
assert_eq!(ok.peers.len(), num_results, "Failed with replication_factor: {replication_factor}, num_results: {num_results}");
}

return Poll::Ready(());
}
// Ignore any other event.
Poll::Ready(Some(_)) => (),
e @ Poll::Ready(_) => panic!("Unexpected return value: {e:?}"),
Poll::Pending => break,
}
}
}

Poll::Pending
}))
}

#[test]
fn get_record_not_found() {
let mut swarms = build_nodes(3);
Expand Down

0 comments on commit 6fbda80

Please sign in to comment.