1
//! The reply channel is bounded: a full one stops its workers, loses nothing,
2
//! and `frame` never waits for it.
3

            
4
use super::*;
5

            
6
const WAIT: Duration = Duration::from_millis(150);
7

            
8
2
fn tight_rig() -> (Mux, mpsc::Receiver<String>) {
9
2
    let ctx = ScriptCtx::new(uuid::Uuid::nil());
10
2
    Mux::new(&Handle::current(), ctx, Session::new).expect("Mux::new")
11
2
}
12

            
13
#[tokio::test]
14
1
async fn a_refusal_comes_back_as_data_without_waiting() {
15
1
    let (mut mux, _replies) = tight_rig();
16
1
    let refusal = mux.frame("(garbage)").expect("a refusal");
17
1
    assert!(refusal.starts_with("(:id 0 :error (:code "), "{refusal}");
18
1
}
19

            
20
#[tokio::test]
21
1
async fn a_worker_waits_for_room_and_keeps_its_order() {
22
1
    let (mut mux, mut replies) = tight_rig();
23
1
    let jobs = 3 * MAX_STREAMS;
24
1
    for id in 1..=jobs {
25
24
        assert_eq!(mux.frame(&format!("(:id {id} :form {id})")), None);
26
    }
27
1
    tokio::time::sleep(WAIT).await;
28
1
    for id in 1..=jobs {
29
24
        let reply = recv(&mut replies).await.expect("reply");
30
24
        assert_eq!(reply, format!("(:id {id} :value {id})"));
31
1
    }
32
1
}
33

            
34
#[tokio::test]
35
1
async fn rejected_input_is_a_parse_reply() {
36
1
    let reply = reject_input(&FrameError::TooLong);
37
1
    assert!(
38
1
        reply.starts_with(
39
1
            "(:id 0 :error (:code parse :message \"frame parse error: frame exceeds 1048576 bytes\""
40
1
        ),
41
1
        "{reply}"
42
1
    );
43
1
}
44

            
45
/// The worker holds one job behind the permit and the queue holds QUEUE_CAPACITY more.
46
2
async fn fill_queue(rig: &mut Rig) {
47
2
    rig.frame("(:id 0 :form 1)");
48
2
    tokio::task::yield_now().await;
49
2
    for id in 1..=QUEUE_CAPACITY {
50
64
        rig.frame(&format!("(:id {id} :form 1)"));
51
    }
52
2
}
53

            
54
#[tokio::test]
55
1
async fn ready_waits_for_room_in_a_full_queue() {
56
1
    let mut rig = rig();
57
1
    let permit = rig.hold_permit().await;
58
1
    fill_queue(&mut rig).await;
59
1
    let next = "(:id 99 :form 1)";
60
1
    let waiting = tokio::time::timeout(WAIT, rig.mux.ready(next)).await;
61
1
    assert!(waiting.is_err(), "the queue is full, ready must wait");
62
1
    drop(permit);
63
1
    tokio::time::timeout(Duration::from_secs(30), rig.mux.ready(next))
64
1
        .await
65
1
        .expect("room once the worker drains");
66
1
    assert_eq!(rig.mux.frame(next), None);
67
1
}
68

            
69
#[tokio::test]
70
1
async fn ready_does_not_wait_for_control_frames_or_unopened_streams() {
71
1
    let mut rig = rig();
72
1
    let _permit = rig.hold_permit().await;
73
1
    fill_queue(&mut rig).await;
74
1
    for frame in ["(:interrupt 0)", "(:id 5 :stream 4 :form 1)", "(garbage"] {
75
3
        tokio::time::timeout(WAIT, rig.mux.ready(frame))
76
3
            .await
77
3
            .expect("ready returns at once");
78
1
    }
79
1
}