diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 2d169e3..c486847 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -36,6 +36,7 @@ gloo-timers = { version = "0.3", optional = true } [dev-dependencies] anyhow = "1" reqwest = "0.12" +spin = "0.9.8" [target.'cfg(target_arch = "wasm32")'.dev-dependencies] tokio = { version = "1", features = [ diff --git a/backon/src/blocking_retry.rs b/backon/src/blocking_retry.rs index 56a80f4..227ad9b 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -198,8 +198,11 @@ where #[cfg(test)] mod tests { + use alloc::string::ToString; + use alloc::vec; + use alloc::vec::Vec; use core::time::Duration; - use std::sync::Mutex; + use spin::Mutex; use super::*; use crate::ExponentialBuilder; @@ -224,7 +227,7 @@ mod tests { let error_times = Mutex::new(0); let f = || { - let mut x = error_times.lock().unwrap(); + let mut x = error_times.lock(); *x += 1; Err::<(), anyhow::Error>(anyhow::anyhow!("not retryable")) }; @@ -240,7 +243,7 @@ mod tests { assert_eq!("not retryable", result.unwrap_err().to_string()); // `f` always returns error "not retryable", so it should be executed // only once. - assert_eq!(*error_times.lock().unwrap(), 1); + assert_eq!(*error_times.lock(), 1); Ok(()) } @@ -249,8 +252,8 @@ mod tests { let error_times = Mutex::new(0); let f = || { - println!("I have been called!"); - let mut x = error_times.lock().unwrap(); + // println!("I have been called!"); + let mut x = error_times.lock(); *x += 1; Err::<(), anyhow::Error>(anyhow::anyhow!("retryable")) }; @@ -266,7 +269,7 @@ mod tests { assert_eq!("retryable", result.unwrap_err().to_string()); // `f` always returns error "retryable", so it should be executed // 4 times (retry 3 times). - assert_eq!(*error_times.lock().unwrap(), 4); + assert_eq!(*error_times.lock(), 4); Ok(()) } diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index f61f78f..383fd34 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -117,7 +117,7 @@ where /// Call the retried function. /// - /// TODO: implement [`std::ops::FnOnce`] after it stable. + /// TODO: implement [`FnOnce`] after it stable. pub fn call(mut self) -> (Ctx, Result) { let mut ctx = self.ctx.take().expect("context must be valid"); loop { @@ -147,14 +147,13 @@ where #[cfg(test)] mod tests { - use core::time::Duration; - - use anyhow::anyhow; - use std::sync::Mutex; - use super::*; use crate::ExponentialBuilder; + use alloc::string::ToString; + use anyhow::anyhow; use anyhow::Result; + use core::time::Duration; + use std::sync::Mutex; struct Test; diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 705eb24..79bd9be 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -96,6 +96,7 @@ #![deny(unused_qualifications)] #![no_std] #[cfg(feature = "std")] +extern crate alloc; extern crate std; mod backoff; diff --git a/backon/src/retry.rs b/backon/src/retry.rs index 617a527..da3e7d7 100644 --- a/backon/src/retry.rs +++ b/backon/src/retry.rs @@ -320,7 +320,10 @@ where #[cfg(test)] #[cfg(any(feature = "tokio-sleep", feature = "gloo-timers-sleep"))] mod tests { - use std::{future::ready, time::Duration}; + use alloc::string::ToString; + use alloc::vec; + use alloc::vec::Vec; + use core::{future::ready, time::Duration}; use tokio::sync::Mutex; #[cfg(target_arch = "wasm32")] diff --git a/backon/src/retry_with_context.rs b/backon/src/retry_with_context.rs index 7de7bec..53f7ba2 100644 --- a/backon/src/retry_with_context.rs +++ b/backon/src/retry_with_context.rs @@ -362,9 +362,9 @@ where #[cfg(test)] mod tests { - use core::time::Duration; - + use alloc::string::ToString; use anyhow::{anyhow, Result}; + use core::time::Duration; use tokio::sync::Mutex; #[cfg(target_arch = "wasm32")]