-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract the runtime used in runtime tests so that it's not hardcoded…
… as Spin Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
- Loading branch information
Showing
6 changed files
with
232 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::io::Read; | ||
|
||
/// Helper for reading from a child process stream in a non-blocking way | ||
pub struct OutputStream { | ||
rx: std::sync::mpsc::Receiver<Result<Vec<u8>, std::io::Error>>, | ||
buffer: Vec<u8>, | ||
} | ||
|
||
impl OutputStream { | ||
pub fn new<R: Read + Send + 'static>(mut stream: R) -> Self { | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
std::thread::spawn(move || { | ||
let mut buffer = vec![0; 1024]; | ||
loop { | ||
let msg = stream.read(&mut buffer).map(|n| buffer[..n].to_vec()); | ||
if let Err(e) = tx.send(msg) { | ||
if let Err(e) = e.0 { | ||
eprintln!("Error reading from stream: {e}"); | ||
} | ||
break; | ||
} | ||
} | ||
}); | ||
Self { | ||
rx, | ||
buffer: Vec::new(), | ||
} | ||
} | ||
|
||
/// Get the output of the stream so far | ||
pub fn output(&mut self) -> &[u8] { | ||
while let Ok(Ok(s)) = self.rx.try_recv() { | ||
self.buffer.extend(s); | ||
} | ||
&self.buffer | ||
} | ||
|
||
/// Get the output of the stream so far | ||
/// | ||
/// Returns None if the output is not valid utf8 | ||
pub fn output_as_str(&mut self) -> Option<&str> { | ||
std::str::from_utf8(self.output()).ok() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::OutputStream; | ||
use crate::io::OutputStream; | ||
|
||
use super::Service; | ||
use anyhow::Context as _; | ||
|
Oops, something went wrong.