Skip to content

Commit 9debe50

Browse files
committed
clocks: Introduce the clocks module
This helps alleviate the repetition across clocks tests, namely: `monotonic-clock`, `wall-clock` and `multi-clock`
1 parent ed92cf9 commit 9debe50

File tree

5 files changed

+61
-72
lines changed

5 files changed

+61
-72
lines changed

tests/rust/wasm32-wasip3/src/bin/monotonic-clock.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
extern crate wit_bindgen;
2-
3-
wit_bindgen::generate!({
4-
inline: r"
5-
package test:test;
6-
7-
world test {
8-
include wasi:clocks/imports@0.3.0-rc-2026-01-06;
9-
include wasi:cli/command@0.3.0-rc-2026-01-06;
10-
}
11-
",
12-
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
13-
features:["clocks-timezone"],
14-
generate_all
15-
});
16-
17-
use monotonic_clock::{Duration, Mark};
18-
use wasi::clocks::monotonic_clock;
19-
20-
const NANOSECOND: Duration = 1;
21-
const MICROSECOND: Duration = NANOSECOND * 1_000;
22-
const MILLISECOND: Duration = MICROSECOND * 1_000;
23-
const SECOND: Duration = MILLISECOND * 1_000;
24-
const MINUTE: Duration = SECOND * 60;
25-
const HOUR: Duration = MINUTE * 60;
26-
const DAY: Duration = HOUR * 24;
1+
use test_wasm32_wasip3::clocks::{
2+
DAY, MILLISECOND, export,
3+
exports::wasi::cli::run::Guest,
4+
wasi::clocks::monotonic_clock::{self, Duration, Mark},
5+
};
276

287
fn compute_duration(start: Mark, end: Mark) -> Duration {
298
// Assume that this test takes less than a day to run (in terms of
@@ -64,7 +43,7 @@ fn test_resolution() {
6443

6544
struct Component;
6645
export!(Component);
67-
impl exports::wasi::cli::run::Guest for Component {
46+
impl Guest for Component {
6847
async fn run() -> Result<(), ()> {
6948
test_wait_for().await;
7049
test_wait_until().await;

tests/rust/wasm32-wasip3/src/bin/multi-clock-wait.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
extern crate wit_bindgen;
2-
3-
wit_bindgen::generate!({
4-
inline: r"
5-
package test:test;
6-
7-
world test {
8-
include wasi:clocks/imports@0.3.0-rc-2026-01-06;
9-
include wasi:cli/command@0.3.0-rc-2026-01-06;
10-
}
11-
",
12-
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
13-
features:["clocks-timezone"],
14-
generate_all
15-
});
16-
171
use core::task::{Context, Poll, Waker};
18-
use monotonic_clock::Mark;
192
use std::future::Future;
20-
use wasi::clocks::monotonic_clock;
3+
use test_wasm32_wasip3::clocks::{
4+
export,
5+
exports::wasi::cli::run::Guest,
6+
wasi::clocks::monotonic_clock::{self, Mark},
7+
};
218

229
// Offsets relative to "now" at which to wait_until(), in nanoseconds.
2310
// These are 20 values chosen uniformly randomly over the range [-5
@@ -71,7 +58,8 @@ async fn test_multi_clock_wait() {
7158

7259
struct Component;
7360
export!(Component);
74-
impl exports::wasi::cli::run::Guest for Component {
61+
62+
impl Guest for Component {
7563
async fn run() -> Result<(), ()> {
7664
test_multi_clock_wait().await;
7765
Ok(())
Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
extern crate wit_bindgen;
2-
3-
wit_bindgen::generate!({
4-
inline: r"
5-
package test:test;
6-
7-
world test {
8-
include wasi:clocks/imports@0.3.0-rc-2026-01-06;
9-
}
10-
",
11-
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
12-
features:["clocks-timezone"],
13-
generate_all
14-
});
15-
16-
use wasi::clocks::system_clock;
1+
use test_wasm32_wasip3::clocks::{
2+
export, exports::wasi::cli::run::Guest, wasi::clocks::system_clock,
3+
};
174

185
const NANOSECOND: u32 = 1;
196
const MICROSECOND: u32 = NANOSECOND * 1_000;
@@ -25,14 +12,24 @@ fn verify_instant(t: system_clock::Instant) {
2512
}
2613

2714
fn main() {
28-
// Not much we can assert about system-clock time.
29-
verify_instant(system_clock::now());
30-
let resolution = system_clock::get_resolution();
31-
let resolution_instant = system_clock::Instant {
32-
seconds: 0,
33-
nanoseconds: resolution as u32,
34-
};
35-
verify_instant(resolution_instant);
36-
// Resolution should be non-zero and represent the clock's precision.
37-
assert!(resolution > 0, "Clock resolution should be non-zero");
15+
unreachable!();
16+
}
17+
18+
struct Component;
19+
export!(Component);
20+
21+
impl Guest for Component {
22+
async fn run() -> Result<(), ()> {
23+
// Not much we can assert about system-clock time.
24+
verify_instant(system_clock::now());
25+
let resolution = system_clock::get_resolution();
26+
let resolution_instant = system_clock::Instant {
27+
seconds: 0,
28+
nanoseconds: resolution as u32,
29+
};
30+
verify_instant(resolution_instant);
31+
// Resolution should be non-zero and represent the clock's precision.
32+
assert!(resolution > 0, "Clock resolution should be non-zero");
33+
Ok(())
34+
}
3835
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
wit_bindgen::generate!({
2+
inline: r"
3+
package wasi-testsuite:test;
4+
5+
world test {
6+
include wasi:clocks/imports@0.3.0-rc-2026-01-06;
7+
include wasi:cli/command@0.3.0-rc-2026-01-06;
8+
}
9+
",
10+
features:["clocks-timezone"],
11+
pub_export_macro: true,
12+
default_bindings_module: "test_wasm32_wasip3::clocks",
13+
generate_all
14+
});
15+
16+
use self::wasi::clocks::monotonic_clock::Duration;
17+
18+
pub const NANOSECOND: Duration = 1;
19+
pub const MICROSECOND: Duration = NANOSECOND * 1_000;
20+
pub const MILLISECOND: Duration = MICROSECOND * 1_000;
21+
pub const SECOND: Duration = MILLISECOND * 1_000;
22+
pub const MINUTE: Duration = SECOND * 60;
23+
pub const HOUR: Duration = MINUTE * 60;
24+
pub const DAY: Duration = HOUR * 24;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod clocks;

0 commit comments

Comments
 (0)