Skip to content

Commit 9a2ae71

Browse files
authored
tracing: run tests on wasm32-unknown-unknown (tokio-rs#959)
## Motivation refs tokio-rs#642 Start Testing the core crate in a wasm environment. This requires a special runner, and an extra dev-dependency. All tests have been ported over except for `span::spans_always_go_to_the_subscriber_that_tagged_them_even_across_threads` because that spins up a thread, and wasm has no concept of threads being spun up. ## Solution Add a wasm-pack tester, and properly tag all tests.
1 parent 9ab7f6e commit 9a2ae71

20 files changed

+131
-1
lines changed

.github/workflows/CI.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ jobs:
166166
matrix:
167167
# TODO(securityinsanity): slowly add wasm32 test runner to each crate, and move to seperate actions that run tests.
168168
subcrate:
169-
- tracing
170169
- tracing-appender
171170
- tracing-attributes
172171
- tracing-core
@@ -192,6 +191,25 @@ jobs:
192191
command: test
193192
args: --no-run -p ${{ matrix.subcrate }}
194193

194+
test-wasm:
195+
needs: check
196+
runs-on: ubuntu-latest
197+
strategy:
198+
matrix:
199+
subcrate:
200+
- tracing
201+
steps:
202+
- uses: actions/checkout@main
203+
- uses: actions-rs/toolchain@v1
204+
with:
205+
target: wasm32-unknown-unknown
206+
toolchain: stable
207+
override: true
208+
- name: install test runner for wasm
209+
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
210+
- name: run wasm tests
211+
run: cd ${{ matrix.subcrate }} && wasm-pack test --node
212+
195213
test-os:
196214
# Test against stable Rust across macOS, Windows, and Linux.
197215
needs: check

tracing/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ futures = "0.1"
3737
criterion = { version = "0.3", default_features = false }
3838
log = "0.4"
3939

40+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
41+
wasm-bindgen-test = "^0.3"
42+
4043
[features]
4144
default = ["std", "attributes"]
4245

tracing/test-log-support/tests/log_no_trace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate test_log_support;
55
use test_log_support::Test;
66
use tracing::Level;
77

8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
89
#[test]
910
fn test_always_log() {
1011
let test = Test::start();

tracing/test-log-support/tests/log_with_trace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl tracing::Subscriber for NopSubscriber {
2626
}
2727
}
2828

29+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
2930
#[test]
3031
fn log_with_trace() {
3132
tracing::subscriber::set_global_default(NopSubscriber).expect("set global should succeed");

tracing/test-log-support/tests/span_activity_filtered_separately.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate test_log_support;
55
use test_log_support::Test;
66
use tracing::Level;
77

8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
89
#[test]
910
fn span_activity_filtered_separately() {
1011
let test = Test::with_filters(&[

tracing/test-log-support/tests/span_lifecycle_can_be_enabled.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate test_log_support;
55
use test_log_support::Test;
66
use tracing::Level;
77

8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
89
#[test]
910
fn span_lifecycle_can_be_enabled() {
1011
let test = Test::with_filters(&[

tracing/test-log-support/tests/span_lifecycle_defaults_off.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate test_log_support;
55
use test_log_support::Test;
66
use tracing::Level;
77

8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
89
#[test]
910
fn span_lifecycle_defaults_off() {
1011
let test = Test::with_filters(&[(module_path!(), log::LevelFilter::Trace)]);

tracing/test-log-support/tests/span_lifecycle_is_trace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate test_log_support;
55
use test_log_support::Test;
66
use tracing::Level;
77

8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
89
#[test]
910
fn span_lifecycle_is_trace() {
1011
let test = Test::with_filters(&[

tracing/test_static_max_level_features/tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Subscriber for TestSubscriber {
3333
fn exit(&self, _span: &Id) {}
3434
}
3535

36+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
3637
#[test]
3738
fn test_static_max_level_features() {
3839
let me = Arc::new(State {

tracing/tests/event.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use tracing::{
2020

2121
macro_rules! event_without_message {
2222
($name:ident: $e:expr) => {
23+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
2324
#[test]
2425
fn $name() {
2526
let (subscriber, handle) = subscriber::mock()
@@ -55,6 +56,7 @@ event_without_message! {nonzeroi32_event_without_message: std::num::NonZeroI32::
5556
// needs API breakage
5657
//event_without_message!{nonzerou128_event_without_message: std::num::NonZeroU128::new(42).unwrap()}
5758

59+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5860
#[test]
5961
fn event_with_message() {
6062
let (subscriber, handle) = subscriber::mock()
@@ -71,6 +73,7 @@ fn event_with_message() {
7173
handle.assert_finished();
7274
}
7375

76+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
7477
#[test]
7578
fn message_without_delims() {
7679
let (subscriber, handle) = subscriber::mock()
@@ -99,6 +102,7 @@ fn message_without_delims() {
99102
handle.assert_finished();
100103
}
101104

105+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
102106
#[test]
103107
fn string_message_without_delims() {
104108
let (subscriber, handle) = subscriber::mock()
@@ -126,6 +130,7 @@ fn string_message_without_delims() {
126130
handle.assert_finished();
127131
}
128132

133+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
129134
#[test]
130135
fn one_with_everything() {
131136
let (subscriber, handle) = subscriber::mock()
@@ -160,6 +165,7 @@ fn one_with_everything() {
160165
handle.assert_finished();
161166
}
162167

168+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
163169
#[test]
164170
fn moved_field() {
165171
let (subscriber, handle) = subscriber::mock()
@@ -180,6 +186,7 @@ fn moved_field() {
180186
handle.assert_finished();
181187
}
182188

189+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
183190
#[test]
184191
fn dotted_field_name() {
185192
let (subscriber, handle) = subscriber::mock()
@@ -200,6 +207,7 @@ fn dotted_field_name() {
200207
handle.assert_finished();
201208
}
202209

210+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
203211
#[test]
204212
fn borrowed_field() {
205213
let (subscriber, handle) = subscriber::mock()
@@ -222,6 +230,7 @@ fn borrowed_field() {
222230
handle.assert_finished();
223231
}
224232

233+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
225234
#[test]
226235
// If emitting log instrumentation, this gets moved anyway, breaking the test.
227236
#[cfg(not(feature = "log"))]
@@ -262,6 +271,7 @@ fn move_field_out_of_struct() {
262271
handle.assert_finished();
263272
}
264273

274+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
265275
#[test]
266276
fn display_shorthand() {
267277
let (subscriber, handle) = subscriber::mock()
@@ -281,6 +291,7 @@ fn display_shorthand() {
281291
handle.assert_finished();
282292
}
283293

294+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
284295
#[test]
285296
fn debug_shorthand() {
286297
let (subscriber, handle) = subscriber::mock()
@@ -300,6 +311,7 @@ fn debug_shorthand() {
300311
handle.assert_finished();
301312
}
302313

314+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
303315
#[test]
304316
fn both_shorthands() {
305317
let (subscriber, handle) = subscriber::mock()
@@ -320,6 +332,7 @@ fn both_shorthands() {
320332
handle.assert_finished();
321333
}
322334

335+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
323336
#[test]
324337
fn explicit_child() {
325338
let (subscriber, handle) = subscriber::mock()
@@ -336,6 +349,7 @@ fn explicit_child() {
336349
handle.assert_finished();
337350
}
338351

352+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
339353
#[test]
340354
fn explicit_child_at_levels() {
341355
let (subscriber, handle) = subscriber::mock()

0 commit comments

Comments
 (0)