| 31 | } |
| 32 | |
| 33 | pub fn new_with_config(guest: &str, port: u16, env_vars: &[&str]) -> std::io::Result<Self> { |
| 34 | let mut lockfile = std::env::temp_dir(); |
| 35 | lockfile.push(format!("TEST_PROGRAMS_WASMTIME_SERVE_{port}.lock")); |
| 36 | let lockfile = File::create(&lockfile)?; |
| 37 | // Once msrv reaches 1.89, replace with std's `.lock()` method |
| 38 | lockfile.lock_exclusive()?; |
| 39 | |
| 40 | // Run wasmtime serve. |
| 41 | // Enable -Scli because we currently don't have a way to build with the |
| 42 | // proxy adapter, so we build with the default adapter. |
| 43 | let mut process = Command::new("wasmtime"); |
| 44 | let listening_addr = format!("127.0.0.1:{port}"); |
| 45 | process |
| 46 | .arg("serve") |
| 47 | .arg("-Scli") |
| 48 | .arg("--addr") |
| 49 | .arg(&listening_addr); |
| 50 | for env_var in env_vars { |
| 51 | process.arg("--env").arg(env_var); |
| 52 | } |
| 53 | let process = process.arg(guest).spawn()?; |
| 54 | let w = WasmtimeServe { lockfile, process }; |
| 55 | |
| 56 | // Clumsily wait for the server to accept connections. |
| 57 | 'wait: loop { |
| 58 | sleep(Duration::from_millis(100)); |
| 59 | if TcpStream::connect(&listening_addr).is_ok() { |
| 60 | break 'wait; |
| 61 | } |
| 62 | } |
| 63 | Ok(w) |
| 64 | } |
| 65 | } |
| 66 | // Wasmtime serve will run until killed. Kill it in a drop impl so the process |
| 67 | // isnt orphaned when the test suite ends (successfully, or unsuccessfully) |