Skip to content

Commit e2a0e29

Browse files
committed
Refactor TCP test to use TCP Listener instead of netcat
1 parent a8f38a8 commit e2a0e29

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

tests/componentize.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,6 @@ All mimsy were the borogoves,
8787

8888
let client = reqwest::blocking::Client::new();
8989

90-
fn retry(func: impl Fn() -> anyhow::Result<String>) -> anyhow::Result<String> {
91-
for i in 0..5 {
92-
match func() {
93-
Ok(text) => {
94-
return Ok(text);
95-
}
96-
Err(err) => {
97-
if i == 4 {
98-
return Err(err.into());
99-
} else {
100-
sleep(Duration::from_secs(1));
101-
continue;
102-
}
103-
}
104-
}
105-
}
106-
unreachable!()
107-
}
108-
10990
let echo = || -> anyhow::Result<String> {
11091
Ok(client
11192
.post("http://127.0.0.1:8080/echo")
@@ -238,7 +219,6 @@ fn sandbox_example() -> anyhow::Result<()> {
238219
Ok(())
239220
}
240221

241-
#[cfg(unix)]
242222
#[test]
243223
fn tcp_example() -> anyhow::Result<()> {
244224
let dir = tempfile::tempdir()?;
@@ -265,11 +245,7 @@ fn tcp_example() -> anyhow::Result<()> {
265245
.success()
266246
.stdout("Component built successfully\n");
267247

268-
let mut nc_handle = std::process::Command::new("nc")
269-
.current_dir(&path)
270-
.args(["-l", "127.0.0.1", "3456"])
271-
.stdin(Stdio::piped())
272-
.spawn()?;
248+
let listener = std::net::TcpListener::bind("127.0.0.1:3456")?;
273249

274250
let tcp_handle = std::process::Command::new("wasmtime")
275251
.current_dir(&path)
@@ -283,12 +259,10 @@ fn tcp_example() -> anyhow::Result<()> {
283259
.stdout(Stdio::piped())
284260
.spawn()?;
285261

286-
let mut nc_std_in = nc_handle.stdin.take().unwrap();
287-
288-
nc_std_in.write_all(b"hello")?;
262+
let (mut stream, _) = listener.accept()?;
263+
stream.write_all(b"hello")?;
289264

290265
let output = tcp_handle.wait_with_output()?;
291-
nc_handle.kill()?;
292266

293267
assert_eq!(
294268
String::from_utf8_lossy(&output.stdout),
@@ -298,6 +272,25 @@ fn tcp_example() -> anyhow::Result<()> {
298272
Ok(())
299273
}
300274

275+
fn retry<T>(func: impl Fn() -> anyhow::Result<T>) -> anyhow::Result<T> {
276+
for i in 0..5 {
277+
match func() {
278+
Ok(t) => {
279+
return Ok(t);
280+
}
281+
Err(err) => {
282+
if i == 4 {
283+
return Err(err.into());
284+
} else {
285+
sleep(Duration::from_secs(1));
286+
continue;
287+
}
288+
}
289+
}
290+
}
291+
unreachable!()
292+
}
293+
301294
fn venv_path(path: &Path) -> PathBuf {
302295
path.join(".venv")
303296
.join(if cfg!(windows) { "Scripts" } else { "bin" })

0 commit comments

Comments
 (0)