Skip to content

Commit

Permalink
Configure WASM Tests and Add Them to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Apr 16, 2021
1 parent 44e8c9b commit 97a3dd2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion tests/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 14 additions & 1 deletion tests/mutex.rs
Original file line number Diff line number Diff line change
@@ -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(());
Expand All @@ -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 {
Expand Down
27 changes: 25 additions & 2 deletions tests/rwlock.rs
Original file line number Diff line number Diff line change
@@ -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<T: Send + 'static>(f: impl Future<Output = T> + Send + 'static) -> future::Boxed<T> {
let (s, r) = async_channel::bounded(1);
thread::spawn(move || {
Expand All @@ -17,6 +30,7 @@ fn spawn<T: Send + 'static>(f: impl Future<Output = T> + Send + 'static) -> futu
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn smoke() {
future::block_on(async {
let lock = RwLock::new(());
Expand All @@ -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);
Expand All @@ -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<AtomicUsize>);

Expand All @@ -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;
Expand Down Expand Up @@ -105,6 +124,7 @@ fn contention() {
});
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn writer_and_readers() {
let lock = Arc::new(RwLock::new(0i32));
Expand Down Expand Up @@ -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<i32> = RwLock::new(0);
Expand Down Expand Up @@ -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<i32> = RwLock::new(0);
Expand Down Expand Up @@ -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<RwLock<i32>> = Arc::new(RwLock::new(0));
Expand Down

0 comments on commit 97a3dd2

Please sign in to comment.