ulo-rpc-conformance 0.1.0-alpha.3

The conformance suite every Ulo RPC link runs
Documentation
//! Scenarios on the link itself, below the server and the client: a `cancel` sent after its
//! request reaches the server after it, on a link declaring `ordered_control`; and a streamed
//! request cancelled before the server acknowledged its `open` reaches the server as the `open`
//! and then the `cancel`, so the server holds nothing for it.
//!
//! On a link that holds the `cancel`, the held `cancel` goes out when an `opened` arrives late,
//! after the call ended, and is dropped with its entry at the deadline its `open` carried, or once
//! the link's hold runs out for an `open` carrying none, so a call whose `opened` never comes
//! holds nothing until the link closes.
//!
//! A link whose shapes carry no streamed request declares the scenarios opening one not
//! applicable, the streamed scenarios above the server asserting its startup refusal of a
//! streamed handler. On a link declared without `ordered_control` a request's
//! control frames travel a lane of their own and the broker can deliver a `cancel` ahead of its
//! request, so there the first scenario's order is not observable and the scenario is skipped,
//! naming the declared reason.

use std::collections::HashSet;
use std::time::{Duration, Instant};

use futures_util::StreamExt;
use futures_util::stream::BoxStream;
use ulo::{App, Module, ModuleDef, ModuleIdentity, Shape, Signal};
use ulo::app::Connected;
use ulo_rpc::link::ClientProbe;
use ulo_rpc::{CallHeaders, Codec, Delivery, Frame, Link, Outbound, Pattern, ReplyTo};

use crate::rt::{sleep, timeout};
use crate::{Broker, Budget, Harness, report};

/// The pattern the requests name; the server's link listens on it and nothing handles it.
const ORDERED: &str = "conformance.ordered";

/// Request and `cancel` pairs sent, each pair's two sends started together.
const ROUNDS: u64 = 200;

/// Streamed requests cancelled as they open.
const OPENS: u64 = 20;

/// How long the server's link waits for each frame.
const PATIENCE: Duration = Duration::from_secs(5);

/// The hold `hold_runs_out` gives a held `cancel`, in place of the link's own, and how late
/// `late_opened`'s `opened` arrives: half the broker's settle budget, 250 ms by default. Each
/// scenario asserts the order of its events, not their spacing, so the bound grows with the
/// broker's latency and is otherwise short.
fn short_hold(budget: Budget) -> Duration {
    budget.settle / 2
}

/// The deadline `held_until_deadline`'s `open` carries: the broker's settle budget and a fifth,
/// 600 ms by default, so the `open` reaches the server and its `opened` the client before it
/// passes and the `cancel` is held.
fn call_deadline(budget: Budget) -> Duration {
    budget.settle + budget.settle / 5
}

/// How often a scenario reads the client probe while it waits on it.
const PROBE_POLL: Duration = Duration::from_millis(10);

/// An app with nothing in it, whose handle the server's link is prepared with.
struct Empty;

impl Module for Empty {
    fn identity(&self) -> ModuleIdentity {
        ModuleIdentity::of_type::<Self>()
    }

    fn register(&self, m: &mut ModuleDef<'_>) {
        let _ = m;
    }
}

/// A server's link listening on [`ORDERED`] and a client's link connected to it, with no server
/// or client above them.
struct Linked<B: Broker> {
    broker: B,
    app: App<Connected>,
    server: B::Link,
    inbound: BoxStream<'static, Delivery>,
    client: B::Link,
    outbound: Outbound,
}

impl<B: Broker> Linked<B> {
    /// The two links over a fresh environment, for a scenario sending a streamed request's `open`
    /// below the server. A link carrying no streamed request has no `open` to send: there the
    /// scenario fails, and the link declares it not applicable, naming the streamed scenarios that
    /// assert its startup refusal of a streamed handler.
    async fn streamed() -> Self {
        let broker = B::start().await;
        let shapes = broker.link().capabilities().shapes;
        assert!(
            shapes.contains(&Shape::ClientStreaming) || shapes.contains(&Shape::Bidi),
            "the link carries no streamed request, so no `open` can be sent: declare the scenario not applicable, \
             naming the streamed scenarios that assert the startup refusal"
        );
        Linked::on(broker).await
    }

    async fn on(broker: B) -> Self {
        let app = App::builder(Empty)
            .runtime(<B::Harness as Harness>::runtime())
            .wire()
            .unwrap_or_else(|error| crate::startup_failed!("the ordering scenario's app did not wire: {}", report(&error)))
            .connect()
            .await
            .unwrap_or_else(|error| crate::startup_failed!("the ordering scenario's app did not connect: {}", report(&error)));
        let mut server = broker.link();
        server.prepare(&app.handle()).await.unwrap_or_else(|error| crate::startup_failed!("the server's link did not prepare: {error}"));
        let inbound = server
            .listen(&[Pattern::from(ORDERED).into()])
            .await
            .unwrap_or_else(|error| crate::startup_failed!("the server's link did not listen: {error}"));
        let client = broker.client_link(&server.bound());
        let handle = app.handle();
        let runtime = handle.runtime().unwrap_or_else(|| crate::startup_failed!("the ordering scenario's app has no runtime"));
        let outbound = client.connect(runtime).await.unwrap_or_else(|error| crate::startup_failed!("the client's link did not connect: {error}"));
        Linked { broker, app, server, inbound, client, outbound }
    }

    /// Reads `rounds` calls opened by `opens` frames, each followed by its `cancel`, from the
    /// server's link. A `cancel` reaching the server first names no call it holds and is dropped,
    /// and a `cancel` never sent leaves its call held, so either shows as a call with no `cancel`
    /// after it.
    async fn read_pairs(&mut self, rounds: u64, opens: &str) {
        let (mut opened, mut cancels) = (0u64, 0u64);
        let mut held = HashSet::new();
        while opened + cancels < 2 * rounds {
            let Some(delivered) = timeout::<B, _>(PATIENCE, self.inbound.next()).await else {
                panic!(
                    "{opened} {opens} and {cancels} cancels of {rounds} each reached the server within {PATIENCE:?} of the last; the \
                     server holds {held:?}"
                );
            };
            let delivery = delivered.expect("the server's inbound stream ended");
            match delivery.frame {
                Frame::Req { id, .. } | Frame::Open { id, .. } => {
                    held.insert(id);
                    opened += 1;
                }
                Frame::Cancel { id } => {
                    assert!(held.remove(&id), "the server received a cancel for a call it does not hold: {id}");
                    cancels += 1;
                }
                other => panic!("the server received a frame other than {opens} or a cancel: {other:?}"),
            }
        }
        assert!(held.is_empty(), "calls with no cancel after them: {held:?}");
    }

    async fn stop(self) {
        let _ = self.client.close().await;
        let _ = self.server.close().await;
        let _ = self.app.close(Signal::new("conformance")).await;
    }
}

/// Sends `ROUNDS` requests, each followed by its `cancel`, the two sends of a round polled together
/// and the request's first, and requires the server's link to deliver every request and then its
/// `cancel`. Skipped on a link declared without `ordered_control`, printing its reason.
pub async fn cancel_follows_its_request<B: Broker>() {
    let broker = B::start().await;
    if let Some(reason) = broker.link().capabilities().why_not_ordered_control() {
        crate::skipped("cancel_follows_its_request", "ordered_control", reason);
        return;
    }
    let mut linked = Linked::on(broker).await;
    let pattern = Pattern::from(ORDERED);
    let codec = if linked.server.capabilities().binary { Codec::Cbor } else { Codec::Json };
    let null = codec.encode(&()).expect("a unit encodes");
    for id in 1..=ROUNDS {
        let request = Frame::Req { id, pattern: ORDERED.to_owned(), headers: CallHeaders::new(), data: null.clone() };
        let request = (linked.outbound.send)(pattern.clone(), request, Some(ReplyTo { id }));
        let cancel = (linked.outbound.send)(pattern.clone(), Frame::Cancel { id }, None);
        let (requested, cancelled) = futures_util::join!(request, cancel);
        requested.unwrap_or_else(|error| panic!("request {id} was not sent: {error}"));
        cancelled.unwrap_or_else(|error| panic!("the cancel of request {id} was not sent: {error}"));
    }
    linked.read_pairs(ROUNDS, "requests").await;
    linked.stop().await;
}

/// Opens `OPENS` streamed requests, each cancelled in the same poll as its `open`, before the
/// server can have acknowledged it, and requires the server's link to deliver every `open` and
/// then its `cancel`, holding no call afterwards. A link holding a streamed request's control
/// frames until the server acknowledges the `open` holds the `cancel` too, and sends it once the
/// acknowledgment arrives. A link carrying no streamed request has no `open` to cancel: there the
/// scenario fails, and the link declares it not applicable, naming the streamed scenarios that
/// assert its startup refusal of a streamed handler.
pub async fn cancel_before_opened<B: Broker>() {
    let mut linked = Linked::<B>::streamed().await;
    let pattern = Pattern::from(ORDERED);
    for id in 1..=OPENS {
        let open = Frame::Open { id, pattern: ORDERED.to_owned(), headers: CallHeaders::new() };
        let open = (linked.outbound.send)(pattern.clone(), open, Some(ReplyTo { id }));
        let cancel = (linked.outbound.send)(pattern.clone(), Frame::Cancel { id }, None);
        let (opened, cancelled) = futures_util::join!(open, cancel);
        opened.unwrap_or_else(|error| panic!("the open of call {id} was not sent: {error}"));
        cancelled.unwrap_or_else(|error| panic!("the cancel of call {id} was not sent: {error}"));
    }
    linked.read_pairs(OPENS, "opens").await;
    linked.stop().await;
}

/// The client probe of `B`'s client link, or a failure naming the declaration.
fn probe<B: Broker>(linked: &Linked<B>) -> &ClientProbe {
    B::probe(&linked.client).unwrap_or_else(|| {
        panic!(
            "the environment reads no client probe, so the link holds no `cancel` for an `opened`: declare the held-cancel \
             scenarios not applicable with the reason"
        )
    })
}

/// Waits until `done` holds of the probe, read every 10 ms, failing after [`PATIENCE`].
async fn probe_until<B: Broker>(probe: &ClientProbe, what: &str, done: impl Fn(&ClientProbe) -> bool) {
    let waited = timeout::<B, _>(PATIENCE, async {
        while !done(probe) {
            sleep::<B>(PROBE_POLL).await;
        }
    })
    .await;
    assert!(waited.is_some(), "{what} did not happen within {PATIENCE:?}");
}

impl<B: Broker> Linked<B> {
    /// Opens streamed request `id` carrying `headers` with the server's `opened` withheld from
    /// the client's link, waits until the server holds the call and its `opened` has reached the
    /// client, and cancels the call, as a call's timeout or drop does: the client's table then
    /// holds the `cancel` alone.
    async fn cancel_with_opened_withheld(&mut self, id: u64, headers: CallHeaders) {
        probe(self).withhold();
        let pattern = Pattern::from(ORDERED);
        let open = Frame::Open { id, pattern: ORDERED.to_owned(), headers };
        (self.outbound.send)(pattern.clone(), open, Some(ReplyTo { id }))
            .await
            .unwrap_or_else(|error| panic!("the open of call {id} was not sent: {error}"));
        let delivered = timeout::<B, _>(PATIENCE, self.inbound.next()).await.expect("the open did not reach the server");
        let frame = delivered.expect("the server's inbound stream ended").frame;
        assert!(matches!(frame, Frame::Open { .. }), "the server received {frame:?} where the open was due");
        let probe = probe(self);
        probe_until::<B>(probe, "the server's `opened` reaching the client", |probe| probe.withheld() == 1).await;
        (self.outbound.send)(pattern, Frame::Cancel { id }, None)
            .await
            .unwrap_or_else(|error| panic!("the cancel of call {id} was not sent: {error}"));
        assert_eq!(probe.calls(), Some(1), "the client's table once the call was cancelled before its `opened`");
    }
}

/// A streamed request cancelled before its `opened` reached the client, as a timeout or a drop
/// cancels it, keeps its `cancel`; the `opened` arriving half a settle budget afterwards, within the
/// link's hold, sends it, and the server receives it for the call it holds. The client's table is
/// then empty. A link carrying no streamed request declares it not applicable.
pub async fn late_opened<B: Broker>() {
    let mut linked = Linked::<B>::streamed().await;
    let late = short_hold(linked.broker.budget());
    linked.cancel_with_opened_withheld(1, CallHeaders::new()).await;
    // Late by the short hold, and well inside the link's own.
    sleep::<B>(late).await;
    probe(&linked).release();
    let delivered = timeout::<B, _>(PATIENCE, linked.inbound.next()).await.unwrap_or_else(|| {
        panic!("the held cancel did not reach the server within {PATIENCE:?} of the late `opened`")
    });
    let frame = delivered.expect("the server's inbound stream ended").frame;
    assert!(matches!(frame, Frame::Cancel { .. }), "the server received {frame:?} where the held cancel was due");
    assert_eq!(probe(&linked).calls(), Some(0), "the client's table once the held cancel went out");
    linked.stop().await;
}

/// A streamed request cancelled before its `opened` reached the client, whose `opened` never
/// comes and whose `open` carried no deadline, leaves no entry in the client's table once the
/// link's hold runs out, shortened here to half the broker's settle budget. A link carrying no
/// streamed request declares it not applicable.
pub async fn hold_runs_out<B: Broker>() {
    let mut linked = Linked::<B>::streamed().await;
    let hold = short_hold(linked.broker.budget());
    probe(&linked).set_hold(hold);
    let cancelled = Instant::now();
    linked.cancel_with_opened_withheld(1, CallHeaders::new()).await;
    probe_until::<B>(probe(&linked), "the held cancel's entry dropped", |probe| probe.calls() == Some(0)).await;
    assert!(cancelled.elapsed() >= hold, "the held cancel was dropped before its hold ran out");
    linked.stop().await;
}

/// A held `cancel` whose call's `open` carried a `deadline-ms` of the broker's settle budget and a
/// fifth is dropped with its entry once that deadline passes, the server ending the call there
/// itself. The link's hold for an `open` with no deadline is set to 30 s, longer than the scenario
/// waits, so only the deadline can drop it in time. A link carrying no streamed request declares
/// it not applicable.
pub async fn held_until_deadline<B: Broker>() {
    let mut linked = Linked::<B>::streamed().await;
    let deadline = call_deadline(linked.broker.budget());
    probe(&linked).set_hold(Duration::from_secs(30));
    let mut headers = CallHeaders::new();
    headers.insert("deadline-ms", deadline.as_millis().to_string());
    let opened = Instant::now();
    linked.cancel_with_opened_withheld(1, headers).await;
    probe_until::<B>(probe(&linked), "the held cancel's entry dropped at its call's deadline", |probe| probe.calls() == Some(0)).await;
    assert!(opened.elapsed() >= deadline, "the held cancel was dropped {:?} after its `open`, before the call's {deadline:?} deadline", opened.elapsed());
    linked.stop().await;
}