Lines
100 %
Functions
Branches
//! The reply channel is bounded: a full one stops its workers, loses nothing,
//! and `frame` never waits for it.
use super::*;
const WAIT: Duration = Duration::from_millis(150);
fn tight_rig() -> (Mux, mpsc::Receiver<String>) {
let ctx = ScriptCtx::new(uuid::Uuid::nil());
Mux::new(&Handle::current(), ctx, Session::new).expect("Mux::new")
}
#[tokio::test]
async fn a_refusal_comes_back_as_data_without_waiting() {
let (mut mux, _replies) = tight_rig();
let refusal = mux.frame("(garbage)").expect("a refusal");
assert!(refusal.starts_with("(:id 0 :error (:code "), "{refusal}");
async fn a_worker_waits_for_room_and_keeps_its_order() {
let (mut mux, mut replies) = tight_rig();
let jobs = 3 * MAX_STREAMS;
for id in 1..=jobs {
assert_eq!(mux.frame(&format!("(:id {id} :form {id})")), None);
tokio::time::sleep(WAIT).await;
let reply = recv(&mut replies).await.expect("reply");
assert_eq!(reply, format!("(:id {id} :value {id})"));
async fn rejected_input_is_a_parse_reply() {
let reply = reject_input(&FrameError::TooLong);
assert!(
reply.starts_with(
"(:id 0 :error (:code parse :message \"frame parse error: frame exceeds 1048576 bytes\""
),
"{reply}"
);
/// The worker holds one job behind the permit and the queue holds QUEUE_CAPACITY more.
async fn fill_queue(rig: &mut Rig) {
rig.frame("(:id 0 :form 1)");
tokio::task::yield_now().await;
for id in 1..=QUEUE_CAPACITY {
rig.frame(&format!("(:id {id} :form 1)"));
async fn ready_waits_for_room_in_a_full_queue() {
let mut rig = rig();
let permit = rig.hold_permit().await;
fill_queue(&mut rig).await;
let next = "(:id 99 :form 1)";
let waiting = tokio::time::timeout(WAIT, rig.mux.ready(next)).await;
assert!(waiting.is_err(), "the queue is full, ready must wait");
drop(permit);
tokio::time::timeout(Duration::from_secs(30), rig.mux.ready(next))
.await
.expect("room once the worker drains");
assert_eq!(rig.mux.frame(next), None);
async fn ready_does_not_wait_for_control_frames_or_unopened_streams() {
let _permit = rig.hold_permit().await;
for frame in ["(:interrupt 0)", "(:id 5 :stream 4 :form 1)", "(garbage"] {
tokio::time::timeout(WAIT, rig.mux.ready(frame))
.expect("ready returns at once");