Provide more stable test for proc stdin

pull/47/head
Chip Senkbeil 3 years ago
parent 756b87a8ae
commit 1fa0494e03
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -56,6 +56,16 @@ lazy_static::lazy_static! {
TEMP_SCRIPT_DIR.child("does_not_exist_bin");
}
macro_rules! next_two_msgs {
($rx:expr) => {{
let out = friendly_recv_line($rx, Duration::from_secs(1)).unwrap();
let res1: Response = serde_json::from_str(&out).unwrap();
let out = friendly_recv_line($rx, Duration::from_secs(1)).unwrap();
let res2: Response = serde_json::from_str(&out).unwrap();
(res1, res2)
}};
}
#[rstest]
fn should_execute_program_and_return_exit_status(mut action_cmd: Command) {
// distant action proc-run -- {cmd} [args]
@ -353,22 +363,16 @@ fn should_support_json_to_forward_stdin_to_remote_process(ctx: &'_ DistantServer
stdin.write_all(req_string.as_bytes()).unwrap();
stdin.flush().unwrap();
// Should receive ok message
let out = friendly_recv_line(&stdout, Duration::from_secs(1))
.expect("Failed to get ok response from proc stdin");
let res: Response = serde_json::from_str(&out).unwrap();
match &res.payload[0] {
ResponseData::Ok => {}
x => panic!("Unexpected response: {:?}", x),
};
// Get stdout from process and verify it
let out =
friendly_recv_line(&stdout, Duration::from_secs(1)).expect("Failed to get proc stdout");
let res: Response = serde_json::from_str(&out).unwrap();
match &res.payload[0] {
ResponseData::ProcStdout { data, .. } => assert_eq!(data, "hello world\n"),
x => panic!("Unexpected response: {:?}", x),
// Should receive ok message & stdout message, although these may be in different order
let (res1, res2) = next_two_msgs!(&stdout);
match (&res1.payload[0], &res2.payload[0]) {
(ResponseData::Ok, ResponseData::ProcStdout { data, .. }) => {
assert_eq!(data, "hello world\n")
}
(ResponseData::ProcStdout { data, .. }, ResponseData::Ok) => {
assert_eq!(data, "hello world\n")
}
x => panic!("Unexpected responses: {:?}", x),
};
// Kill the remote process since it only terminates when stdin closes, but we
@ -383,23 +387,16 @@ fn should_support_json_to_forward_stdin_to_remote_process(ctx: &'_ DistantServer
stdin.write_all(req_string.as_bytes()).unwrap();
stdin.flush().unwrap();
// Should receive ok message
let out = friendly_recv_line(&stdout, Duration::from_secs(1))
.expect("Failed to get ok response from proc stdin");
let res: Response = serde_json::from_str(&out).unwrap();
match &res.payload[0] {
ResponseData::Ok => {}
x => panic!("Unexpected response: {:?}", x),
};
// Get the indicator of a process completion
let out = friendly_recv_line(&stdout, Duration::from_secs(1)).expect("Failed to get proc done");
let res: Response = serde_json::from_str(&out).unwrap();
match &res.payload[0] {
ResponseData::ProcDone { success, .. } => {
// Should receive ok message & process completion
let (res1, res2) = next_two_msgs!(&stdout);
match (&res1.payload[0], &res2.payload[0]) {
(ResponseData::Ok, ResponseData::ProcDone { success, .. }) => {
assert!(!success, "Process succeeded unexpectedly");
}
x => panic!("Unexpected response: {:?}", x),
(ResponseData::ProcDone { success, .. }, ResponseData::Ok) => {
assert!(!success, "Process succeeded unexpectedly");
}
x => panic!("Unexpected responses: {:?}", x),
};
// Verify that we received nothing on stderr channel

@ -71,7 +71,6 @@ pub fn spawn_line_reader<T>(mut reader: T) -> mpsc::Receiver<String>
where
T: std::io::Read + Send + 'static,
{
let id = rand::random::<u8>();
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let mut buf = String::new();
@ -85,15 +84,12 @@ where
buf.push_str(data.as_ref());
// Send all complete lines
match buf.rfind('\n') {
Some(idx) => {
let remaining = buf.split_off(idx + 1);
for line in buf.lines() {
tx.send(line.to_string()).unwrap();
}
buf = remaining;
if let Some(idx) = buf.rfind('\n') {
let remaining = buf.split_off(idx + 1);
for line in buf.lines() {
tx.send(line.to_string()).unwrap();
}
None => {}
buf = remaining;
}
}

Loading…
Cancel
Save