1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
test_r::enable!();
use crate::common::js_subtest_parser::{
BlockInfo, SubtestDiscovery, discover_subtests, rewrite_for_block, rewrite_for_node_test,
};
use crate::common::{
CompiledTest, GolemPreparedComponent, TestInstance, load_node_compat_config,
setup_node_compat_test_files,
};
use camino::Utf8Path;
use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use test_r::core::{DynamicTestRegistration, TestProperties};
use test_r::{test_dep, test_gen};
use tokio::time::timeout;
use wasmtime::component::Val;
#[allow(dead_code)]
#[path = "common/mod.rs"]
mod common;
struct FullPreparedComponent(Arc<GolemPreparedComponent>);
/// Parent compiles the node-compat wrapper crate exactly once and ships
/// the wasm path to every worker via test-r's `Cloneable` scope. The
/// expensive piece (cargo build of the wrapper crate) therefore runs a
/// single time per suite even with `--test-threads N` and capture on.
#[test_dep(tagged_as = "node_compat_full_compiled", scope = Cloneable)]
async fn compiled_node_compat_full() -> CompiledTest {
let path = Utf8Path::new("examples/runtime/node-compat-runner");
CompiledTest::new_with_features(
path,
true,
common::FeatureCombination::FullNoLoggingWithGolem,
)
.await
.expect("Failed to compile node-compat-runner")
}
/// Each worker materialises its own `FullPreparedComponent` (which owns
/// the per-worker `wasmtime::Engine`, `Linker`, `Component`, and
/// epoch-ticker thread) from the parent-shipped wasm path. This is
/// `PerWorker` rather than `Shared` so it does not re-trigger the
/// single-threaded fallback that `Shared` deps cause under capture.
#[test_dep(scope = PerWorker)]
fn prepare_node_compat_full(
#[tagged_as("node_compat_full_compiled")] compiled: &CompiledTest,
) -> Arc<FullPreparedComponent> {
Arc::new(FullPreparedComponent(Arc::new(
GolemPreparedComponent::new(compiled.wasm_path()).expect("Failed to prepare component"),
)))
}
// --- Helper types and functions ---
/// Cloneable representation of discovery data for use in test closures.
#[derive(Clone)]
enum DiscoveryData {
Block(Vec<BlockInfo>),
NodeTest,
}
fn handle_test_result(
result: anyhow::Result<Option<Val>>,
stdout: &str,
stderr: &str,
) -> anyhow::Result<()> {
match result {
Ok(Some(Val::String(ref s))) if s.starts_with("PASS") => Ok(()),
Ok(Some(Val::String(ref s))) if s.starts_with("SKIP:") => Ok(()),
Ok(Some(Val::String(ref s))) => {
anyhow::bail!(
"Test failed: {}\n[stdout]\n{}\n[stderr]\n{}",
s,
stdout.trim(),
stderr.trim()
)
}
Ok(other) => {
anyhow::bail!(
"Unexpected return: {:?}\n[stdout]\n{}\n[stderr]\n{}",
other,
stdout.trim(),
stderr.trim()
)
}
Err(e) => {
anyhow::bail!(
"Invocation error: {}\n[stdout]\n{}\n[stderr]\n{}",
e,
stdout.trim(),
stderr.trim()
)
}
}
}
// --- Flaky test retry support ---
/// Maximum number of attempts for tests marked `"flaky": true` in
/// `tests/node_compat/config.jsonc`. A flaky test passes if any single attempt
/// passes; it only fails after all `FLAKY_MAX_ATTEMPTS` attempts fail.
const FLAKY_MAX_ATTEMPTS: u32 = 10;
// --- Shard tagging for CI parallelism ---
const NUM_SHARDS: u64 = 8;
fn shard_tag(name: &str) -> String {
let mut hasher = DefaultHasher::new();
name.hash(&mut hasher);
format!("shard{}", hasher.finish() % NUM_SHARDS)
}
// --- Dynamic test generation ---
#[test_gen]
fn gen_node_compat_tests(r: &mut DynamicTestRegistration) {
let entries =
load_node_compat_config("tests/node_compat/config.jsonc").expect("Failed to load config");
let dependency_name = "arc_fullpreparedcomponent".to_string();
for entry in entries {
let path = entry.path.clone();
let file_test_name = path.replace('/', "__").replace(['.', '-'], "_");
let test_timeout_secs = entry.timeout_secs;
if !entry.split || entry.subtests.is_empty() {
// Non-split: one Rust test per file (unchanged behavior)
let props = TestProperties {
is_ignored: entry.category.should_ignore_in_runner(),
tags: vec![shard_tag(&file_test_name)],
..TestProperties::unit_test()
};
let entry_flaky = entry.flaky;
r.add_async_test(
file_test_name,
props,
Some(vec![dependency_name.clone()]),
move |deps| {
let prepared: Arc<Arc<FullPreparedComponent>> = deps
.get("arc_fullpreparedcomponent")
.expect("FullPreparedComponent dependency not found")
.downcast::<Arc<FullPreparedComponent>>()
.expect("FullPreparedComponent type mismatch");
let prepared = prepared.as_ref().as_ref().0.clone();
let path = path.clone();
Box::pin(async move {
let max_attempts = if entry_flaky { FLAKY_MAX_ATTEMPTS } else { 1 };
let mut last_err: Option<anyhow::Error> = None;
for attempt in 1..=max_attempts {
let prepared = prepared.clone();
let path = path.clone();
let test_future = async {
let mut instance =
TestInstance::from_golem_prepared(&prepared).await?;
instance.set_epoch_deadline(test_timeout_secs);
setup_node_compat_test_files(instance.temp_dir_path(), &path)?;
let guest_path = format!("/home/node/test/{}", path);
let (result, stdout, stderr) = instance
.invoke_and_capture_output_with_stderr(
None,
"run-test",
&[Val::String(guest_path)],
)
.await;
handle_test_result(result, &stdout, &stderr)
};
let attempt_result =
match timeout(Duration::from_secs(test_timeout_secs), test_future)
.await
{
Ok(result) => result,
Err(_) => Err(anyhow::anyhow!(
"Test timed out after {}s",
test_timeout_secs
)),
};
match attempt_result {
Ok(()) => return Ok(()),
Err(e) => {
if entry_flaky && attempt < max_attempts {
eprintln!(
"Flaky test attempt {}/{} failed, retrying: {}",
attempt, max_attempts, e
);
}
last_err = Some(e);
}
}
}
Err(last_err.unwrap_or_else(|| {
anyhow::anyhow!("Test failed with no recorded error")
}))
})
},
);
} else {
// Split: one Rust test per subtest
let suite_path = format!("tests/node_compat/suite/{}", path);
let source = match fs::read_to_string(&suite_path) {
Ok(s) => s,
Err(e) => {
eprintln!("WARNING: Cannot read split test file {}: {}", suite_path, e);
continue;
}
};
let discovery = discover_subtests(&path, &source);
// Staleness check: compare discovered subtest count vs config count
let discovered_count = match &discovery {
SubtestDiscovery::None => 0,
SubtestDiscovery::Block(blocks) => blocks.len(),
SubtestDiscovery::NodeTest(tests) => tests.len(),
};
if discovered_count != entry.subtests.len() {
eprintln!(
"WARNING: Subtest count mismatch for {}: config has {}, discovered {}. Run migration tool.",
path,
entry.subtests.len(),
discovered_count
);
}
for subtest in &entry.subtests {
let test_name = format!("{}__{}", file_test_name, subtest.name);
let is_ignored = subtest.category.should_ignore_in_runner();
let props = TestProperties {
is_ignored,
tags: vec![shard_tag(&test_name)],
..TestProperties::unit_test()
};
let path = path.clone();
let subtest_index = subtest.index;
let source = source.clone();
let discovery_clone = match &discovery {
SubtestDiscovery::None => None,
SubtestDiscovery::Block(blocks) => Some(DiscoveryData::Block(blocks.clone())),
SubtestDiscovery::NodeTest(_) => Some(DiscoveryData::NodeTest),
};
let subtest_flaky = subtest.flaky;
r.add_async_test(
test_name,
props,
Some(vec![dependency_name.clone()]),
move |deps| {
let prepared: Arc<Arc<FullPreparedComponent>> = deps
.get("arc_fullpreparedcomponent")
.expect("FullPreparedComponent dependency not found")
.downcast::<Arc<FullPreparedComponent>>()
.expect("FullPreparedComponent type mismatch");
let prepared = prepared.as_ref().as_ref().0.clone();
let path = path.clone();
let source = source.clone();
let discovery_clone = discovery_clone.clone();
Box::pin(async move {
let max_attempts =
if subtest_flaky { FLAKY_MAX_ATTEMPTS } else { 1 };
let mut last_err: Option<anyhow::Error> = None;
for attempt in 1..=max_attempts {
let prepared = prepared.clone();
let path = path.clone();
let source = source.clone();
let discovery_clone = discovery_clone.clone();
let attempt_result = async {
let mut instance =
TestInstance::from_golem_prepared(&prepared).await?;
instance.set_epoch_deadline(test_timeout_secs);
setup_node_compat_test_files(instance.temp_dir_path(), &path)?;
// Rewrite the test file to isolate the target subtest
let rewritten = match &discovery_clone {
Some(DiscoveryData::Block(blocks)) => {
rewrite_for_block(&source, blocks, subtest_index)
}
Some(DiscoveryData::NodeTest) => {
rewrite_for_node_test(&source, subtest_index)
}
None => source.clone(),
};
// Write the rewritten file to the temp dir
let test_filename = path.rsplit('/').next().unwrap_or(&path);
let suite = path.split('/').next().unwrap_or("parallel");
let rewritten_path = instance
.temp_dir_path()
.join("home")
.join("node")
.join("test")
.join(suite)
.join(test_filename);
fs::write(&rewritten_path, &rewritten)?;
let guest_path = format!("/home/node/test/{}", path);
let test_future = async {
let (result, stdout, stderr) = instance
.invoke_and_capture_output_with_stderr(
None,
"run-test",
&[Val::String(guest_path)],
)
.await;
handle_test_result(result, &stdout, &stderr)
};
match timeout(
Duration::from_secs(test_timeout_secs),
test_future,
)
.await
{
Ok(result) => result,
Err(_) => {
let stdout = instance.read_stdout().unwrap_or_default();
let stderr = instance.read_stderr().unwrap_or_default();
anyhow::bail!(
"Test timed out after {}s\n[stdout]\n{}\n[stderr]\n{}",
test_timeout_secs,
stdout.trim(),
stderr.trim()
)
}
}
}
.await;
match attempt_result {
Ok(()) => return Ok(()),
Err(e) => {
if subtest_flaky && attempt < max_attempts {
eprintln!(
"Flaky test attempt {}/{} failed, retrying: {}",
attempt, max_attempts, e
);
}
last_err = Some(e);
}
}
}
Err(last_err.unwrap_or_else(|| {
anyhow::anyhow!("Test failed with no recorded error")
}))
})
},
);
}
}
}
}