use std::time::Duration;
use futures_util::{StreamExt, stream};
use ulo::{Bound, CancelReason};
use ulo_transport::{Count, ErrorKind};
use crate::Broker;
use crate::cases::app::{Fixture, IGNORE, Mounts, TICKS, eventually, failed, refused_at_startup, streams, within};
const WAIT: Duration = Duration::from_secs(10);
const LIMIT: u32 = 8;
pub async fn reply_overflow<B: Broker>() {
let fixture = Fixture::<B>::start().await;
if !streams(&fixture.capabilities()) {
refused_at_startup(&fixture.broker, Mounts { streaming: true, binary: false, every_instance: false }).await;
return fixture.stop().await;
}
let rpc = fixture.rpc().clone().max_queued_items(Count::Max(LIMIT)).expect("a limit of eight");
let ticks = rpc.stream::<_, u64>(TICKS, &()).idle_timeout(Bound::After(WAIT)).await.expect("the stream opens");
let probe = fixture.server.probe.clone();
let settle = fixture.broker.budget().settle;
assert!(eventually::<B>(settle.max(Duration::from_secs(5)), || probe.stopped()).await, "the producer kept running past the overflow");
assert_eq!(probe.cancelled(), Some(Some(CancelReason::ClientCancelled)), "how the producer ended");
let items: Vec<_> = within::<B, _>(WAIT, "the overflowed stream", ticks.collect()).await;
let held: Vec<u64> = items.iter().map_while(|item| item.as_ref().ok().copied()).collect();
assert_eq!(held, (0..u64::from(LIMIT)).collect::<Vec<_>>(), "the items held at the overflow: {items:?}");
assert_eq!(items.len(), LIMIT as usize + 1, "the stream did not end at its overflow: {items:?}");
let error = items.into_iter().last().and_then(Result::err).expect("the stream ends with the overflow");
assert_eq!((error.kind(), error.reason()), (ErrorKind::Unavailable, Some("overflow")), "{error}");
fixture.stop().await;
}
pub async fn request_overflow<B: Broker>() {
let fixture = Fixture::<B>::start_queued(Count::Max(LIMIT)).await;
if !streams(&fixture.capabilities()) {
refused_at_startup(&fixture.broker, Mounts { streaming: true, binary: false, every_instance: false }).await;
return fixture.stop().await;
}
let items = stream::iter(0..i64::from(LIMIT) * 4);
let answer = fixture.rpc().send_stream::<_, i64>(IGNORE, items).timeout(WAIT).await;
let error = failed(answer, ErrorKind::Unavailable);
assert_eq!(error.reason(), Some("overflow"), "{error}");
fixture.stop().await;
}