()
| 3 | |
| 4 | #[test_log::test] |
| 5 | fn tcp_echo_server() -> Result<()> { |
| 6 | use std::io::{Read, Write}; |
| 7 | use std::net::{Shutdown, TcpStream}; |
| 8 | |
| 9 | println!("testing {}", test_programs::TCP_ECHO_SERVER); |
| 10 | |
| 11 | // Run the component in wasmtime |
| 12 | // -Sinherit-network required for sockets to work |
| 13 | let mut wasmtime_process = Command::new("wasmtime") |
| 14 | .arg("run") |
| 15 | .arg("-Sinherit-network") |
| 16 | .arg(test_programs::TCP_ECHO_SERVER) |
| 17 | .stdout(std::process::Stdio::piped()) |
| 18 | .spawn()?; |
| 19 | |
| 20 | let addr = get_listening_address(wasmtime_process.stdout.take().expect("stdout is piped"))?; |
| 21 | |
| 22 | println!("tcp echo server is listening on {addr:?}"); |
| 23 | |
| 24 | let mut sock1 = TcpStream::connect(addr).context("connect sock1")?; |
| 25 | println!("sock1 connected"); |
| 26 | |
| 27 | let mut sock2 = TcpStream::connect(addr).context("connect sock2")?; |
| 28 | println!("sock2 connected"); |
| 29 | |
| 30 | const MESSAGE1: &[u8] = b"hello, echoserver!\n"; |
| 31 | |
| 32 | sock1.write_all(MESSAGE1).context("write to sock1")?; |
| 33 | println!("sock1 wrote to echo server"); |
| 34 | |
| 35 | let mut sock3 = TcpStream::connect(addr).context("connect sock3")?; |
| 36 | println!("sock3 connected"); |
| 37 | |
| 38 | const MESSAGE2: &[u8] = b"hello, gussie!\n"; |
| 39 | sock2.write_all(MESSAGE2).context("write to sock1")?; |
| 40 | println!("sock2 wrote to echo server"); |
| 41 | |
| 42 | sock1.shutdown(Shutdown::Write)?; |
| 43 | sock2.shutdown(Shutdown::Write)?; |
| 44 | |
| 45 | let mut readback2 = Vec::new(); |
| 46 | sock2 |
| 47 | .read_to_end(&mut readback2) |
| 48 | .context("read from sock2")?; |
| 49 | println!("read from sock2"); |
| 50 | |
| 51 | let mut readback1 = Vec::new(); |
| 52 | sock1 |
| 53 | .read_to_end(&mut readback1) |
| 54 | .context("read from sock1")?; |
| 55 | println!("read from sock1"); |
| 56 | |
| 57 | assert_eq!(MESSAGE1, readback1, "readback of sock1"); |
| 58 | assert_eq!(MESSAGE2, readback2, "readback of sock2"); |
| 59 | |
| 60 | let mut sock4 = TcpStream::connect(addr).context("connect sock4")?; |
| 61 | println!("sock4 connected"); |
| 62 | const MESSAGE4: &[u8] = b"hello, sparky!\n"; |
nothing calls this directly
no test coverage detected