Skip to content

Commit

Permalink
Test in different worker types and Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 9, 2024
1 parent 0045d6b commit 3ed3edb
Showing 15 changed files with 153 additions and 228 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage-documentation.yaml
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ jobs:
run: echo "BUILD_STD_COMPONENTS=-Zbuild-std=core,alloc" >> $GITHUB_ENV
- name: Test
env:
WASM_BINDGEN_USE_BROWSER: 1
CHROMEDRIVER: chromedriver
run: |
mkdir coverage-output
159 changes: 55 additions & 104 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -15,144 +15,95 @@ env:
jobs:
test:
name:
Test ${{ matrix.target.description }} ${{ matrix.rust.description }} ${{
matrix.features.description }}
Test ${{ matrix.driver.description }} ${{ matrix.environment.description }} ${{
matrix.rust.description }} ${{ matrix.features.description }}

runs-on: ${{ matrix.target.os }}
runs-on: ${{ matrix.driver.os }}

timeout-minutes: 10

strategy:
fail-fast: false
matrix:
target:
- {
os: ubuntu-latest,
target: wasm32-unknown-unknown,
description: Chrome,
name: CHROMEDRIVER,
value: chromedriver,
}
- {
os: ubuntu-latest,
target: wasm32-unknown-unknown,
description: Firefox,
name: GECKODRIVER,
value: geckodriver,
}
- {
os: macos-latest,
target: wasm32-unknown-unknown,
description: Safari,
name: SAFARIDRIVER,
value: safaridriver,
}
- { target: wasm32-unknown-unknown, docargs: -Zdoctest-xcompile }
rust:
- { version: stable }
- { version: nightly }
- {
version: nightly,
description: with Atomics,
component: --component rust-src,
flags: "-Ctarget-feature=+atomics,+bulk-memory",
build-std: true,
atomics: true,
}
features:
- { features: "", no_std: false }
- { features: --no-default-features, no_std: true, description: "(`no_std`)" }
include:
- target:
{
os: ubuntu-latest,
target: x86_64-unknown-linux-gnu,
description: Native,
native: true,
}
rust: { version: stable }
features: { features: "", no_std: false }

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install `wasm-bindgen-cli`
uses: taiki-e/install-action@v2
with:
tool: wasm-bindgen-cli
- name: Install Rust
run: |
rustup toolchain install ${{ matrix.rust.version }} --profile minimal ${{ matrix.rust.component }} --target ${{ matrix.target.target }}
rustup default ${{ matrix.rust.version }}
- name: Set `build-std` components
if: matrix.rust.build-std == true && matrix.features.no_std == false
run: echo "BUILD_STD_COMPONENTS=-Zbuild-std=panic_abort,std" >> $GITHUB_ENV
- name: Set `build-std` `no_std` components
if: matrix.rust.build-std == true && matrix.features.no_std == true
run: echo "BUILD_STD_COMPONENTS=-Zbuild-std=core,alloc" >> $GITHUB_ENV
- name: Set Driver
if: matrix.target.native == false
run: echo "${{ matrix.target.name }}=${{ matrix.target.value }}" >> $GITHUB_ENV
- name: Test
env:
RUSTFLAGS: ${{ matrix.rust.flags }}
run:
cargo test --features serde ${{ matrix.features.features }} --target ${{
matrix.target.target }} $BUILD_STD_COMPONENTS --workspace

doctest:
name:
Doctest ${{ matrix.target.description }} ${{ matrix.rust.description }} ${{
matrix.features.description }}

runs-on: ${{ matrix.target.os }}

timeout-minutes: 10

strategy:
fail-fast: false
matrix:
target:
driver:
- {
os: ubuntu-latest,
target: wasm32-unknown-unknown,
description: Chrome,
name: CHROMEDRIVER,
value: chromedriver,
browser: true,
}
- {
os: ubuntu-latest,
target: wasm32-unknown-unknown,
description: Firefox,
name: GECKODRIVER,
value: geckodriver,
browser: true,
firefox: true,
}
- {
os: macos-latest,
target: wasm32-unknown-unknown,
description: Safari,
name: SAFARIDRIVER,
value: safaridriver,
browser: true,
}
rust:
- { version: nightly }
- { os: ubuntu-24.04, description: Node.js, nodejs: true }
environment:
- { name: WASM_BINDGEN_USE_BROWSER, browser: true }
- {
version: nightly,
description: with Atomics,
component: --component rust-src,
flags: "-Ctarget-feature=+atomics,+bulk-memory",
build-std: true,
description: Dedicated Worker,
name: WASM_BINDGEN_USE_DEDICATED_WORKER,
browser: true,
}
features:
- { features: "", no_std: false }
- { features: --no-default-features, no_std: true, description: "(`no_std`)" }
- {
description: Shared Worker,
name: WASM_BINDGEN_USE_SHARED_WORKER,
browser: true,
shared-worker: true,
}
- {
description: Service Worker,
name: WASM_BINDGEN_USE_SERVICE_WORKER,
browser: true,
service-worker: true,
}
- { browser: false }
include:
- target:
{
os: ubuntu-latest,
target: x86_64-unknown-linux-gnu,
description: Native,
native: true,
}
- target: { target: x86_64-unknown-linux-gnu }
rust: { version: stable }
features: { features: "", no_std: false }
driver: { os: ubuntu-latest, description: Native, native: true }
exclude:
- driver: { browser: true }
environment: { browser: false }
- driver: { nodejs: true }
environment: { browser: true }
# Firefox doesn't support `Atomics.waitAsync()` and the polyfill requires spawning workers.
- driver: { firefox: true }
rust: { atomics: true }
environment: { shared-worker: true }
# Firefox doesn't support module service workers.
- driver: { firefox: true }
environment: { service-worker: true }
# `wasm-bindgen` support of Node.js with atomics is incomplete.
- driver: { nodejs: true }
rust: { atomics: true }

steps:
- name: Checkout
@@ -171,16 +122,16 @@ jobs:
- name: Set `build-std` `no_std` components
if: matrix.rust.build-std == true && matrix.features.no_std == true
run: echo "BUILD_STD_COMPONENTS=-Zbuild-std=core,alloc" >> $GITHUB_ENV
- name: Set Driver and enable cross-compilation
if: matrix.target.native == false
run: |
echo "${{ matrix.target.name }}=${{ matrix.target.value }}" >> $GITHUB_ENV
echo "DOCTEST_XCOMPILE_ARG=-Zdoctest-xcompile" >> $GITHUB_ENV
- name: Doctest
- name: Set Driver
if: matrix.driver.browser == true
run: echo "${{ matrix.driver.name }}=${{ matrix.driver.value }}" >> $GITHUB_ENV
- name: Set Environment
if: matrix.environment.browser == true
run: echo "${{ matrix.environment.name }}=1" >> $GITHUB_ENV
- name: Test
env:
RUSTFLAGS: ${{ matrix.rust.flags }}
RUSTDOCFLAGS: ${{ matrix.rust.flags }}
run:
cargo test --features serde ${{ matrix.features.features }} --target ${{
matrix.target.target }} $BUILD_STD_COMPONENTS --workspace --doc ${{ matrix.target.args }}
$DOCTEST_XCOMPILE_ARG
matrix.target.target }} $BUILD_STD_COMPONENTS --workspace ${{ matrix.target.docargs }}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -195,7 +195,7 @@ test () {

# Run tests.
mkdir -p coverage-input/$path
CHROMEDRIVER=chromedriver WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT=$(realpath coverage-input/$path) $command ${@:3}
WASM_BINDGEN_USE_BROWSER=1 CHROMEDRIVER=chromedriver WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT=$(realpath coverage-input/$path) $command ${@:3}

local crate_name=web_time
local IFS=$'\n'
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -54,8 +54,6 @@
//! # #[cfg(target_arch = "wasm32")]
//! # use tests_web as _;
//! #
//! # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
//! #
//! # #[wasm_bindgen_test::wasm_bindgen_test]
//! # fn main() {
//! let now = Instant::now();
@@ -190,8 +188,5 @@ pub use self::time::*;
#[cfg(all(not(doc), docsrs))]
compile_error!("`--cfg docsrs` must only be used via `RUSTDOCFLAGS`, not `RUSTFLAGS`");

#[cfg(all(test, target_arch = "wasm32"))]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[cfg(all(test, not(target_arch = "wasm32")))]
fn main() {}
3 changes: 1 addition & 2 deletions tests-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -53,9 +53,8 @@ web-time = { path = "../", default-features = false }
futures-channel = { version = "0.3", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3", default-features = false }
web-sys = { version = "0.3", default-features = false, features = [
"DedicatedWorkerGlobalScope",
"console",
"OfflineAudioContext",
"WorkerGlobalScope",
] }
web-thread = { git = "https://github.com/daxpedda/wasm-worker", rev = "93236ab92354cbe0a0c07e71b9888be8b3e45999", optional = true, features = [
"audio-worklet",
7 changes: 6 additions & 1 deletion tests/atomic_failure.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ mod util;
use futures_util::future;
use futures_util::future::Either;
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::OfflineAudioContext;
use web_sys::{console, OfflineAudioContext};
use web_thread::web::audio_worklet::BaseAudioContextExt;
use web_time::Instant;

@@ -17,6 +17,11 @@ use self::util::{Flag, MAX_DIFF};
/// Testing failure of [`Instant::now()`] in audio worklet.
#[wasm_bindgen_test]
async fn test() {
if web_sys::window().is_none() {
console::error_1(&"found ourselves not in a `Window`".into());
return;
}

let context =
OfflineAudioContext::new_with_number_of_channels_and_length_and_sample_rate(1, 1, 8000.)
.unwrap();
26 changes: 21 additions & 5 deletions tests/atomic_success.rs
Original file line number Diff line number Diff line change
@@ -7,13 +7,19 @@ mod util;

use futures_channel::oneshot;
use wasm_bindgen_test::wasm_bindgen_test;
use web_thread::web;
use web_sys::console;
use web_thread::web::{self, has_spawn_support};
use web_time::{Duration, Instant};

use self::util::{sleep, Flag, DIFF};
use self::util::{sleep, Flag, DIFF, WAIT};

#[wasm_bindgen_test]
async fn basic() {
if !has_spawn_support() {
console::error_1(&"can't spawn threads".into());
return;
}

let earlier = Instant::now();

let flag = Flag::new();
@@ -24,7 +30,7 @@ async fn basic() {
let later = Instant::now();
assert!(earlier <= later, "{:?}", earlier - later);

sleep(DIFF).await;
sleep(WAIT).await;

let later = Instant::now();
assert!((later - earlier) >= DIFF, "{:?}", later - earlier);
@@ -41,6 +47,11 @@ async fn basic() {

#[wasm_bindgen_test]
async fn delay() {
if !has_spawn_support() {
console::error_1(&"can't spawn threads".into());
return;
}

sleep(Duration::from_secs(2)).await;

let earlier = Instant::now();
@@ -53,7 +64,7 @@ async fn delay() {
let later = Instant::now();
assert!(earlier <= later, "{:?}", earlier - later);

sleep(DIFF).await;
sleep(WAIT).await;

let later = Instant::now();
assert!((later - earlier) >= DIFF, "{:?}", later - earlier);
@@ -70,14 +81,19 @@ async fn delay() {

#[wasm_bindgen_test]
async fn worker() {
if !has_spawn_support() {
console::error_1(&"can't spawn threads".into());
return;
}

let (sender, receiver) = oneshot::channel();
web::spawn_async(move || async move { sender.send(Instant::now()).unwrap() });

let earlier = receiver.await.unwrap();
let later = Instant::now();
assert!(earlier <= later, "{:?}", earlier - later);

sleep(DIFF).await;
sleep(WAIT).await;

let later = Instant::now();
assert!((later - earlier) >= DIFF, "{:?}", later - earlier);
4 changes: 2 additions & 2 deletions tests/instant_failure_1.rs
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@ mod util;
use wasm_bindgen_test::wasm_bindgen_test;
use web_time::{Duration, Instant};

use self::util::{sleep, DIFF};
use self::util::{sleep, WAIT};

/// [`Instant::add()`] failure.
#[wasm_bindgen_test(unsupported = pollster::test)]
#[should_panic = "overflow when adding duration to instant"]
async fn add_failure() {
sleep(DIFF).await;
sleep(WAIT).await;
let _ = Instant::now() + Duration::MAX;
}

4 changes: 2 additions & 2 deletions tests/instant_failure_2.rs
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@ mod util;
use wasm_bindgen_test::wasm_bindgen_test;
use web_time::{Duration, Instant};

use self::util::{sleep, DIFF};
use self::util::{sleep, WAIT};

/// [`Instant::add_assign()`] failure.
#[wasm_bindgen_test(unsupported = pollster::test)]
#[should_panic = "overflow when adding duration to instant"]
async fn add_assign_failure() {
sleep(DIFF).await;
sleep(WAIT).await;
let mut instant = Instant::now();
instant += Duration::MAX;
}
Loading

0 comments on commit 3ed3edb

Please sign in to comment.