MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / tcp_stream_client

Function tcp_stream_client

test-programs/tests/tcp_stream_client.rs:6–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4
5#[test_log::test]
6fn tcp_stream_client() -> Result<()> {
7 use std::io::{Read, Write};
8
9 let server = TcpListener::bind("127.0.0.1:8082").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::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 server_stream
29 .read_exact(&mut buf)
30 .context("reading ping message")?;
31 assert_eq!(&buf, b"ping\n", "expected ping from component");
32
33 server_stream
34 .write_all(b"pong\n")
35 .context("writing reply")?;
36 server_stream.flush().context("flushing")?;
37
38 server_stream
39 .shutdown(Shutdown::Both)
40 .context("shutting down connection")?;
41
42 let output = child
43 .wait_with_output()
44 .context("waiting for component exit")?;
45
46 assert!(
47 output.status.success(),
48 "\nComponent exited abnormally (stderr:\n{})",
49 String::from_utf8_lossy(&output.stderr)
50 );
51
52 Ok(())
53}

Callers

nothing calls this directly

Calls 4

local_addrMethod · 0.80
spawnMethod · 0.80
write_allMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected