Skip to content

Commit 6213f00

Browse files
committed
Allow tests for wasm with wasm-pack
1 parent 79cfacd commit 6213f00

10 files changed

+70
-23
lines changed

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ gloo-timers = { version = "0.3", features = ["futures"] }
1818
[dev-dependencies]
1919
anyhow = "1"
2020
reqwest = "0.12"
21+
22+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
23+
tokio = { version = "1", features = [
24+
"macros",
25+
"rt",
26+
"sync",
27+
], default-features = false }
28+
wasm-bindgen-test = "0.3"
29+
30+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
2131
sqlx = { version = "0.8.0", features = ["runtime-tokio", "sqlite"] }
2232
tokio = { version = "1", features = [
2333
"time",

examples/blocking.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
use anyhow::Result;
2-
use backon::BlockingRetryable;
3-
use backon::ExponentialBuilder;
42

53
// For more examples, please see: https://docs.rs/backon/#examples
64

75
fn fetch() -> Result<String> {
86
Ok("hello, world!".to_string())
97
}
108

9+
// this example does not run on wasm32-unknown-unknown
10+
#[cfg(not(target_arch = "wasm32"))]
1111
fn main() -> Result<()> {
12-
let content = fetch.retry(&ExponentialBuilder::default()).call()?;
12+
use backon::BlockingRetryable;
13+
14+
let content = fetch.retry(&backon::ExponentialBuilder::default()).call()?;
1315
println!("fetch succeeded: {}", content);
1416

1517
Ok(())
1618
}
19+
20+
#[cfg(target_arch = "wasm32")]
21+
fn main() {}

examples/closure.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
use anyhow::Result;
2-
use backon::BlockingRetryable;
3-
use backon::ExponentialBuilder;
4-
51
// For more examples, please see: https://docs.rs/backon/#examples
62

7-
fn main() -> Result<()> {
3+
// this example does not run on wasm32-unknown-unknown
4+
#[cfg(not(target_arch = "wasm32"))]
5+
fn main() -> anyhow::Result<()> {
6+
use backon::BlockingRetryable;
7+
88
let var = 42;
99
// `f` can use input variables
1010
let f = || Ok::<u32, anyhow::Error>(var);
11-
let result = f.retry(&ExponentialBuilder::default()).call()?;
11+
let result = f.retry(&backon::ExponentialBuilder::default()).call()?;
1212
println!("var = {result}");
1313

1414
Ok(())
1515
}
16+
17+
#[cfg(target_arch = "wasm32")]
18+
fn main() {}

examples/sqlx.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
use anyhow::Result;
2-
use backon::ExponentialBuilder;
3-
use backon::Retryable;
4-
use sqlx::sqlite::SqlitePoolOptions;
1+
// For more examples, please see: https://docs.rs/backon/#examples
52

3+
// this example does not run on wasm32-unknown-unknown
4+
#[cfg(not(target_arch = "wasm32"))]
65
#[tokio::main]
7-
async fn main() -> Result<()> {
8-
let pool = SqlitePoolOptions::new()
6+
async fn main() -> anyhow::Result<()> {
7+
use backon::Retryable;
8+
9+
let pool = sqlx::sqlite::SqlitePoolOptions::new()
910
.max_connections(5)
1011
.connect("sqlite::memory:")
1112
.await?;
1213

1314
let row: (i64,) = (|| sqlx::query_as("SELECT $1").bind(150_i64).fetch_one(&pool))
14-
.retry(&ExponentialBuilder::default())
15+
.retry(&backon::ExponentialBuilder::default())
1516
.await?;
1617

1718
assert_eq!(row.0, 150);
1819

1920
Ok(())
2021
}
22+
23+
#[cfg(target_arch = "wasm32")]
24+
fn main() {}

src/constant.rs

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ impl Iterator for ConstantBackoff {
129129
mod tests {
130130
use std::time::Duration;
131131

132+
#[cfg(target_arch = "wasm32")]
133+
use wasm_bindgen_test::wasm_bindgen_test as test;
134+
132135
use crate::backoff::BackoffBuilder;
133136
use crate::constant::ConstantBuilder;
134137

src/exponential.rs

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ pub(crate) fn saturating_mul(d: Duration, rhs: f32) -> Duration {
180180
mod tests {
181181
use std::time::Duration;
182182

183+
#[cfg(target_arch = "wasm32")]
184+
use wasm_bindgen_test::wasm_bindgen_test as test;
185+
183186
use crate::backoff::BackoffBuilder;
184187
use crate::exponential::ExponentialBuilder;
185188

src/fibonacci.rs

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ impl Iterator for FibonacciBackoff {
164164
mod tests {
165165
use std::time::Duration;
166166

167+
#[cfg(target_arch = "wasm32")]
168+
use wasm_bindgen_test::wasm_bindgen_test as test;
169+
167170
use crate::backoff::BackoffBuilder;
168171
use crate::fibonacci::FibonacciBuilder;
169172

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,14 @@ pub use retry::Retryable;
181181
mod retry_with_context;
182182
pub use retry_with_context::RetryableWithContext;
183183

184+
#[cfg(not(target_arch = "wasm32"))]
184185
mod blocking_retry;
186+
#[cfg(not(target_arch = "wasm32"))]
185187
pub use blocking_retry::BlockingRetry;
188+
#[cfg(not(target_arch = "wasm32"))]
186189
pub use blocking_retry::BlockingRetryable;
187190

191+
#[cfg(not(target_arch = "wasm32"))]
188192
mod blocking_retry_with_context;
193+
#[cfg(not(target_arch = "wasm32"))]
189194
pub use blocking_retry_with_context::BlockingRetryableWithContext;

src/retry.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,20 @@ mod tests {
288288

289289
use tokio::sync::Mutex;
290290

291+
#[cfg(target_arch = "wasm32")]
292+
use wasm_bindgen_test::wasm_bindgen_test as test;
293+
294+
#[cfg(not(target_arch = "wasm32"))]
295+
use tokio::test;
296+
291297
use super::*;
292298
use crate::exponential::ExponentialBuilder;
293299

294300
async fn always_error() -> anyhow::Result<()> {
295301
Err(anyhow::anyhow!("test_query meets error"))
296302
}
297303

298-
#[tokio::test]
304+
#[test]
299305
async fn test_retry() -> anyhow::Result<()> {
300306
let result = always_error
301307
.retry(&ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)))
@@ -306,7 +312,7 @@ mod tests {
306312
Ok(())
307313
}
308314

309-
#[tokio::test]
315+
#[test]
310316
async fn test_retry_with_not_retryable_error() -> anyhow::Result<()> {
311317
let error_times = Mutex::new(0);
312318

@@ -331,7 +337,7 @@ mod tests {
331337
Ok(())
332338
}
333339

334-
#[tokio::test]
340+
#[test]
335341
async fn test_retry_with_retryable_error() -> anyhow::Result<()> {
336342
let error_times = Mutex::new(0);
337343

@@ -356,7 +362,7 @@ mod tests {
356362
Ok(())
357363
}
358364

359-
#[tokio::test]
365+
#[test]
360366
async fn test_fn_mut_when_and_notify() -> anyhow::Result<()> {
361367
let mut calls_retryable: Vec<()> = vec![];
362368
let mut calls_notify: Vec<()> = vec![];

src/retry_with_context.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,17 @@ where
336336
mod tests {
337337
use std::time::Duration;
338338

339-
use anyhow::anyhow;
339+
use anyhow::{anyhow, Result};
340340
use tokio::sync::Mutex;
341341

342+
#[cfg(target_arch = "wasm32")]
343+
use wasm_bindgen_test::wasm_bindgen_test as test;
344+
345+
#[cfg(not(target_arch = "wasm32"))]
346+
use tokio::test;
347+
342348
use super::*;
343349
use crate::exponential::ExponentialBuilder;
344-
use anyhow::Result;
345350

346351
struct Test;
347352

@@ -351,7 +356,7 @@ mod tests {
351356
}
352357
}
353358

354-
#[tokio::test]
359+
#[test]
355360
async fn test_retry_with_not_retryable_error() -> Result<()> {
356361
let error_times = Mutex::new(0);
357362

0 commit comments

Comments
 (0)