Skip to content

Commit 2747043

Browse files
authored
tests: enable running wasm32-unknown-unknown tests (#4421)
* 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
1 parent 2a5071f commit 2747043

30 files changed

+301
-109
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- loom-compile
3737
- check-readme
3838
- test-hyper
39+
- wasm32-unknown-unknown
3940
steps:
4041
- run: exit 0
4142

@@ -391,3 +392,17 @@ jobs:
391392
echo 'tokio-test = { path = "../tokio-test" }' >>Cargo.toml
392393
git diff
393394
cargo test --features full
395+
396+
wasm32-unknown-unknown:
397+
name: test tokio for wasm32-unknown-unknown
398+
runs-on: ubuntu-latest
399+
steps:
400+
- uses: actions/checkout@v2
401+
- name: Install Rust
402+
run: rustup update stable
403+
- uses: Swatinem/rust-cache@v1
404+
- name: Install wasm-pack
405+
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
406+
- name: test tokio
407+
run: wasm-pack test --node -- --features "macros sync"
408+
working-directory: tokio

tokio/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,17 @@ tokio-test = { version = "0.4.0", path = "../tokio-test" }
128128
tokio-stream = { version = "0.1", path = "../tokio-stream" }
129129
futures = { version = "0.3.0", features = ["async-await"] }
130130
mockall = "0.10.2"
131-
proptest = "1"
132-
rand = "0.8.0"
133131
tempfile = "3.1.0"
134132
async-stream = "0.3"
133+
134+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
135+
proptest = "1"
136+
rand = "0.8.0"
135137
socket2 = "0.4"
136138

139+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
140+
wasm-bindgen-test = "0.3.0"
141+
137142
[target.'cfg(target_os = "freebsd")'.dev-dependencies]
138143
mio-aio = { version = "0.6.0", features = ["tokio"] }
139144

tokio/src/coop.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ cfg_coop! {
207207
mod test {
208208
use super::*;
209209

210+
#[cfg(target_arch = "wasm32")]
211+
use wasm_bindgen_test::wasm_bindgen_test as test;
212+
210213
fn get() -> Budget {
211214
CURRENT.with(|cell| cell.get())
212215
}

tokio/src/runtime/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
175175
// At the top due to macros
176176
#[cfg(test)]
177+
#[cfg(not(target_arch = "wasm32"))]
177178
#[macro_use]
178179
mod tests;
179180

tokio/src/runtime/task/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ impl<S: Schedule> LocalNotified<S> {
347347
impl<S: Schedule> UnownedTask<S> {
348348
// Used in test of the inject queue.
349349
#[cfg(test)]
350+
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
350351
pub(super) fn into_notified(self) -> Notified<S> {
351352
Notified(self.into_task())
352353
}

tokio/src/sync/tests/atomic_waker.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ impl AssertSync for AtomicWaker {}
1212
impl AssertSend for Waker {}
1313
impl AssertSync for Waker {}
1414

15+
#[cfg(target_arch = "wasm32")]
16+
use wasm_bindgen_test::wasm_bindgen_test as test;
17+
1518
#[test]
1619
fn basic_usage() {
1720
let mut waker = task::spawn(AtomicWaker::new());
@@ -34,6 +37,7 @@ fn wake_without_register() {
3437
}
3538

3639
#[test]
40+
#[cfg(not(target_arch = "wasm32"))] // wasm currently doesn't support unwinding
3741
fn atomic_waker_panic_safe() {
3842
use std::panic;
3943
use std::ptr;

tokio/src/sync/tests/notify.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::mem::ManuallyDrop;
44
use std::sync::Arc;
55
use std::task::{Context, RawWaker, RawWakerVTable, Waker};
66

7+
#[cfg(target_arch = "wasm32")]
8+
use wasm_bindgen_test::wasm_bindgen_test as test;
9+
710
#[test]
811
fn notify_clones_waker_before_lock() {
912
const VTABLE: &RawWakerVTable = &RawWakerVTable::new(clone_w, wake, wake_by_ref, drop_w);

tokio/src/sync/tests/semaphore_batch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::sync::batch_semaphore::Semaphore;
22
use tokio_test::*;
33

4+
#[cfg(target_arch = "wasm32")]
5+
use wasm_bindgen_test::wasm_bindgen_test as test;
6+
47
#[test]
58
fn poll_acquire_one_available() {
69
let s = Semaphore::new(100);
@@ -167,6 +170,7 @@ fn poll_acquire_one_zero_permits() {
167170

168171
#[test]
169172
#[should_panic]
173+
#[cfg(not(target_arch = "wasm32"))] // wasm currently doesn't support unwinding
170174
fn validates_max_permits() {
171175
use std::usize;
172176
Semaphore::new((usize::MAX >> 2) + 1);

tokio/src/util/linked_list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ mod tests {
614614
}
615615
}
616616

617+
#[cfg(not(target_arch = "wasm32"))]
617618
proptest::proptest! {
618619
#[test]
619620
fn fuzz_linked_list(ops: Vec<usize>) {

tokio/tests/_require_full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#![cfg(not(feature = "full"))]
1+
#![cfg(not(any(feature = "full", target_arch = "wasm32")))]
22
compile_error!("run main Tokio tests with `--features full`");

0 commit comments

Comments
 (0)