use std::sync::Arc;
use bytes::Bytes;
use rstest::rstest;
use super::super::WireframeApp;
use crate::{
app::{PrepareError, PreparedApp},
codec::LengthDelimitedFrameCodec,
push::{PushError, PushQueues, PushQueuesBuilder},
session::{ConnectionId, SessionRegistry},
};
#[test]
fn supplied_session_registry_is_shared() {
let registry = Arc::new(SessionRegistry::<Bytes>::new());
let app: WireframeApp = WireframeApp::new()
.expect("builder should initialize")
.with_session_registry(Arc::clone(®istry));
assert!(
Arc::ptr_eq(&app.session_registry(), ®istry),
"builder should expose the supplied registry"
);
}
#[test]
fn default_session_registry_allocates_identifiers() {
let app: WireframeApp = WireframeApp::new().expect("builder should initialize");
assert_eq!(
app.session_registry().allocate_connection_id(),
ConnectionId::new(1),
"a fresh registry should start at one"
);
}
#[test]
fn with_codec_resets_push_configuration() {
let registry = Arc::new(SessionRegistry::<Bytes>::new());
let template = PushQueues::<Bytes>::builder()
.high_capacity(8)
.low_capacity(8);
let builder: WireframeApp = WireframeApp::default();
let app = builder
.with_push_queues(template)
.with_session_registry(Arc::clone(®istry))
.with_codec(LengthDelimitedFrameCodec::new(1024));
assert!(
app.push_queues.is_none(),
"with_codec should drop the frame-typed template"
);
assert!(
!Arc::ptr_eq(&app.session_registry(), ®istry),
"with_codec should replace the frame-typed registry"
);
}
#[rstest]
#[case::zero_capacity(PushQueues::<Bytes>::builder().high_capacity(0))]
#[case::oversize_capacity(PushQueues::<Bytes>::builder().high_capacity(usize::MAX))]
#[tokio::test]
async fn prepare_rejects_invalid_push_template(#[case] template: PushQueuesBuilder<Bytes>) {
let app: WireframeApp = WireframeApp::new()
.expect("builder should initialize")
.with_push_queues(template);
let result = app.prepare().await;
assert!(
matches!(result, Err(PrepareError::InvalidPushConfig { .. })),
"preparation should reject an unusable template"
);
}
#[tokio::test]
async fn prepared_app_exposes_default_push_template() -> Result<(), Box<dyn std::error::Error>> {
let prepared: PreparedApp = WireframeApp::new()?.prepare().await?;
let (_queues, _handle) = prepared.push_queues().build()?;
Ok(())
}
#[tokio::test]
async fn prepared_app_retains_session_registry() -> Result<(), Box<dyn std::error::Error>> {
let registry = Arc::new(SessionRegistry::<Bytes>::new());
let prepared: PreparedApp = WireframeApp::new()?
.with_session_registry(Arc::clone(®istry))
.prepare()
.await?;
if !Arc::ptr_eq(&prepared.session_registry(), ®istry) {
return Err("preparation should preserve the supplied registry".into());
}
Ok(())
}
#[tokio::test]
async fn prepared_push_template_enforces_the_codec_limit() -> Result<(), Box<dyn std::error::Error>>
{
let builder: WireframeApp = WireframeApp::new()?;
let prepared: PreparedApp = builder
.with_codec(LengthDelimitedFrameCodec::new(64))
.prepare()
.await?;
let (_queues, handle) = prepared.push_queues().build()?;
let oversized = handle
.push_high_priority(Bytes::from_static(&[0; 65]))
.await;
if oversized != Err(PushError::FrameTooLarge { size: 65, max: 64 }) {
return Err("the connection template must carry the codec limit".into());
}
if handle
.push_high_priority(Bytes::from_static(&[0; 64]))
.await
.is_err()
{
return Err("a frame at the codec limit must be accepted".into());
}
Ok(())
}
#[tokio::test]
async fn prepared_push_template_keeps_a_stricter_limit() -> Result<(), Box<dyn std::error::Error>> {
let template = PushQueues::<Bytes>::builder()
.high_capacity(1)
.max_frame_len(64, |frame: &Bytes| frame.len());
let builder: WireframeApp = WireframeApp::new()?;
let prepared: PreparedApp = builder
.with_codec(LengthDelimitedFrameCodec::new(2048))
.with_push_queues(template)
.prepare()
.await?;
let (_queues, handle) = prepared.push_queues().build()?;
let oversized = handle
.push_high_priority(Bytes::from_static(&[0; 65]))
.await;
if oversized != Err(PushError::FrameTooLarge { size: 65, max: 64 }) {
return Err("the stricter template limit must survive the codec ceiling".into());
}
Ok(())
}