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 3cf284e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
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
12 changes: 12 additions & 0 deletions 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;

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,18 +23,21 @@ 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;
Expand Down
16 changes: 16 additions & 0 deletions tests/rwlock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

#[cfg(not(target_arch = "wasm32"))]
use std::thread;

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);

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 +25,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 +37,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 +48,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,6 +78,7 @@ 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;
Expand Down Expand Up @@ -151,6 +164,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 +195,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 +227,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 3cf284e

Please sign in to comment.