use super::super::super::doubt::{outside, unknown};
use super::super::super::{Capture, Offsets, Piece, Post, Standing};
use super::super::run::UNREAD_LIMIT;
use super::{Mailbox, NOW, call, poll, quick, took};
pub(super) fn piece(stdout: &str, stderr: &str) -> Post {
Post::Partial(Piece {
stdout: stdout.to_owned(),
stderr: stderr.to_owned(),
})
}
pub(super) fn ending(stdout: &str, exit_code: i32) -> Post {
Post::Capture(Capture {
stdout: stdout.to_owned(),
stderr: String::new(),
exit_code,
})
}
pub(super) fn at(stdout: u64, stderr: u64) -> Offsets {
Offsets { stdout, stderr }
}
pub(super) fn live(stdout: &str, stderr: &str) -> Standing {
Standing {
partial: Some(Piece {
stdout: stdout.to_owned(),
stderr: stderr.to_owned(),
}),
capture: None,
}
}
pub(super) fn running(mail: &Mailbox) -> String {
let id = mail.post(NOW, "local", &call("laptop", "bz"));
assert_eq!(took(mail, "laptop").len(), 1);
id
}
#[test]
fn posts_absorb_in_order_and_the_capture_ends_the_run() {
let mail = quick();
let id = running(&mail);
assert_eq!(
mail.complete(NOW, "laptop", &id, &piece("a", "x")),
Ok(live("", "")),
"the receipt is the lease fact, not an echo"
);
assert_eq!(poll(&mail, "local", &id), Ok(None), "the run is not over");
assert_eq!(
mail.complete(NOW, "laptop", &id, &piece("b", "")),
Ok(live("", ""))
);
let whole = Capture {
stdout: "abc".to_owned(),
stderr: "x".to_owned(),
exit_code: 0,
};
assert_eq!(
mail.complete(NOW, "laptop", &id, &ending("c", 0)),
Ok(Standing {
partial: None,
capture: Some(whole.clone()),
}),
"the last post's text is what was not yet posted"
);
assert_eq!(poll(&mail, "local", &id), Ok(Some(whole)));
}
#[test]
fn since_is_idempotent_and_partial_is_the_lease_fact() {
let mail = quick();
let id = running(&mail);
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(0, 0))),
Ok(Standing::default()),
"nobody has posted for this slot"
);
mail.complete(NOW, "laptop", &id, &piece("", ""))
.expect("still running");
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(0, 0))),
Ok(live("", ""))
);
mail.complete(NOW, "laptop", &id, &piece("ab", "e"))
.expect("in flight");
for _ in 0..2 {
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(0, 0))),
Ok(live("ab", "e"))
);
}
mail.complete(NOW, "laptop", &id, &piece("cd", ""))
.expect("in flight");
for _ in 0..2 {
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(2, 1))),
Ok(live("cd", ""))
);
}
}
#[test]
fn an_offset_no_stream_can_answer_from_is_refused() {
let mail = quick();
let id = running(&mail);
mail.complete(NOW, "laptop", &id, &piece("aé", ""))
.expect("in flight");
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(1, 0))),
Ok(live("é", ""))
);
for bad in [
Some(at(0, 0)),
Some(at(2, 0)),
Some(at(9, 0)),
Some(at(1, 1)),
] {
assert_eq!(
mail.collect(NOW, "local", &id, bad),
Err(outside(&id)),
"{bad:?}"
);
}
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(3, 0))),
Ok(live("", ""))
);
}
#[test]
fn a_final_capture_with_since_is_the_remainder() {
let mail = quick();
let id = running(&mail);
mail.complete(NOW, "laptop", &id, &piece("ab", ""))
.expect("in flight");
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(0, 0))),
Ok(live("ab", ""))
);
mail.complete(NOW, "laptop", &id, &ending("cd", 3))
.expect("in flight");
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(2, 0))),
Ok(Standing {
partial: None,
capture: Some(Capture {
stdout: "cd".to_owned(),
stderr: String::new(),
exit_code: 3,
}),
})
);
assert_eq!(poll(&mail, "local", &id), Err(unknown(&id)), "read once");
}
#[test]
fn a_post_for_a_finished_run_is_a_spent_handle() {
let mail = quick();
let id = running(&mail);
mail.complete(NOW, "laptop", &id, &ending("out", 0))
.expect("in flight");
for late in [ending("out", 0), piece("more", "")] {
assert_eq!(mail.complete(NOW, "laptop", &id, &late), Err(unknown(&id)));
}
assert_eq!(
poll(&mail, "local", &id).map(|c| c.map(|c| c.stdout)),
Ok(Some("out".to_owned())),
"and what landed first is what is collected"
);
}
#[test]
fn a_redelivered_run_that_streams_is_marked_at_its_first_post() {
let mail = quick();
let id = running(&mail);
assert_eq!(took(&mail, "laptop").len(), 1, "handed out a second time");
mail.complete(NOW, "laptop", &id, &piece("a", ""))
.expect("in flight");
mail.complete(NOW, "laptop", &id, &piece("", "warn"))
.expect("in flight");
let read = mail
.collect(NOW, "local", &id, Some(at(0, 0)))
.expect("the asker's own handle")
.partial
.expect("posted");
assert!(
read.stderr.contains("\"laptop\" 2 times"),
"{}",
read.stderr
);
assert!(
read.stderr.ends_with("\nwarn"),
"said once: {}",
read.stderr
);
}
#[test]
fn unread_text_is_bounded_and_the_drop_is_said_on_stderr() {
let mail = quick();
let id = running(&mail);
let flood = format!("{}éyyy", "x".repeat(UNREAD_LIMIT - 1));
mail.complete(NOW, "laptop", &id, &piece(&flood, ""))
.expect("in flight");
mail.complete(NOW, "laptop", &id, &piece("more", ""))
.expect("in flight");
let held = (UNREAD_LIMIT - 1) as u64;
assert_eq!(
mail.collect(NOW, "local", &id, Some(at(held, 0))),
Ok(live("m", "")),
"the cut fell before the character that did not fit, leaving one byte of room"
);
mail.complete(NOW, "laptop", &id, &ending("fits", 0))
.expect("in flight");
let ended = mail
.collect(NOW, "local", &id, Some(at(held + 1, 0)))
.expect("the asker's own handle")
.capture
.expect("over");
assert_eq!(ended.stdout, "fits", "an acknowledged read made room");
assert_eq!(
ended.stderr,
format!(
"\nyog: stdout exceeded this server's {UNREAD_LIMIT}-byte capture limit; \
8 further bytes were dropped\n"
)
);
}