Skip to content

Commit

Permalink
tests: enable running wasm32-unknown-unknown tests (#4421)
Browse files Browse the repository at this point in the history
* Several of tokio's features (e.g. the channel implementation) do not
  need a runtime to work, and can be compiled and used for
  wasm32-unknown-unknown targets
* This change enables running tests for the `sync` and `macros` features
  so that we can note any regressions there
  • Loading branch information
ipetkov authored Jan 27, 2022
1 parent 2a5071f commit 2747043
Show file tree
Hide file tree
Showing 30 changed files with 301 additions and 109 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- loom-compile
- check-readme
- test-hyper
- wasm32-unknown-unknown
steps:
- run: exit 0

Expand Down Expand Up @@ -391,3 +392,17 @@ jobs:
echo 'tokio-test = { path = "../tokio-test" }' >>Cargo.toml
git diff
cargo test --features full
wasm32-unknown-unknown:
name: test tokio for wasm32-unknown-unknown
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup update stable
- uses: Swatinem/rust-cache@v1
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: test tokio
run: wasm-pack test --node -- --features "macros sync"
working-directory: tokio
9 changes: 7 additions & 2 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,17 @@ tokio-test = { version = "0.4.0", path = "../tokio-test" }
tokio-stream = { version = "0.1", path = "../tokio-stream" }
futures = { version = "0.3.0", features = ["async-await"] }
mockall = "0.10.2"
proptest = "1"
rand = "0.8.0"
tempfile = "3.1.0"
async-stream = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
proptest = "1"
rand = "0.8.0"
socket2 = "0.4"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.0"

[target.'cfg(target_os = "freebsd")'.dev-dependencies]
mio-aio = { version = "0.6.0", features = ["tokio"] }

Expand Down
3 changes: 3 additions & 0 deletions tokio/src/coop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ cfg_coop! {
mod test {
use super::*;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

fn get() -> Budget {
CURRENT.with(|cell| cell.get())
}
Expand Down
1 change: 1 addition & 0 deletions tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@

// At the top due to macros
#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
#[macro_use]
mod tests;

Expand Down
1 change: 1 addition & 0 deletions tokio/src/runtime/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl<S: Schedule> LocalNotified<S> {
impl<S: Schedule> UnownedTask<S> {
// Used in test of the inject queue.
#[cfg(test)]
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(super) fn into_notified(self) -> Notified<S> {
Notified(self.into_task())
}
Expand Down
4 changes: 4 additions & 0 deletions tokio/src/sync/tests/atomic_waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ impl AssertSync for AtomicWaker {}
impl AssertSend for Waker {}
impl AssertSync for Waker {}

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[test]
fn basic_usage() {
let mut waker = task::spawn(AtomicWaker::new());
Expand All @@ -34,6 +37,7 @@ fn wake_without_register() {
}

#[test]
#[cfg(not(target_arch = "wasm32"))] // wasm currently doesn't support unwinding
fn atomic_waker_panic_safe() {
use std::panic;
use std::ptr;
Expand Down
3 changes: 3 additions & 0 deletions tokio/src/sync/tests/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::mem::ManuallyDrop;
use std::sync::Arc;
use std::task::{Context, RawWaker, RawWakerVTable, Waker};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[test]
fn notify_clones_waker_before_lock() {
const VTABLE: &RawWakerVTable = &RawWakerVTable::new(clone_w, wake, wake_by_ref, drop_w);
Expand Down
4 changes: 4 additions & 0 deletions tokio/src/sync/tests/semaphore_batch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::sync::batch_semaphore::Semaphore;
use tokio_test::*;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[test]
fn poll_acquire_one_available() {
let s = Semaphore::new(100);
Expand Down Expand Up @@ -167,6 +170,7 @@ fn poll_acquire_one_zero_permits() {

#[test]
#[should_panic]
#[cfg(not(target_arch = "wasm32"))] // wasm currently doesn't support unwinding
fn validates_max_permits() {
use std::usize;
Semaphore::new((usize::MAX >> 2) + 1);
Expand Down
1 change: 1 addition & 0 deletions tokio/src/util/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ mod tests {
}
}

#[cfg(not(target_arch = "wasm32"))]
proptest::proptest! {
#[test]
fn fuzz_linked_list(ops: Vec<usize>) {
Expand Down
2 changes: 1 addition & 1 deletion tokio/tests/_require_full.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#![cfg(not(feature = "full"))]
#![cfg(not(any(feature = "full", target_arch = "wasm32")))]
compile_error!("run main Tokio tests with `--features full`");
20 changes: 15 additions & 5 deletions tokio/tests/macros_join.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
#![cfg(feature = "macros")]
#![allow(clippy::blacklisted_name)]

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test;

#[cfg(not(target_arch = "wasm32"))]
use tokio::test as maybe_tokio_test;

use tokio::sync::oneshot;
use tokio_test::{assert_pending, assert_ready, task};

#[tokio::test]
#[maybe_tokio_test]
async fn sync_one_lit_expr_comma() {
let foo = tokio::join!(async { 1 },);

assert_eq!(foo, (1,));
}

#[tokio::test]
#[maybe_tokio_test]
async fn sync_one_lit_expr_no_comma() {
let foo = tokio::join!(async { 1 });

assert_eq!(foo, (1,));
}

#[tokio::test]
#[maybe_tokio_test]
async fn sync_two_lit_expr_comma() {
let foo = tokio::join!(async { 1 }, async { 2 },);

assert_eq!(foo, (1, 2));
}

#[tokio::test]
#[maybe_tokio_test]
async fn sync_two_lit_expr_no_comma() {
let foo = tokio::join!(async { 1 }, async { 2 });

assert_eq!(foo, (1, 2));
}

#[tokio::test]
#[maybe_tokio_test]
async fn two_await() {
let (tx1, rx1) = oneshot::channel::<&str>();
let (tx2, rx2) = oneshot::channel::<u32>();
Expand Down
10 changes: 9 additions & 1 deletion tokio/tests/macros_pin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#![cfg(feature = "macros")]

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test;

#[cfg(not(target_arch = "wasm32"))]
use tokio::test as maybe_tokio_test;

async fn one() {}
async fn two() {}

#[tokio::test]
#[maybe_tokio_test]
async fn multi_pin() {
tokio::pin! {
let f1 = one();
Expand Down
Loading

0 comments on commit 2747043

Please sign in to comment.