Skip to content

Commit 374dc19

Browse files
committed
test: wait until serve is ready
1 parent 53c4a2e commit 374dc19

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tests/componentize.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
io::Write,
2+
io::{Read, Write},
33
path::{Path, PathBuf},
44
process::Stdio,
55
thread::sleep,
@@ -77,8 +77,20 @@ fn http_example() -> anyhow::Result<()> {
7777
let mut handle = std::process::Command::new("wasmtime")
7878
.current_dir(&path)
7979
.args(["serve", "--wasi", "common", "http.wasm"])
80+
.stdout(Stdio::piped())
8081
.spawn()?;
8182

83+
let mut buf = [0; 64];
84+
let mut stdout = handle.stdout.take().unwrap();
85+
86+
// Read at least one byte from stdout
87+
retry(|| -> anyhow::Result<()> {
88+
if buf.is_empty() && stdout.read(&mut buf)? == 0 {
89+
return Err(anyhow::anyhow!("No data"));
90+
}
91+
Ok(())
92+
})?;
93+
8294
let content = "’Twas brillig, and the slithy toves
8395
Did gyre and gimble in the wabe:
8496
All mimsy were the borogoves,
@@ -272,8 +284,8 @@ fn tcp_example() -> anyhow::Result<()> {
272284
Ok(())
273285
}
274286

275-
fn retry<T>(func: impl Fn() -> anyhow::Result<T>) -> anyhow::Result<T> {
276-
for i in 0..10 {
287+
fn retry<T>(mut func: impl FnMut() -> anyhow::Result<T>) -> anyhow::Result<T> {
288+
for i in 0..5 {
277289
match func() {
278290
Ok(t) => {
279291
return Ok(t);

0 commit comments

Comments
 (0)