use super::*;
#[derive(Clone, Copy)]
pub(super) enum AfterRelease {
Reply,
Panic,
}
pub(super) async fn answer_body(body: &mut RequestBodyStream) -> Option<Bytes> {
while let Some(item) = body.next().await {
item.ok()?;
}
Some(Bytes::from_static(b"live"))
}
pub(super) fn holding_handler(
started: Arc<Notify>,
release: Arc<Notify>,
finish: AfterRelease,
) -> StreamingHandler {
Arc::new(move |_parts: RequestParts, mut body: RequestBodyStream| {
let start_signal = Arc::clone(&started);
let release_signal = Arc::clone(&release);
Box::pin(async move {
start_signal.notify_one();
release_signal.notified().await;
match finish {
AfterRelease::Reply => answer_body(&mut body).await,
AfterRelease::Panic => panic!("the streaming handler panicked"),
}
})
})
}
pub(super) fn parked_reply(gated: Arc<Notify>, release: Arc<Notify>) -> ResponseHandler<Envelope> {
Arc::new(move |request: &Envelope| {
let is_gated = request.payload_bytes() == GATED_PAYLOAD;
let gated_signal = Arc::clone(&gated);
let release_signal = Arc::clone(&release);
Box::pin(async move {
if is_gated {
gated_signal.notify_one();
release_signal.notified().await;
}
Response::Single(Envelope::new(PARKED_ROUTE, None, b"done".to_vec()))
})
})
}
pub(super) fn scenario_app(
handler: StreamingHandler,
gated: Arc<Notify>,
release: Arc<Notify>,
) -> TestResult<App> {
WireframeApp::new()
.map(|app| app.with_message_assembler(TestAssembler))
.and_then(|app| app.streaming_route(LIVE_ROUTE, handler))
.and_then(|app| app.route_response(PARKED_ROUTE, parked_reply(gated, release)))
.map_err(|error| TestError::Msg(format!("build the application: {error}")))
}
pub(super) async fn park_one(client: &mut Client, key: u64, body: &[u8]) -> TestResult {
send_complete(client, PARKED_ROUTE, key, body).await
}
pub(super) struct Paired {
pub(super) client: Client,
pub(super) connection: JoinHandle<io::Result<()>>,
pub(super) release: Arc<Notify>,
pub(super) gated: Arc<Notify>,
gate_release: Arc<Notify>,
}
pub(super) async fn parked_pair(finish: AfterRelease) -> TestResult<Paired> {
let started = Arc::new(Notify::new());
let release = Arc::new(Notify::new());
let gated = Arc::new(Notify::new());
let gate_release = Arc::new(Notify::new());
let app = scenario_app(
holding_handler(Arc::clone(&started), Arc::clone(&release), finish),
Arc::clone(&gated),
Arc::clone(&gate_release),
)?;
let Scenario {
mut client,
connection,
} = spawn_connection(app).await?;
open_body(&mut client, LIVE_ROUTE, LIVE_KEY, &started).await?;
park_one(&mut client, FIRST_KEY, GATED_PAYLOAD).await?;
park_one(&mut client, SECOND_KEY, SECOND_PAYLOAD).await?;
Ok(Paired {
client,
connection,
release,
gated,
gate_release,
})
}
pub(super) async fn answer_live_body(paired: &mut Paired) -> TestResult {
paired.release.notify_one();
send_request(
&mut paired.client,
LIVE_ROUTE,
&continuation_frame_payload(LIVE_KEY.into(), 1.into(), b"end", true)?,
)
.await?;
ensure(
read_reply(&mut paired.client, "the live reply").await? == b"live".to_vec(),
"the live body must be answered before the parked messages",
)?;
bounded_for(
STEP_WAIT,
"the drain reaching its first reply",
paired.gated.notified(),
)
.await
}
pub(super) async fn answer_parked_pair(paired: &mut Paired) -> TestResult {
paired.gate_release.notify_one();
ensure(
read_reply(&mut paired.client, "the first parked reply").await? == b"done".to_vec(),
"the gated parked message must be answered once released",
)?;
ensure(
read_reply(&mut paired.client, "the second parked reply").await? == b"done".to_vec(),
"the second parked message must be answered",
)
}
pub(super) struct Sentinel {
_connection: Scenario,
_started: Arc<Notify>,
_release: Arc<Notify>,
}
pub(super) async fn parked_sentinel() -> TestResult<Sentinel> {
let started = Arc::new(Notify::new());
let release = Arc::new(Notify::new());
let app = scenario_app(
holding_handler(
Arc::clone(&started),
Arc::clone(&release),
AfterRelease::Reply,
),
Arc::new(Notify::new()),
Arc::new(Notify::new()),
)?;
let mut connection = spawn_connection(app).await?;
open_body(
&mut connection.client,
LIVE_ROUTE,
SENTINEL_LIVE_KEY,
&started,
)
.await?;
park_one(&mut connection.client, SENTINEL_PARKED_KEY, b"sentinel").await?;
Ok(Sentinel {
_connection: connection,
_started: started,
_release: release,
})
}