|
1 | | -use anyhow::{anyhow, Context, Result}; |
2 | | -use wasmtime::{ |
3 | | - component::{Component, Linker, ResourceTable}, |
4 | | - Config, Engine, Store, |
5 | | -}; |
6 | | -use wasmtime_wasi::{pipe::MemoryOutputPipe, WasiCtx, WasiView}; |
7 | | -use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView}; |
8 | | - |
9 | | -struct Ctx { |
10 | | - table: ResourceTable, |
11 | | - wasi: WasiCtx, |
12 | | - http: WasiHttpCtx, |
13 | | -} |
14 | | - |
15 | | -impl WasiView for Ctx { |
16 | | - fn table(&mut self) -> &mut ResourceTable { |
17 | | - &mut self.table |
18 | | - } |
19 | | - fn ctx(&mut self) -> &mut WasiCtx { |
20 | | - &mut self.wasi |
21 | | - } |
22 | | -} |
23 | | - |
24 | | -impl WasiHttpView for Ctx { |
25 | | - fn table(&mut self) -> &mut ResourceTable { |
26 | | - &mut self.table |
27 | | - } |
28 | | - fn ctx(&mut self) -> &mut WasiHttpCtx { |
29 | | - &mut self.http |
30 | | - } |
31 | | -} |
32 | | - |
33 | | -fn run_in_wasmtime(wasm: &[u8], stdout: Option<MemoryOutputPipe>) -> Result<()> { |
34 | | - let config = Config::default(); |
35 | | - let engine = Engine::new(&config).context("creating engine")?; |
36 | | - let component = Component::new(&engine, wasm).context("loading component")?; |
37 | | - |
38 | | - let mut linker: Linker<Ctx> = Linker::new(&engine); |
39 | | - wasmtime_wasi::add_to_linker_sync(&mut linker).context("add wasi to linker")?; |
40 | | - wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker) |
41 | | - .context("add wasi-http to linker")?; |
42 | | - |
43 | | - let mut builder = WasiCtx::builder(); |
44 | | - builder.inherit_stderr().inherit_network(); |
45 | | - let wasi = match stdout { |
46 | | - Some(stdout) => builder.stdout(stdout).build(), |
47 | | - None => builder.inherit_stdout().build(), |
48 | | - }; |
49 | | - let mut store = Store::new( |
50 | | - &engine, |
51 | | - Ctx { |
52 | | - table: ResourceTable::new(), |
53 | | - wasi, |
54 | | - http: WasiHttpCtx::new(), |
55 | | - }, |
56 | | - ); |
57 | | - |
58 | | - let instance = linker.instantiate(&mut store, &component)?; |
59 | | - let run_interface = instance |
60 | | - .get_export(&mut store, None, "wasi:cli/run@0.2.0") |
61 | | - .ok_or_else(|| anyhow!("wasi:cli/run missing?"))?; |
62 | | - let run_func_export = instance |
63 | | - .get_export(&mut store, Some(&run_interface), "run") |
64 | | - .ok_or_else(|| anyhow!("run export missing?"))?; |
65 | | - let run_func = instance |
66 | | - .get_typed_func::<(), (Result<(), ()>,)>(&mut store, &run_func_export) |
67 | | - .context("run as typed func")?; |
68 | | - |
69 | | - println!("entering wasm..."); |
70 | | - let (runtime_result,) = run_func.call(&mut store, ())?; |
71 | | - runtime_result.map_err(|()| anyhow!("run returned an error"))?; |
72 | | - println!("done"); |
73 | | - |
74 | | - Ok(()) |
75 | | -} |
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::process::Command; |
76 | 3 |
|
77 | 4 | #[test_log::test] |
78 | 5 | fn tcp_echo_server() -> Result<()> { |
79 | 6 | use std::io::{Read, Write}; |
80 | 7 | use std::net::{Shutdown, TcpStream}; |
81 | | - use std::thread::sleep; |
82 | | - use std::time::Duration; |
83 | 8 |
|
84 | 9 | println!("testing {}", test_programs_artifacts::TCP_ECHO_SERVER); |
85 | | - let wasm = std::fs::read(test_programs_artifacts::TCP_ECHO_SERVER).context("read wasm")?; |
86 | 10 |
|
87 | | - let pipe = wasmtime_wasi::pipe::MemoryOutputPipe::new(1024 * 1024); |
88 | | - let write_end = pipe.clone(); |
89 | | - let wasmtime_thread = std::thread::spawn(move || run_in_wasmtime(&wasm, Some(write_end))); |
| 11 | + // Run the component in wasmtime |
| 12 | + // -Sinherit-network allows it to accept network connections |
| 13 | + let mut wasmtime_process = Command::new("wasmtime") |
| 14 | + .arg("run") |
| 15 | + .arg("-Sinherit-network") |
| 16 | + .arg(test_programs_artifacts::TCP_ECHO_SERVER) |
| 17 | + .stdout(std::process::Stdio::piped()) |
| 18 | + .spawn()?; |
90 | 19 |
|
91 | | - 'wait: loop { |
92 | | - sleep(Duration::from_millis(100)); |
93 | | - for line in pipe.contents().split(|c| *c == b'\n') { |
94 | | - if line.starts_with(b"Listening on") { |
95 | | - break 'wait; |
96 | | - } |
97 | | - } |
98 | | - } |
| 20 | + let addr = get_listening_address(wasmtime_process.stdout.take().expect("stdout is piped"))?; |
99 | 21 |
|
100 | | - let mut tcpstream = |
101 | | - TcpStream::connect("127.0.0.1:8080").context("connect to wasm echo server")?; |
102 | | - println!("connected to wasm echo server"); |
| 22 | + println!("tcp echo server is listening on {addr:?}"); |
103 | 23 |
|
104 | | - const MESSAGE: &[u8] = b"hello, echoserver!\n"; |
| 24 | + let mut stream1 = TcpStream::connect(&addr).context("connect stream1")?; |
| 25 | + println!("stream1 connected"); |
105 | 26 |
|
106 | | - tcpstream.write_all(MESSAGE).context("write to socket")?; |
107 | | - println!("wrote to echo server"); |
| 27 | + let mut stream2 = TcpStream::connect(&addr).context("connect stream2")?; |
| 28 | + println!("stream3 connected"); |
108 | 29 |
|
109 | | - tcpstream.shutdown(Shutdown::Write)?; |
| 30 | + const MESSAGE1: &[u8] = b"hello, echoserver!\n"; |
110 | 31 |
|
111 | | - let mut readback = Vec::new(); |
112 | | - tcpstream |
113 | | - .read_to_end(&mut readback) |
114 | | - .context("read from socket")?; |
| 32 | + stream1.write_all(MESSAGE1).context("write to stream1")?; |
| 33 | + println!("stream1 wrote to echo server"); |
115 | 34 |
|
116 | | - println!("read from wasm server"); |
117 | | - assert_eq!(MESSAGE, readback); |
| 35 | + let mut stream3 = TcpStream::connect(&addr).context("connect stream3")?; |
| 36 | + println!("stream3 connected"); |
| 37 | + |
| 38 | + const MESSAGE2: &[u8] = b"hello, gussie!\n"; |
| 39 | + stream2.write_all(MESSAGE2).context("write to stream1")?; |
| 40 | + println!("stream2 wrote to echo server"); |
| 41 | + |
| 42 | + stream1.shutdown(Shutdown::Write)?; |
| 43 | + stream2.shutdown(Shutdown::Write)?; |
| 44 | + |
| 45 | + let mut readback2 = Vec::new(); |
| 46 | + stream2 |
| 47 | + .read_to_end(&mut readback2) |
| 48 | + .context("read from stream2")?; |
| 49 | + println!("read from stream2"); |
| 50 | + |
| 51 | + let mut readback1 = Vec::new(); |
| 52 | + stream1 |
| 53 | + .read_to_end(&mut readback1) |
| 54 | + .context("read from stream1")?; |
| 55 | + println!("read from stream1"); |
| 56 | + |
| 57 | + assert_eq!(MESSAGE1, readback1, "readback of stream 1"); |
| 58 | + assert_eq!(MESSAGE2, readback2, "readback of stream 2"); |
| 59 | + |
| 60 | + const MESSAGE3: &[u8] = b"hello, willa!\n"; |
| 61 | + stream3.write_all(MESSAGE3).context("write to stream1")?; |
| 62 | + println!("stream3 wrote to echo server"); |
| 63 | + stream3.shutdown(Shutdown::Write)?; |
| 64 | + |
| 65 | + let mut readback3 = Vec::new(); |
| 66 | + stream3 |
| 67 | + .read_to_end(&mut readback3) |
| 68 | + .context("read from stream3")?; |
| 69 | + println!("read from stream3"); |
| 70 | + assert_eq!(MESSAGE3, readback3, "readback of stream 3"); |
| 71 | + |
| 72 | + wasmtime_process.kill()?; |
118 | 73 |
|
119 | | - if wasmtime_thread.is_finished() { |
120 | | - wasmtime_thread.join().expect("wasmtime panicked")?; |
121 | | - } |
122 | 74 | Ok(()) |
123 | 75 | } |
| 76 | + |
| 77 | +fn get_listening_address( |
| 78 | + mut wasmtime_stdout: std::process::ChildStdout, |
| 79 | +) -> Result<std::net::SocketAddr> { |
| 80 | + use std::io::Read; |
| 81 | + use std::thread::sleep; |
| 82 | + use std::time::Duration; |
| 83 | + |
| 84 | + // Gather complete contents of stdout here |
| 85 | + let mut stdout_contents = String::new(); |
| 86 | + loop { |
| 87 | + // Wait for process to print |
| 88 | + sleep(Duration::from_millis(100)); |
| 89 | + |
| 90 | + // Read more that the process printed, append to contents |
| 91 | + let mut buf = vec![0; 4096]; |
| 92 | + let len = wasmtime_stdout |
| 93 | + .read(&mut buf) |
| 94 | + .context("reading wasmtime stdout")?; |
| 95 | + buf.truncate(len); |
| 96 | + stdout_contents |
| 97 | + .push_str(std::str::from_utf8(&buf).context("wasmtime stdout should be string")?); |
| 98 | + |
| 99 | + // Parse out the line where guest program says where it is listening |
| 100 | + for line in stdout_contents.lines() { |
| 101 | + if let Some(rest) = line.strip_prefix("Listening on ") { |
| 102 | + // Forget wasmtime_stdout, rather than drop it, so that any |
| 103 | + // subsequent stdout from wasmtime doesn't panic on a broken |
| 104 | + // pipe. |
| 105 | + std::mem::forget(wasmtime_stdout); |
| 106 | + return rest |
| 107 | + .parse() |
| 108 | + .with_context(|| format!("parsing socket addr from line: {line:?}")); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments