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

Fix Estimator panic #403

Merged
merged 1 commit into from
Mar 17, 2022
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
21 changes: 20 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl Estimator {
}

fn record(&mut self, new: u64, now: Instant) {
let delta = new - self.prev.0;
let delta = new.saturating_sub(self.prev.0);
if delta == 0 || now < self.prev.1 {
return;
}
Expand Down Expand Up @@ -511,6 +511,8 @@ pub(crate) enum Status {

#[cfg(test)]
mod tests {
use crate::ProgressBar;

use super::*;

#[test]
Expand Down Expand Up @@ -558,4 +560,21 @@ mod tests {
let secs = duration_to_secs(duration);
assert_eq!(secs_to_duration(secs), duration);
}

#[test]
fn test_estimator_rewind_position() {
let now = Instant::now();
let mut est = Estimator::new(now);
est.record(0, now);
est.record(1, now);
// Should not panic.
est.record(0, now);

let pb = ProgressBar::hidden();
pb.set_length(10);
pb.set_position(1);
pb.tick();
// Should not panic.
pb.set_position(0);
}
}