Skip to content

Commit

Permalink
Fleshed out the WCGI runner integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Mar 6, 2023
1 parent 91a8d4c commit d09b6a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
5 changes: 2 additions & 3 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,8 @@ impl RunWithPathBuf {
.context("Emscripten runner failed")?;
}

let (store, _compiler_type) = self.store.get_store()?;
let mut runner = wasmer_wasi::runners::wcgi::WcgiRunner::new(store);
runner.set_args(args.to_vec());
let mut runner = wasmer_wasi::runners::wcgi::WcgiRunner::new(id);
runner.config().args(args);
if runner.can_run_command(id, command).unwrap_or(false) {
runner
.run_cmd(&container, id)
Expand Down
2 changes: 0 additions & 2 deletions lib/wasi/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ impl crate::runners::Runner for WasiRunner {
let mut builder = prepare_webc_env(container, &atom_name, &self.args)?;

if let Some(tasks) = &self.tasks {
eprintln!("Aasdfasf");

let rt = PluggableRuntimeImplementation::new(Arc::clone(&tasks));
builder.set_runtime(Arc::new(rt));
}
Expand Down
39 changes: 35 additions & 4 deletions lib/wasi/tests/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ mod wasi {

#[cfg(feature = "webc_runner_rt_wcgi")]
mod wcgi {
use std::thread::JoinHandle;

use rand::Rng;
use tokio::runtime::Handle;
use wasmer::Store;
use wasmer_wasi::{runners::wcgi::WcgiRunner, runtime::task_manager::tokio::TokioTaskManager};

use super::*;
Expand All @@ -49,18 +50,48 @@ mod wcgi {
let webc = download_cached("https://wapm.dev/syrusakbary/staticserver").await;
let tasks = TokioTaskManager::new(Handle::current());
let container = WapmContainer::from_bytes(webc).unwrap();

let mut runner = WcgiRunner::new("staticserver");
let port = rand::thread_rng().gen_range(10000_u16..65535_u16);
let port = 12345;
let (tx, rx) = futures::channel::oneshot::channel();
runner
.config()
.addr(([127, 0, 0, 1], port).into())
.task_manager(tasks)
.abort_channel(rx);
runner.run_cmd(&container, "wcgi").unwrap();
// Note: the server blocks, so spin it up in a background thread and kill it
// after we've made our request.
let _guard = thread_spawn(move || {
runner.run_cmd(&container, "wcgi").unwrap();
});

// The way we test this is by fetching "/" and checking it contains
// something we expect
let resp = reqwest::get(format!("http://localhost:{port}/index.html"))
.await
.unwrap();
let body = resp.error_for_status().unwrap().text().await.unwrap();

assert!(body.contains("asdf"), "{}", body);

// Make sure we shut the server down afterwards
drop(tx);
}

todo!();
fn thread_spawn(f: impl FnOnce() + Send + 'static) -> impl Drop {
struct JoinOnDrop(Option<JoinHandle<()>>);
impl Drop for JoinOnDrop {
fn drop(&mut self) {
if let Err(e) = self.0.take().unwrap().join() {
if !std::thread::panicking() {
std::panic::resume_unwind(e);
}
}
}
}
let handle = std::thread::spawn(f);

JoinOnDrop(Some(handle))
}
}

Expand Down

0 comments on commit d09b6a5

Please sign in to comment.