Skip to content

Commit 692935b

Browse files
committed
Add wasip2 tests
1 parent 5d14b6d commit 692935b

File tree

8 files changed

+88
-12
lines changed

8 files changed

+88
-12
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
**/target
22
**/*.rs.bk
33
Cargo.lock
4-
.cargo
4+
/.cargo
55
.config

Justfile

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
# Builds `tonic-web-wasm-client`
1+
# Builds `tonic-web-wasm-client` for `wasm32-unknown-unknown` target
22
build:
33
@echo 'Building...'
44
cargo build --target wasm32-unknown-unknown
55

6-
# Builds test `tonic-web` server
6+
# Builds `tonic-web-wasm-client` for `wasm32-wasip2` target
7+
build-wasip2:
8+
@echo 'Building...'
9+
cargo build --target wasm32-wasip2
10+
11+
# Builds test `tonic-web` server natively
712
build-test-server:
813
@echo 'Building test server...'
914
cd test-suite/simple/server && cargo build
@@ -23,12 +28,29 @@ test-headless:
2328
@echo 'Testing...'
2429
cd test-suite/simple/client && wasm-pack test --headless --chrome
2530

26-
# Builds test `tonic-web` server (with compression enabled: gzip)
31+
# Checks wasmtime version, needed only for -wasip2 recipes
32+
check-wasmtime-version:
33+
#!/usr/bin/env bash
34+
set -euo pipefail
35+
version_output=$(wasmtime --version)
36+
current_version=$(echo "${version_output}" | awk '{print $2}')
37+
required_version="35.0.0"
38+
if ! printf '%s\n' "${required_version}" "${current_version}" | sort --check=silent --version-sort; then
39+
echo "Error: wasmtime version ${current_version} is installed."
40+
echo "Version 35.0.0 or greater is required."
41+
exit 1
42+
fi
43+
44+
# Runs wasip2 tests in `wasmtime`
45+
test-wasip2: check-wasmtime-version
46+
cd test-suite/simple/client && cargo test --target wasm32-wasip2
47+
48+
# Builds test `tonic-web` server (with compression enabled: gzip) for `wasm32-unknown-unknown` target
2749
build-gzip-test-server:
2850
@echo 'Building test server...'
2951
cd test-suite/gzip/server && cargo build
3052

31-
# Starts test `tonic-web` server (with compression enabled: gzip)
53+
# Starts test `tonic-web` server (with compression enabled: gzip) natively
3254
start-gzip-test-server:
3355
@echo 'Starting test server...'
3456
cd test-suite/gzip/server && cargo run
@@ -42,3 +64,9 @@ test-gzip:
4264
test-gzip-headless:
4365
@echo 'Testing...'
4466
cd test-suite/gzip/client && wasm-pack test --headless --chrome
67+
68+
# Runs wasip2 tests (with compression enabled: gzip) in `wasmtime`
69+
test-gzip-wasip2: check-wasmtime-version
70+
@echo 'Testing...'
71+
cd test-suite/gzip/client && cargo test --target wasm32-wasip2
72+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-wasip2]
2+
runner = "wasmtime run -Scli -Shttp" # Runs wasip2 tests in wasmtime

test-suite/gzip/client/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ tonic-prost-build = { version = "0.14", default-features = false }
1515

1616
[dev-dependencies]
1717
tonic-web-wasm-client = { path = "../../.." }
18+
19+
# Browser-specific dependencies
20+
[target.wasm32-unknown-unknown.dev-dependencies]
1821
wasm-bindgen-test = "0.3"
22+
23+
[target.wasm32-wasip2.dependencies]
24+
wstd = "0.5"

test-suite/gzip/client/tests/web.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use client::proto::{echo_client::EchoClient, EchoRequest};
22
use tonic::codegen::CompressionEncoding;
33
use tonic_web_wasm_client::Client;
4+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
45
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
56

7+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
68
wasm_bindgen_test_configure!(run_in_browser);
79

810
fn build_client() -> EchoClient<Client> {
@@ -12,7 +14,11 @@ fn build_client() -> EchoClient<Client> {
1214
EchoClient::new(wasm_client).accept_compressed(CompressionEncoding::Gzip)
1315
}
1416

15-
#[wasm_bindgen_test]
17+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
18+
#[cfg_attr(
19+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
20+
wstd::test
21+
)]
1622
async fn test_echo() {
1723
let mut client = build_client();
1824

@@ -27,7 +33,11 @@ async fn test_echo() {
2733
assert_eq!(response.message, "echo(John)");
2834
}
2935

30-
#[wasm_bindgen_test]
36+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
37+
#[cfg_attr(
38+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
39+
wstd::test
40+
)]
3141
async fn test_echo_stream() {
3242
let mut client = build_client();
3343

@@ -51,7 +61,11 @@ async fn test_echo_stream() {
5161
assert!(response.is_none());
5262
}
5363

54-
#[wasm_bindgen_test]
64+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
65+
#[cfg_attr(
66+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
67+
wstd::test
68+
)]
5569
async fn test_infinite_echo_stream() {
5670
let mut client = build_client();
5771

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-wasip2]
2+
runner = "wasmtime run -Scli -Shttp" # Runs wasip2 tests in wasmtime

test-suite/simple/client/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ tonic-prost-build = { version = "0.14", default-features = false }
1515

1616
[dev-dependencies]
1717
tonic-web-wasm-client = { path = "../../.." }
18+
19+
# Browser-specific dependencies
20+
[target.wasm32-unknown-unknown.dev-dependencies]
1821
wasm-bindgen-test = "0.3"
22+
23+
[target.wasm32-wasip2.dependencies]
24+
wstd = "0.5"

test-suite/simple/client/tests/web.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use client::proto::{echo_client::EchoClient, EchoRequest};
22
use tonic::Code;
33
use tonic_web_wasm_client::Client;
4+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
45
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
56

7+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
68
wasm_bindgen_test_configure!(run_in_browser);
79

810
fn build_client() -> EchoClient<Client> {
@@ -12,7 +14,11 @@ fn build_client() -> EchoClient<Client> {
1214
EchoClient::new(wasm_client)
1315
}
1416

15-
#[wasm_bindgen_test]
17+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
18+
#[cfg_attr(
19+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
20+
wstd::test
21+
)]
1622
async fn test_echo() {
1723
let mut client = build_client();
1824

@@ -27,7 +33,11 @@ async fn test_echo() {
2733
assert_eq!(response.message, "echo(John)");
2834
}
2935

30-
#[wasm_bindgen_test]
36+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
37+
#[cfg_attr(
38+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
39+
wstd::test
40+
)]
3141
async fn test_echo_stream() {
3242
let mut client = build_client();
3343

@@ -51,7 +61,11 @@ async fn test_echo_stream() {
5161
assert!(response.is_none());
5262
}
5363

54-
#[wasm_bindgen_test]
64+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
65+
#[cfg_attr(
66+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
67+
wstd::test
68+
)]
5569
async fn test_infinite_echo_stream() {
5670
let mut client = build_client();
5771

@@ -75,7 +89,11 @@ async fn test_infinite_echo_stream() {
7589
assert!(response.is_some());
7690
}
7791

78-
#[wasm_bindgen_test]
92+
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
93+
#[cfg_attr(
94+
all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
95+
wstd::test
96+
)]
7997
async fn test_error_response() {
8098
let mut client = build_client();
8199

0 commit comments

Comments
 (0)