Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/tcp_stream_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use wstd::io::{self, AsyncRead, AsyncWrite};
use wstd::net::TcpStream;

#[wstd::main]
async fn main() -> io::Result<()> {
let mut args = std::env::args();

let _ = args.next();

let addr = args.next().ok_or_else(|| {
io::Error::new(
std::io::ErrorKind::InvalidInput,
"address argument required",
)
})?;

let mut stream = TcpStream::connect(addr).await?;

stream.write_all(b"ping\n").await?;

let mut reply = Vec::new();
stream.read_to_end(&mut reply).await?;

Ok(())
}
53 changes: 53 additions & 0 deletions test-programs/tests/tcp_stream_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use anyhow::{Context, Result};
use std::net::{Shutdown, TcpListener};
use std::process::{Command, Stdio};

#[test_log::test]
fn tcp_stream_client() -> Result<()> {
use std::io::{Read, Write};

let server = TcpListener::bind("127.0.0.1:8082").context("binding temporary test server")?;
let addr = server
.local_addr()
.context("getting local listener address")?;

let child = Command::new("wasmtime")
.arg("run")
.arg("-Sinherit-network")
.arg(test_programs::TCP_STREAM_CLIENT)
.arg(addr.to_string())
.stdout(Stdio::piped())
.spawn()
.context("spawning wasmtime component")?;

let (mut server_stream, _addr) = server
.accept()
.context("accepting TCP connection from component")?;

let mut buf = [0u8; 5];
server_stream
.read_exact(&mut buf)
.context("reading ping message")?;
assert_eq!(&buf, b"ping\n", "expected ping from component");

server_stream
.write_all(b"pong\n")
.context("writing reply")?;
server_stream.flush().context("flushing")?;

server_stream
.shutdown(Shutdown::Both)
.context("shutting down connection")?;

let output = child
.wait_with_output()
.context("waiting for component exit")?;

assert!(
output.status.success(),
"\nComponent exited abnormally (stderr:\n{})",
String::from_utf8_lossy(&output.stderr)
);

Ok(())
}
Loading