Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/kad/query/peers/closest: Make at_capacity use max #1548

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion protocols/kad/src/query/peers/closest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ impl ClosestPeersIter {
/// k closest nodes it has not already queried".
fn at_capacity(&self) -> bool {
match self.state {
State::Stalled => self.num_waiting >= self.config.num_results,
State::Stalled => self.num_waiting >= usize::max(
self.config.num_results, self.config.parallelism
),
State::Iterating { .. } => self.num_waiting >= self.config.parallelism,
State::Finished => true
}
Expand Down Expand Up @@ -691,4 +693,29 @@ mod tests {

QuickCheck::new().tests(10).quickcheck(prop as fn(_) -> _)
}

#[test]
fn stalled_at_capacity() {
fn prop(mut iter: ClosestPeersIter) {
iter.state = State::Stalled;

for i in 0..usize::max(iter.config.parallelism, iter.config.num_results) {
iter.num_waiting = i;
assert!(
!iter.at_capacity(),
"Iterator should not be at capacity if less than \
`max(parallelism, num_results)` requests are waiting.",
)
}

iter.num_waiting = usize::max(iter.config.parallelism, iter.config.num_results);
assert!(
iter.at_capacity(),
"Iterator should be at capacity if `max(parallelism, num_results)` requests are \
waiting.",
)
}

QuickCheck::new().tests(10).quickcheck(prop as fn(_))
}
}