Skip to content

Commit

Permalink
Fix tests no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-kong committed Aug 27, 2024
1 parent 7f6960c commit 2e76686
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
15 changes: 9 additions & 6 deletions backon/src/blocking_retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"))
};
Expand All @@ -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(())
}

Expand All @@ -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"))
};
Expand All @@ -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(())
}

Expand Down
11 changes: 5 additions & 6 deletions backon/src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E>) {
let mut ctx = self.ctx.take().expect("context must be valid");
loop {
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#![deny(unused_qualifications)]
#![no_std]
#[cfg(feature = "std")]
extern crate alloc;
extern crate std;

mod backoff;
Expand Down
5 changes: 4 additions & 1 deletion backon/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
4 changes: 2 additions & 2 deletions backon/src/retry_with_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 2e76686

Please sign in to comment.