Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

27.3 KB
Raw Blame
//! The deja gRPC egress boundary — a tower `Service` wrapper around the
//! transport under every generated tonic client.
//!
//! The wrapper is installed at the transport CONSTRUCTION sites (the shared
//! hyper pool `Client` type alias and the UCS channels), so every unary rpc —
//! current and future, on any service client built over the wrapped transport
//! — crosses one deja boundary with zero changes to how gRPC client code is
//! written or called.
//!
//! Identity carries NO explicit call-site id: this is a generic transport
//! wrapper, so there is no per-call-site literal to name. It is
//! `CallsiteSource::SyntacticHash` with `scope = "grpc::/package.Service/Method"`
//! — the rpc path is the most version-independent name an egress call has, and
//! it rides in the scope rather than as a rank-1 id — plus the span path as the
//! calling-context disambiguator. Routing is HARDCODED
//! `ReplayStrategy::Substitute` — egress is never re-issued.
//!
//! Substitution replays the recorded wire bytes through tonic's own decoder
//! (see [`super::semantic_boundary`]), so typed `Status` errors, trailers-only
//! shapes, and metadata partitioning reproduce by construction.

use std::{
    future::Future,
    panic::Location,
    pin::Pin,
    task::{Context, Poll},
};

use tonic::{body::Body as TonicBody, codegen::http};

use super::semantic_boundary::{self, BufferedBody, GrpcResultEnvelope};

/// Boxed error type matching the `GrpcService` transport-error bound.
type BoxError = Box<dyn std::error::Error + Send + Sync>;

const BOUNDARY: &str = "grpc";
const COMPONENT: &str = "external_services::grpc_client::deja_transport";
const OPERATION: &str = "unary";

/// The transport wrapper. Generic over the inner tower service so the same
/// type serves the shared hyper pool (dynamic routing / health / recovery)
/// and the UCS `tonic::transport::Channel`s.
///
/// `inner` is `None` only for a boundary built by
/// [`DejaGrpcTransport::substituted`] — see there and [`connect_or_substitute`].
#[derive(Debug, Clone)]
pub struct DejaGrpcTransport<S> {
    inner: Option<S>,
}

impl<S> DejaGrpcTransport<S> {
    /// Wraps a live transport. Call at the construction site, never per request.
    pub fn new(inner: S) -> Self {
        Self { inner: Some(inner) }
    }

    /// A boundary with no transport beneath it.
    ///
    /// Valid ONLY under replay, where every call is served from the recording:
    /// readiness is answered directly, and a call the recording cannot answer
    /// fail-stops via `deja::__private::fail_stop_absent_executor` rather than
    /// connecting.
    pub fn substituted() -> Self {
        Self { inner: None }
    }
}

/// Wrap a transport the host builds eagerly, standing in for it under replay.
///
/// Under replay `connect` is never run: no rpc is issued live, so the transport
/// is dead weight and could not have connected anyway. In every other mode it
/// runs verbatim, and `None` propagates the host's own outcome unchanged.
pub async fn connect_or_substitute<S, F, Fut>(connect: F) -> Option<DejaGrpcTransport<S>>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = Option<S>>,
{
    if deja::__private::runtime_mode().is_replay() {
        Some(DejaGrpcTransport::substituted())
    } else {
        connect().await.map(DejaGrpcTransport::new)
    }
}

/// A boundary with no transport asked to issue a call live. Unreachable in
/// practice — [`DejaGrpcTransport::substituted`] exists only under replay, where
/// the `run` thunk is never invoked — but named so a broken invariant is legible.
#[derive(Debug)]
pub struct AbsentTransportError;

impl std::fmt::Display for AbsentTransportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(
            "deja: gRPC boundary has no transport (constructed for substitution only) \
             and cannot issue this call live",
        )
    }
}

impl std::error::Error for AbsentTransportError {}

/// A substituted transport error: the recorded display chain of the original
/// failure. Approximate by design (the original error struct is gone); tonic
/// maps it through `Status::from_error` exactly as it would a live failure.
#[derive(Debug)]
pub struct ReplayedTransportError(pub String);

impl std::fmt::Display for ReplayedTransportError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "deja replayed transport error: {}", self.0)
    }
}

impl std::error::Error for ReplayedTransportError {}

impl<S, RB> tonic::codegen::Service<http::Request<TonicBody>> for DejaGrpcTransport<S>
where
    S: tonic::codegen::Service<http::Request<TonicBody>, Response = http::Response<RB>>
        + Clone
        + Send
        + 'static,
    S::Future: Send + 'static,
    S::Error: Into<BoxError>,
    RB: http_body::Body<Data = bytes::Bytes> + Send + 'static,
    RB::Error: Into<BoxError>,
{
    type Response = http::Response<TonicBody>;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, BoxError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match &mut self.inner {
            // KNOWN FRAGILITY, deliberately left alone. This delegates ungated by
            // `is_active()`, so the inner service answers readiness even on replay.
            // Safe only because tonic's readiness is buffer-backed, not
            // connection-backed: `Buffer::poll_ready` checks worker liveness and
            // queue capacity, not a connection (tonic 0.14.5). An inner service
            // that answered from a live connection would fail HERE, before `call`
            // can substitute. Tracked deja-side, where the mode lifecycle a gate
            // would have to depend on lives.
            Some(inner) => inner.poll_ready(cx).map_err(Into::into),
            // Nothing to drive: substitution answers every call.
            None => Poll::Ready(Ok(())),
        }
    }

    #[track_caller]
    fn call(&mut self, request: http::Request<TonicBody>) -> Self::Future {
        // Standard tower clone-dance: `self.inner` was driven to readiness by
        // poll_ready, so hand THAT instance to the future and keep the fresh
        // clone (which will be polled to readiness before its own use).
        let inner = match self.inner.as_mut() {
            Some(inner) => {
                let clone = inner.clone();
                Some(std::mem::replace(inner, clone))
            }
            None => None,
        };

        // Boundary engaged only when a deja hook is live (record or replay);
        // otherwise pure passthrough — no buffering, no dispatch, streaming
        // untouched.
        if !is_active() {
            return match inner {
                Some(inner) => Box::pin(passthrough(inner, request)),
                // A transport-less boundary outside replay cannot serve anything.
                None => Box::pin(async { Err(BoxError::from(AbsentTransportError)) }),
            };
        }
        let caller = Location::caller();
        Box::pin(boundary_call(inner, request, caller))
    }
}

fn is_active() -> bool {
    // Covers BOTH worlds: replay/record via the runtime hook (runtime_mode)
    // and record via the recording hook — substitution must engage in replay,
    // so this must never be a record-only check.
    deja::__private::observation_is_active()
}

async fn passthrough<S, RB>(
    mut inner: S,
    request: http::Request<TonicBody>,
) -> Result<http::Response<TonicBody>, BoxError>
where
    S: tonic::codegen::Service<http::Request<TonicBody>, Response = http::Response<RB>>,
    S::Error: Into<BoxError>,
    RB: http_body::Body<Data = bytes::Bytes> + Send + 'static,
    RB::Error: Into<BoxError>,
{
    let response = inner.call(request).await.map_err(Into::into)?;
    Ok(response.map(TonicBody::new))
}

/// One unary gRPC exchange as one deja boundary crossing.
async fn boundary_call<S, RB>(
    inner: Option<S>,
    request: http::Request<TonicBody>,
    caller: &'static Location<'static>,
) -> Result<http::Response<TonicBody>, BoxError>
where
    S: tonic::codegen::Service<http::Request<TonicBody>, Response = http::Response<RB>>
        + Send
        + 'static,
    S::Future: Send + 'static,
    S::Error: Into<BoxError>,
    RB: http_body::Body<Data = bytes::Bytes> + Send + 'static,
    RB::Error: Into<BoxError>,
{
    let (parts, body) = request.into_parts();

    // Unary request: buffer the frames before forwarding — args identity
    // needs the message bytes, and the rebuilt body is what the inner
    // transport actually sends.
    let request_bytes = match http_body_util::BodyExt::collect(body).await {
        Ok(collected) => collected.to_bytes(),
        Err(error) => return Err(error.into()),
    };

    let rpc: String = parts.uri.path().to_owned();
    let authority = parts.uri.authority().map(ToString::to_string);

    // Descriptor-decoded proto3-JSON for the local protos; UCS (unknown
    // schema) falls back to the canonical wire identity inside `grpc_args`.
    #[cfg(any(feature = "dynamic_routing", feature = "revenue_recovery"))]
    let decoded_request =
        semantic_boundary::descriptors::decode_unary_request(&rpc, &request_bytes);
    #[cfg(not(any(feature = "dynamic_routing", feature = "revenue_recovery")))]
    let decoded_request: Option<serde_json::Value> = None;

    let args_value = semantic_boundary::grpc_args(
        &rpc,
        authority.as_deref(),
        &parts.headers,
        &request_bytes,
        decoded_request,
    );
    let correlation = semantic_boundary::correlation(&parts.headers);

    // Identity: rank-2 span-path (from the ambient tracing context) — NO explicit
    // call-site id. rank-1 Explicit is a hand-out tag, not a burden to stamp on
    // call-sites; the rpc path lives in the ARGS (grpc_args), not the identity, so
    // replay matches within a span by occurrence + args — exactly like the
    // db / redis / superposition boundaries. The syntactic hash of "grpc::<path>"
    // is the rank-3 fallback (each in-span call is still disambiguated by
    // occurrence, so distinct rpcs never collapse onto one identity).
    let scope = format!("grpc::{rpc}");
    let identity = deja::__private::CallsiteIdentity {
        version: 1,
        source: deja::__private::CallsiteSource::SyntacticHash,
        id: None,
        scope: Some(scope.clone()),
        occurrence: deja::__private::next_boundary_occurrence(
            correlation.as_deref(),
            deja::__private::CallsiteSource::SyntacticHash,
            Some(&scope),
        ),
        caller_function: Some(COMPONENT.to_string()),
        lexical_path: Some(scope.clone()),
        syntax_hash: Some(deja::__private::stable_callsite_hash(&scope)),
        span_path: deja::__private::current_span_path(),
        span_instance: deja::__private::current_span_instance(),
    };

    // Egress routing is hardcoded: never re-issued on replay.
    let semantics = deja::__private::BoundarySemantics {
        replay_strategy: deja::ReplayStrategy::Substitute,
        kind: Some(BOUNDARY.to_string()),
        declaration: Some(
            deja::BoundaryDeclaration::default().operation(deja::OperationKind::ExternalCall),
        ),
    };
    let spec =
        deja::__private::BoundarySpec::with_semantics(BOUNDARY, COMPONENT, OPERATION, semantics);
    let observation =
        deja::__private::CrossingObservation::with_correlation(spec, identity, caller, correlation);

    let rebuilt_request = http::Request::from_parts(
        parts,
        TonicBody::new(BufferedBody::new(request_bytes, None)),
    );

    match inner {
        // Transport present: a miss may be a genuine novel call, so this keeps
        // `dispatch_async`'s default substitute-miss fail-stop.
        Some(inner) => {
            deja::__private::owned_dispatch_async(
                observation,
                move || args_value,
                move || passthrough(inner, rebuilt_request),
                reconstruct_from_recorded,
                read_response,
                record_envelope,
                deja::__private::round_trip!(Result<http::Response<TonicBody>, BoxError>),
                None::<fn(&Result<http::Response<TonicBody>, BoxError>) -> bool>,
            )
            .await
        }
        // Transport deliberately absent: a miss establishes nothing about the
        // candidate, so it gets its own reason. The `run` thunk is never invoked
        // here (replay + Substitute neither runs nor re-runs); an error rather
        // than an `unreachable!` so a broken invariant stays legible.
        None => {
            deja::__private::owned_dispatch_async(
                observation,
                move || args_value,
                move || async { Err(BoxError::from(AbsentTransportError)) },
                move |input| match input {
                    deja::__private::ReconstructInput::Hit(recorded) => reconstruct_hit(recorded),
                    // Absent-executor stop: with no transport there is nothing to execute.
                    // The target names the rpc and authority; `BOUNDARY` is shared.
                    deja::__private::ReconstructInput::Miss(_) => {
                        let target = match authority.as_deref() {
                            Some(authority) => format!("{rpc} at {authority}"),
                            None => rpc.to_string(),
                        };
                        deja::__private::fail_stop_absent_executor(BOUNDARY, OPERATION, &target)
                    }
                },
                read_response,
                record_envelope,
                deja::__private::round_trip!(Result<http::Response<TonicBody>, BoxError>),
                None::<fn(&Result<http::Response<TonicBody>, BoxError>) -> bool>,
            )
            .await
        }
    }
}

/// The data and trailers as read, or why the body could not be read.
type ReadResponse = Result<(bytes::Bytes, Option<http::HeaderMap>), String>;

/// Reads the body frame by frame and rebuilds one yielding the same frames, errors included.
async fn read_response(
    result: Result<http::Response<TonicBody>, BoxError>,
) -> (
    Result<http::Response<TonicBody>, BoxError>,
    Result<ReadResponse, String>,
) {
    let response = match result {
        Ok(response) => response,
        Err(error) => return (Err(error), Ok(Ok((bytes::Bytes::new(), None)))),
    };
    let (parts, mut body) = response.into_parts();
    let mut frames = Vec::new();
    let mut data = bytes::BytesMut::new();
    let mut trailers = None;
    let failure = loop {
        match http_body_util::BodyExt::frame(&mut body).await {
            None => break None,
            Some(Ok(frame)) => {
                if let Some(chunk) = frame.data_ref() {
                    data.extend_from_slice(chunk);
                }
                if let Some(frame_trailers) = frame.trailers_ref() {
                    trailers = Some(frame_trailers.clone());
                }
                frames.push(Ok(frame));
            }
            Some(Err(error)) => break Some(error),
        }
    };
    let read = match &failure {
        None => Ok((data.freeze(), trailers)),
        Some(error) => Err(error.to_string()),
    };
    frames.extend(failure.map(Err));
    let body = http_body_util::StreamBody::new(futures::stream::iter(frames));
    (
        Ok(http::Response::from_parts(parts, TonicBody::new(body))),
        Ok(read),
    )
}

/// The envelope of what was read; a failed read records as a transport error.
fn record_envelope(
    read: ReadResponse,
    result: &Result<http::Response<TonicBody>, BoxError>,
) -> (serde_json::Value, bool) {
    let envelope = match (result, read) {
        (Ok(response), Ok((data, trailers))) => GrpcResultEnvelope::from_response_parts(
            response.status().as_u16(),
            response.headers(),
            &data,
            trailers.as_ref(),
        ),
        (Ok(_), Err(reason)) => GrpcResultEnvelope::TransportError { error: reason },
        (Err(error), _) => GrpcResultEnvelope::TransportError {
            error: format!("{error}"),
        },
    };
    let is_error = envelope.is_err();
    let value = serde_json::to_value(&envelope).unwrap_or_else(
        |error| serde_json::json!({ "deja_grpc_capture_error": error.to_string() }),
    );
    (value, is_error)
}

/// A hit goes to [`reconstruct_hit`]; a miss becomes a transport failure naming the call.
fn reconstruct_from_recorded(
    input: deja::__private::ReconstructInput<'_>,
) -> deja::__private::Reconstructed<Result<http::Response<TonicBody>, BoxError>> {
    match input {
        deja::__private::ReconstructInput::Hit(recorded) => reconstruct_hit(recorded),
        // A novel call answers as a transport error, not a stop: no peer answered,
        // and callers already handle it, so later calls stay observable.
        deja::__private::ReconstructInput::Miss(miss) => {
            deja::__private::Reconstructed::Synthesized(Err(format!(
                "deja: no recorded gRPC response for {}::{} (occurrence {})",
                miss.component, miss.method, miss.occurrence,
            )
            .into()))
        }
    }
}

/// The hit path: recorded envelope -> the identical wire response through
/// tonic's own decoder, or the recorded transport failure.
fn reconstruct_hit(
    recorded: serde_json::Value,
) -> deja::__private::Reconstructed<Result<http::Response<TonicBody>, BoxError>> {
    let Ok(envelope) = serde_json::from_value::<GrpcResultEnvelope>(recorded) else {
        return deja::__private::Reconstructed::Failed(::std::string::String::from(
            "recorded value is not a `GrpcResultEnvelope`",
        ));
    };
    match &envelope {
        GrpcResultEnvelope::Response { .. } => {
            match semantic_boundary::reconstruct_response(&envelope) {
                Some(response) => {
                    deja::__private::Reconstructed::Value(Ok(response.map(TonicBody::new)))
                }
                None => deja::__private::Reconstructed::Failed(::std::string::String::from(
                    "recorded gRPC response could not be rebuilt into an http::Response \
                     (status line, headers or body frame missing from the envelope)",
                )),
            }
        }
        GrpcResultEnvelope::TransportError { error } => deja::__private::Reconstructed::Value(Err(
            Box::new(ReplayedTransportError(error.clone())),
        )),
    }
}

#[cfg(all(test, feature = "dynamic_routing"))]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::{
        super::dynamic_routing::{
            success_rate_client::success_rate::{
                success_rate_calculator_server::{
                    SuccessRateCalculator, SuccessRateCalculatorServer,
                },
                CalGlobalSuccessRateRequest, CalGlobalSuccessRateResponse, CalSuccessRateRequest,
                CalSuccessRateResponse, InvalidateWindowsRequest, InvalidateWindowsResponse,
                LabelWithScore, UpdateSuccessRateWindowRequest, UpdateSuccessRateWindowResponse,
            },
            SuccessRateCalculatorClient,
        },
        *,
    };

    /// Canned scorer: a deterministic Ok and a typed decline.
    pub(crate) struct CannedScorer;

    #[tonic::async_trait]
    impl SuccessRateCalculator for CannedScorer {
        async fn fetch_success_rate(
            &self,
            request: tonic::Request<CalSuccessRateRequest>,
        ) -> Result<tonic::Response<CalSuccessRateResponse>, tonic::Status> {
            let id = request.into_inner().id;
            Ok(tonic::Response::new(CalSuccessRateResponse {
                labels_with_score: vec![LabelWithScore {
                    score: 0.75,
                    label: format!("stripe:{id}"),
                }],
                routing_approach: 0,
            }))
        }

        async fn update_success_rate_window(
            &self,
            _request: tonic::Request<UpdateSuccessRateWindowRequest>,
        ) -> Result<tonic::Response<UpdateSuccessRateWindowResponse>, tonic::Status> {
            Err(tonic::Status::invalid_argument("window rejected"))
        }

        async fn invalidate_windows(
            &self,
            _request: tonic::Request<InvalidateWindowsRequest>,
        ) -> Result<tonic::Response<InvalidateWindowsResponse>, tonic::Status> {
            Err(tonic::Status::unimplemented("not in this test"))
        }

        async fn fetch_entity_and_global_success_rate(
            &self,
            _request: tonic::Request<CalGlobalSuccessRateRequest>,
        ) -> Result<tonic::Response<CalGlobalSuccessRateResponse>, tonic::Status> {
            Err(tonic::Status::unimplemented("not in this test"))
        }
    }

    /// The frames tonic polls from `body`, errors as their text.
    async fn frames(mut body: TonicBody) -> Vec<Result<String, String>> {
        let mut seen = Vec::new();
        while let Some(frame) = http_body_util::BodyExt::frame(&mut body).await {
            seen.push(match frame {
                Ok(frame) => Ok(format!("{frame:?}")),
                Err(status) => Err(format!("{:?} {}", status.code(), status.message())),
            });
        }
        seen
    }

    fn response(
        frames: Vec<Result<http_body::Frame<bytes::Bytes>, tonic::Status>>,
    ) -> http::Response<TonicBody> {
        let body = http_body_util::StreamBody::new(futures::stream::iter(frames));
        let mut response = http::Response::new(TonicBody::new(body));
        response.headers_mut().insert(
            "content-type",
            http::HeaderValue::from_static("application/grpc"),
        );
        response
    }

    /// The rebuilt body yields the same frames, a mid-stream error at the same frame.
    #[tokio::test]
    async fn the_read_body_yields_the_frames_tonic_would_have_seen() {
        let mut trailers = http::HeaderMap::new();
        trailers.insert("grpc-status", http::HeaderValue::from_static("0"));
        let fixtures = || {
            [
                vec![
                    Ok(http_body::Frame::data(bytes::Bytes::from_static(
                        b"\0\0\0\0\x02ab",
                    ))),
                    Ok(http_body::Frame::data(bytes::Bytes::from_static(b"cd"))),
                    Ok(http_body::Frame::trailers(trailers.clone())),
                ],
                vec![
                    Ok(http_body::Frame::data(bytes::Bytes::from_static(b"\0\0\0"))),
                    Err(tonic::Status::unavailable("connection reset")),
                ],
            ]
        };
        for (original, through) in fixtures().into_iter().zip(fixtures()) {
            let fails = original.iter().any(Result::is_err);
            let direct = frames(response(original).into_body()).await;
            let (rebuilt, read) = read_response(Ok(response(through))).await;
            assert_eq!(matches!(read, Ok(Err(_))), fails, "{read:?}");
            let rebuilt = rebuilt.expect("an Ok stays Ok");
            assert_eq!(rebuilt.headers()["content-type"], "application/grpc");
            assert_eq!(frames(rebuilt.into_body()).await, direct);
        }
    }

    /// What was read becomes the envelope, a transport error included.
    #[tokio::test]
    async fn the_recorded_envelope_is_what_was_read() {
        let mut trailers = http::HeaderMap::new();
        trailers.insert("grpc-status", http::HeaderValue::from_static("0"));
        let data = bytes::Bytes::from_static(b"\0\0\0\0\x02ab");
        let (value, read) = read_response(Ok(response(vec![
            Ok(http_body::Frame::data(data.clone())),
            Ok(http_body::Frame::trailers(trailers.clone())),
        ])))
        .await;
        let (recorded, is_error) = record_envelope(read.expect("the body reads"), &value);
        let response = value.expect("an Ok stays Ok");
        let expected = GrpcResultEnvelope::from_response_parts(
            response.status().as_u16(),
            response.headers(),
            &data,
            Some(&trailers),
        );
        assert_eq!(
            recorded,
            serde_json::to_value(&expected).expect("serializes")
        );
        assert_eq!(is_error, expected.is_err());

        let (value, read) = read_response(Err(BoxError::from("dns error"))).await;
        let (recorded, is_error) = record_envelope(read.expect("nothing to read"), &value);
        assert!(is_error);
        assert_eq!(
            recorded,
            serde_json::to_value(GrpcResultEnvelope::TransportError {
                error: "dns error".to_owned()
            })
            .expect("serializes")
        );
    }

    /// A body failing mid-read records as a transport error.
    #[tokio::test]
    async fn a_body_that_fails_records_as_a_transport_error() {
        let (value, read) = read_response(Ok(response(vec![
            Ok(http_body::Frame::data(bytes::Bytes::from_static(b"\0\0\0"))),
            Err(tonic::Status::unavailable("connection reset")),
        ])))
        .await;
        let (recorded, is_error) = record_envelope(read.expect("the read always reports"), &value);
        assert!(is_error);
        let Ok(GrpcResultEnvelope::TransportError { error }) =
            serde_json::from_value::<GrpcResultEnvelope>(recorded)
        else {
            panic!("a failed read records a transport error");
        };
        assert!(error.contains("connection reset"), "{error}");
    }

    /// Inactive-hook passthrough: real tonic server + wrapped hyper pool,
    /// traffic must be untouched in both arms. (The RECORD side needs a
    /// separate process, because the global hook OnceLock latches on first
    /// read and this test must observe the pre-install state.)
    #[tokio::test]
    async fn inactive_wrapper_is_a_pure_passthrough() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        tokio::spawn(
            tonic::transport::Server::builder()
                .add_service(SuccessRateCalculatorServer::new(CannedScorer))
                .serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener)),
        );

        let pool =
            hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
                .http2_only(true)
                .build_http();
        let transport = DejaGrpcTransport::new(pool);
        let uri: http::Uri = format!("http://127.0.0.1:{port}").parse().unwrap();
        let mut client = SuccessRateCalculatorClient::with_origin(transport, uri);

        assert!(!is_active(), "no deja hook may be active in the lib tests");
        let response = client
            .fetch_success_rate(tonic::Request::new(CalSuccessRateRequest {
                id: "m1".to_owned(),
                params: "card".to_owned(),
                labels: vec![],
                config: None,
            }))
            .await
            .unwrap()
            .into_inner();
        assert_eq!(response.labels_with_score.len(), 1);
        assert_eq!(
            response.labels_with_score.first().unwrap().label,
            "stripe:m1"
        );

        let status = client
            .update_success_rate_window(UpdateSuccessRateWindowRequest {
                id: "m1".to_owned(),
                params: String::new(),
                labels_with_status: vec![],
                global_labels_with_status: vec![],
                config: None,
            })
            .await
            .unwrap_err();
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        assert_eq!(status.message(), "window rejected");
    }
}