Lines
100 %
Functions
Branches
//! Close, reuse while draining, and dropping the `Mux`.
use super::*;
fn assert_interrupted(reply: &str, id: u32, stream: u32) {
let head = format!("(:id {id} :stream {stream} :error (:code interrupted");
assert!(reply.starts_with(&head), "{reply}");
}
#[tokio::test]
async fn close_answers_the_queue_without_the_eval_permit() {
let mut rig = rig();
let permit = rig.hold_permit().await;
rig.frame("(:id 1 :stream 1 :form 1)");
rig.frame("(:id 2 :stream 1 :form 2)");
rig.frame("(:close 1)");
assert_interrupted(&rig.next().await, 1, 1);
assert_interrupted(&rig.next().await, 2, 1);
assert_eq!(rig.mux.permits.available_permits(), 0);
drop(permit);
async fn close_reaches_a_worker_already_waiting_for_the_permit() {
tokio::task::yield_now().await;
async fn a_reused_number_is_fresh_while_the_old_worker_drains() {
rig.ask("(:id 1 :stream 1 :form (defun f () 7))").await;
rig.frame("(:id 2 :stream 1 :form (f))");
rig.frame("(:id 3 :stream 1 :form (f))");
assert_eq!(rig.mux.streams.len(), 2);
let fresh = rig.next().await;
assert!(fresh.starts_with("(:id 3 :stream 1 :error"), "{fresh}");
async fn closing_zero_or_an_unknown_stream_is_ignored() {
rig.ask("(:id 1 :form (defun h () 5))").await;
rig.frame("(:close 0)");
rig.frame("(:close 6)");
assert_eq!(rig.ask("(:id 2 :form (h))").await, "(:id 2 :value 5)");
assert_eq!(rig.mux.streams.len(), 1);
async fn closed_streams_do_not_count_toward_the_cap() {
for stream in 1..MAX_STREAMS {
rig.frame(&format!("(:id {stream} :stream {stream} :form 1)"));
rig.frame("(:close 3)");
rig.frame("(:id 50 :stream 20 :form 1)");
assert_eq!(rig.mux.streams.len(), MAX_STREAMS);
let mut replies = Vec::new();
for _ in 0..MAX_STREAMS {
replies.push(rig.next().await);
assert!(
replies
.iter()
.all(|reply| !reply.contains("too-many-streams")),
"{replies:?}"
);
async fn dropping_the_mux_drains_every_queue_then_ends_the_channel() {
let Rig {
mut mux,
mut replies,
refusals,
} = rig();
drop(refusals);
for frame in [
"(:id 1 :form 1)",
"(:id 2 :stream 1 :form 2)",
"(:id 3 :stream 1 :form 3)",
] {
assert_eq!(mux.frame(frame), None);
drop(mux);
let mut drained = Vec::new();
let collect = async {
while let Some(reply) = recv(&mut replies).await {
drained.push(reply);
};
tokio::time::timeout(Duration::from_secs(30), collect)
.await
.expect("the channel ends once the workers are done");
drained.sort();
assert_eq!(
drained,
[
"(:id 1 :value 1)",
"(:id 2 :stream 1 :value 2)",
"(:id 3 :stream 1 :value 3)"
]
async fn abort_answers_every_queued_request_interrupted_even_after_the_mux_is_gone() {
rig.frame("(:id 2 :stream 2 :form 2)");
let abort = rig.mux.abort_handle();
drop(rig.mux);
abort.abort();
let mut replies = [recv(&mut rig.replies).await, recv(&mut rig.replies).await];
replies.sort();
assert_interrupted(replies[0].as_deref().unwrap_or_default(), 1, 1);
assert_interrupted(replies[1].as_deref().unwrap_or_default(), 2, 2);