From 97a3dd2f63577036f56fc9d50204b69f0124dee3 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Fri, 16 Apr 2021 09:31:55 -0500 Subject: [PATCH] Configure WASM Tests and Add Them to CI --- .github/workflows/build-and-test.yaml | 6 ++++++ Cargo.toml | 3 +++ tests/barrier.rs | 2 +- tests/mutex.rs | 15 ++++++++++++++- tests/rwlock.rs | 27 +++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index 4b55a30..089bcda 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -25,6 +25,9 @@ jobs: profile: minimal override: true + - name: Install WASM Test Tools + run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + - name: Run cargo check uses: actions-rs/cargo@v1 with: @@ -44,6 +47,9 @@ jobs: command: check args: --all --bins --examples --tests --all-features --target wasm32-unknown-unknown + - name: Test WASM + run: wasm-pack test --headless --chrome + - name: Run cargo test uses: actions-rs/cargo@v1 with: diff --git a/Cargo.toml b/Cargo.toml index db79041..593aa97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,6 @@ event-listener = "2.5.1" async-channel = "1.5.0" fastrand = "1.4.0" futures-lite = "1.11.0" + +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = "0.3" \ No newline at end of file diff --git a/tests/barrier.rs b/tests/barrier.rs index 83fbc72..057d4fc 100644 --- a/tests/barrier.rs +++ b/tests/barrier.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use std::thread; use async_lock::Barrier; -use futures_lite::future; +use futures_lite::future; #[test] fn smoke() { diff --git a/tests/mutex.rs b/tests/mutex.rs index 7a1acdc..69364ef 100644 --- a/tests/mutex.rs +++ b/tests/mutex.rs @@ -1,10 +1,19 @@ -use std::sync::Arc; +#[cfg(not(target_arch = "wasm32"))] use std::thread; +#[cfg(not(target_arch = "wasm32"))] +use std::sync::Arc; use async_lock::Mutex; use futures_lite::future; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_test::*; + +#[cfg(target_arch = "wasm32")] +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn smoke() { future::block_on(async { let m = Mutex::new(()); @@ -14,24 +23,28 @@ fn smoke() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn try_lock() { let m = Mutex::new(()); *m.try_lock().unwrap() = (); } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn into_inner() { let m = Mutex::new(10i32); assert_eq!(m.into_inner(), 10); } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_mut() { let mut m = Mutex::new(10i32); *m.get_mut() = 20; assert_eq!(m.into_inner(), 20); } +#[cfg(not(target_arch = "wasm32"))] #[test] fn contention() { future::block_on(async { diff --git a/tests/rwlock.rs b/tests/rwlock.rs index 7e6473d..40a3350 100644 --- a/tests/rwlock.rs +++ b/tests/rwlock.rs @@ -1,11 +1,24 @@ -use std::future::Future; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; + +#[cfg(not(target_arch = "wasm32"))] +use futures_lite::prelude::*; +#[cfg(not(target_arch = "wasm32"))] +use std::future::Future; +#[cfg(not(target_arch = "wasm32"))] use std::thread; +use futures_lite::future; + use async_lock::{RwLock, RwLockUpgradableReadGuard}; -use futures_lite::{future, prelude::*}; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_test::*; + +#[cfg(target_arch = "wasm32")] +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +#[cfg(not(target_arch = "wasm32"))] fn spawn(f: impl Future + Send + 'static) -> future::Boxed { let (s, r) = async_channel::bounded(1); thread::spawn(move || { @@ -17,6 +30,7 @@ fn spawn(f: impl Future + Send + 'static) -> futu } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn smoke() { future::block_on(async { let lock = RwLock::new(()); @@ -28,6 +42,7 @@ fn smoke() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn try_write() { future::block_on(async { let lock = RwLock::new(0isize); @@ -38,12 +53,14 @@ fn try_write() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn into_inner() { let lock = RwLock::new(10); assert_eq!(lock.into_inner(), 10); } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn into_inner_and_drop() { struct Counter(Arc); @@ -66,12 +83,14 @@ fn into_inner_and_drop() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_mut() { let mut lock = RwLock::new(10); *lock.get_mut() = 20; assert_eq!(lock.into_inner(), 20); } +#[cfg(not(target_arch = "wasm32"))] #[test] fn contention() { const N: u32 = 10; @@ -105,6 +124,7 @@ fn contention() { }); } +#[cfg(not(target_arch = "wasm32"))] #[test] fn writer_and_readers() { let lock = Arc::new(RwLock::new(0i32)); @@ -151,6 +171,7 @@ fn writer_and_readers() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn upgrade() { future::block_on(async { let lock: RwLock = RwLock::new(0); @@ -181,6 +202,7 @@ fn upgrade() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn not_upgrade() { future::block_on(async { let mutex: RwLock = RwLock::new(0); @@ -212,6 +234,7 @@ fn not_upgrade() { } #[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn upgradable_with_concurrent_writer() { future::block_on(async { let lock: Arc> = Arc::new(RwLock::new(0));