-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirect stdio #1
Open
fabianfreyer
wants to merge
2
commits into
master
Choose a base branch
from
stdout_redirection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−18
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -150,21 +150,34 @@ impl Runnable for Process { | |
thread::sleep(time::Duration::from_millis(10)); | ||
|
||
let mut p = lockable.lock().unwrap(); | ||
// p is a MutexGuard<_Process>, so each access to fields of _Process calls | ||
// deref / deref_mut to get the underlying _Process. To avoid borrowing | ||
// conflicts, get a reference to the underlying _Process struct once. | ||
let p = &mut *p; | ||
|
||
// Handle stdio | ||
if let Some(ref mut stdout) = p.stdout_reader { | ||
for line in stdout.lines() { | ||
info!("{:?}", line) | ||
p.stdout_sender | ||
.send(line.unwrap()) | ||
.expect("Could not send stdout"); | ||
} | ||
} | ||
|
||
if let Some(ref mut stderr) = p.stderr_reader { | ||
for line in stderr.lines() { | ||
error!("{:?}", line) | ||
p.stderr_sender | ||
.send(line.unwrap()) | ||
.expect("Could not send stderr"); | ||
} | ||
} | ||
|
||
if let Some(ref mut stdin) = p.stdin_writer { | ||
if let Ok(input) = p.stdin_receiver.try_recv() { | ||
info!("received: {}", input); | ||
stdin.write_all(input.as_bytes()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this probably a |
||
.expect("Could not write to stdin"); | ||
} | ||
} | ||
|
||
// Check if the process has already exited | ||
|
@@ -235,3 +248,49 @@ impl Runnable for Process { | |
self.lock().unwrap().handle = handle; | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::time; | ||
use super::*; | ||
extern crate pretty_env_logger; | ||
|
||
macro_rules! sleep { | ||
( $ms:expr ) => ({ | ||
let duration = time::Duration::from_millis($ms); | ||
thread::sleep(duration); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_run_ls_max_retries() { | ||
let interval = 1000; | ||
let p = from_argv!(["ls", "-la"], "/", interval); | ||
|
||
p.clone().launch(); | ||
|
||
sleep!(interval*p.lock().unwrap().max_restart_count); | ||
sleep!(interval); | ||
|
||
info!("{:?}",p); | ||
} | ||
|
||
#[test] | ||
fn test_exitstatus() { | ||
let p = from_argv!(["false"]); | ||
p.lock().unwrap().max_restart_count = 0; | ||
p.clone().launch(); | ||
sleep!(500); | ||
assert_eq!(p.lock().unwrap().exit_status, Some(1)); | ||
} | ||
|
||
#[test] | ||
fn test_echo() { | ||
let p = from_argv!(["echo", "test"], "/"); | ||
p.lock().unwrap().max_restart_count = 0; | ||
p.clone().launch(); | ||
sleep!(500); | ||
assert_eq!(p.lock().unwrap().stdout.recv(), Ok("test".to_string())); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After fubarnetes/fubarnetes#1 is solved, this should ideally be some
PUB
/SUB
or similar message queue.