Skip to content

Commit 682ad7a

Browse files
committed
add integration tests for TcpStream::connect
1 parent 1a34b8d commit 682ad7a

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

examples/tcp_stream_client.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use wstd::io::{self, AsyncRead, AsyncWrite};
2+
use wstd::net::TcpStream;
3+
4+
#[wstd::main]
5+
async fn main() -> io::Result<()> {
6+
let mut args = std::env::args();
7+
8+
let _ = args.next();
9+
10+
let addr = args.next().ok_or_else(|| {
11+
io::Error::new(
12+
std::io::ErrorKind::InvalidInput,
13+
"address argument required",
14+
)
15+
})?;
16+
17+
let mut stream = TcpStream::connect(addr).await?;
18+
19+
stream.write_all(b"ping\n").await?;
20+
21+
let mut reply = Vec::new();
22+
stream.read_to_end(&mut reply).await?;
23+
24+
Ok(())
25+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use anyhow::{Context, Result};
2+
use std::{
3+
net::TcpListener,
4+
process::{Command, Stdio},
5+
};
6+
7+
#[test_log::test]
8+
fn tcp_stream_client() -> Result<()> {
9+
let server = TcpListener::bind("127.0.0.1:8081").context("binding temporary test server")?;
10+
let addr = server
11+
.local_addr()
12+
.context("getting local listener address")?;
13+
14+
let child = Command::new("wasmtime")
15+
.arg("run")
16+
.arg("-Sinherit-network")
17+
.arg(test_programs_artifacts::TCP_STREAM_CLIENT)
18+
.arg(addr.to_string())
19+
.stdout(Stdio::piped())
20+
.spawn()
21+
.context("spawning wasmtime component")?;
22+
23+
let (mut server_stream, _addr) = server
24+
.accept()
25+
.context("accepting TCP connection from component")?;
26+
27+
let mut buf = [0u8; 5];
28+
std::io::Read::read_exact(&mut server_stream, &mut buf).context("reading ping message")?;
29+
assert_eq!(&buf, b"ping\n", "expected ping from component");
30+
31+
std::io::Write::write_all(&mut server_stream, b"pong\n").context("writing reply")?;
32+
std::io::Write::flush(&mut server_stream).context("flushing")?;
33+
34+
std::net::TcpStream::shutdown(&mut server_stream, std::net::Shutdown::Both)
35+
.context("shutting down connection")?;
36+
37+
let output = child
38+
.wait_with_output()
39+
.context("waiting for component exit")?;
40+
41+
assert!(
42+
output.status.success(),
43+
"\nComponent exited abnormally (stderr:\n{})",
44+
String::from_utf8_lossy(&output.stderr)
45+
);
46+
47+
Ok(())
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!("../../../examples/tcp_stream_client.rs");

0 commit comments

Comments
 (0)