From 62e96944ab780529bafc2743dd2bf41ae51875cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Tue, 3 Oct 2023 17:09:59 +0200 Subject: [PATCH] Add unit tests to ExponentialBackoff --- src/exponential_backoff.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/exponential_backoff.rs b/src/exponential_backoff.rs index 9de4f29..fb9cf43 100644 --- a/src/exponential_backoff.rs +++ b/src/exponential_backoff.rs @@ -35,3 +35,29 @@ impl ExponentialBackoff { delay } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn correct_delays() { + let mut backoff = + ExponentialBackoff::new(Duration::from_millis(60), Duration::from_millis(300)); + assert_eq!(backoff.next_delay(), Duration::from_millis(60)); + assert_eq!(backoff.next_delay(), Duration::from_millis(120)); + assert_eq!(backoff.next_delay(), Duration::from_millis(240)); + assert_eq!(backoff.next_delay(), Duration::from_millis(300)); + assert_eq!(backoff.next_delay(), Duration::from_millis(300)); + } + + #[test] + fn reset() { + let mut backoff = + ExponentialBackoff::new(Duration::from_millis(60), Duration::from_millis(300)); + assert_eq!(backoff.next_delay(), Duration::from_millis(60)); + backoff.reset(); + assert_eq!(backoff.next_delay(), Duration::from_millis(60)); + assert_eq!(backoff.next_delay(), Duration::from_millis(120)); + } +}