Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit ee38a52

Browse files
authored
sp-runtime-interface-test: Fix flaky test (#13770)
1 parent 4c7866a commit ee38a52

File tree

1 file changed

+64
-41
lines changed
  • primitives/runtime-interface/test/src

1 file changed

+64
-41
lines changed

primitives/runtime-interface/test/src/lib.rs

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -176,62 +176,85 @@ fn test_versionining_register_only() {
176176
call_wasm_method::<HostFunctions>(wasm_binary_unwrap(), "test_versionning_register_only_works");
177177
}
178178

179+
fn run_test_in_another_process(
180+
test_name: &str,
181+
test_body: impl FnOnce(),
182+
) -> Option<std::process::Output> {
183+
if std::env::var("RUN_FORKED_TEST").is_ok() {
184+
test_body();
185+
None
186+
} else {
187+
let output = std::process::Command::new(std::env::current_exe().unwrap())
188+
.arg(test_name)
189+
.env("RUN_FORKED_TEST", "1")
190+
.output()
191+
.unwrap();
192+
193+
assert!(output.status.success());
194+
Some(output)
195+
}
196+
}
197+
179198
#[test]
180199
fn test_tracing() {
181-
use std::fmt;
182-
use tracing::span::Id as SpanId;
183-
use tracing_core::field::{Field, Visit};
184-
185-
#[derive(Clone)]
186-
struct TracingSubscriber(Arc<Mutex<Inner>>);
187-
188-
struct FieldConsumer(&'static str, Option<String>);
189-
impl Visit for FieldConsumer {
190-
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
191-
if field.name() == self.0 {
192-
self.1 = Some(format!("{:?}", value))
200+
// Run in a different process to ensure that the `Span` is registered with our local
201+
// `TracingSubscriber`.
202+
run_test_in_another_process("test_tracing", || {
203+
use std::fmt;
204+
use tracing::span::Id as SpanId;
205+
use tracing_core::field::{Field, Visit};
206+
207+
#[derive(Clone)]
208+
struct TracingSubscriber(Arc<Mutex<Inner>>);
209+
210+
struct FieldConsumer(&'static str, Option<String>);
211+
impl Visit for FieldConsumer {
212+
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
213+
if field.name() == self.0 {
214+
self.1 = Some(format!("{:?}", value))
215+
}
193216
}
194217
}
195-
}
196218

197-
#[derive(Default)]
198-
struct Inner {
199-
spans: HashSet<String>,
200-
}
201-
202-
impl tracing::subscriber::Subscriber for TracingSubscriber {
203-
fn enabled(&self, _: &tracing::Metadata) -> bool {
204-
true
219+
#[derive(Default)]
220+
struct Inner {
221+
spans: HashSet<String>,
205222
}
206223

207-
fn new_span(&self, span: &tracing::span::Attributes) -> tracing::Id {
208-
let mut inner = self.0.lock().unwrap();
209-
let id = SpanId::from_u64((inner.spans.len() + 1) as _);
210-
let mut f = FieldConsumer("name", None);
211-
span.record(&mut f);
212-
inner.spans.insert(f.1.unwrap_or_else(|| span.metadata().name().to_owned()));
213-
id
214-
}
224+
impl tracing::subscriber::Subscriber for TracingSubscriber {
225+
fn enabled(&self, _: &tracing::Metadata) -> bool {
226+
true
227+
}
215228

216-
fn record(&self, _: &SpanId, _: &tracing::span::Record) {}
229+
fn new_span(&self, span: &tracing::span::Attributes) -> tracing::Id {
230+
let mut inner = self.0.lock().unwrap();
231+
let id = SpanId::from_u64((inner.spans.len() + 1) as _);
232+
let mut f = FieldConsumer("name", None);
233+
span.record(&mut f);
234+
inner.spans.insert(f.1.unwrap_or_else(|| span.metadata().name().to_owned()));
235+
id
236+
}
217237

218-
fn record_follows_from(&self, _: &SpanId, _: &SpanId) {}
238+
fn record(&self, _: &SpanId, _: &tracing::span::Record) {}
219239

220-
fn event(&self, _: &tracing::Event) {}
240+
fn record_follows_from(&self, _: &SpanId, _: &SpanId) {}
221241

222-
fn enter(&self, _: &SpanId) {}
242+
fn event(&self, _: &tracing::Event) {}
223243

224-
fn exit(&self, _: &SpanId) {}
225-
}
244+
fn enter(&self, _: &SpanId) {}
226245

227-
let subscriber = TracingSubscriber(Default::default());
228-
let _guard = tracing::subscriber::set_default(subscriber.clone());
246+
fn exit(&self, _: &SpanId) {}
247+
}
229248

230-
// Call some method to generate a trace
231-
call_wasm_method::<HostFunctions>(wasm_binary_unwrap(), "test_return_data");
249+
let subscriber = TracingSubscriber(Default::default());
250+
let _guard = tracing::subscriber::set_default(subscriber.clone());
251+
252+
// Call some method to generate a trace
253+
call_wasm_method::<HostFunctions>(wasm_binary_unwrap(), "test_return_data");
232254

233-
let inner = subscriber.0.lock().unwrap();
234-
assert!(inner.spans.contains("return_input_version_1"));
255+
let inner = subscriber.0.lock().unwrap();
256+
assert!(inner.spans.contains("return_input_version_1"));
257+
});
235258
}
236259

237260
#[test]

0 commit comments

Comments
 (0)