Skip to main content

xwt_tests/tests/
aborted_bi_read_stream.rs

1//! This test ensures that the read stream observes the abortion of
2//! the corresponding write side at the peer via
3//! the [`xwt_core::stream::ReadAborted`] API.
4//!
5//! Only an actual abort is covered here: the native driver completes this
6//! call solely on a `RESET_STREAM`, while the web driver also completes it on
7//! a clean stream finish.
8
9use xwt_core::prelude::*;
10
11#[derive(Debug, thiserror::Error)]
12pub enum Error<Endpoint>
13where
14    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
15    Endpoint::Connecting: std::fmt::Debug,
16    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenBi + std::fmt::Debug,
17{
18    #[error("connect: {0}")]
19    Connect(#[source] xwt_error::Connect<Endpoint>),
20    #[error("open: {0}")]
21    Open(#[source] xwt_error::OpenBi<ConnectSessionFor<Endpoint>>),
22    #[error("read stream aborted: {0}")]
23    ReadStreamAborted(#[source] ReadAbortedErrorFor<RecvStreamFor<ConnectSessionFor<Endpoint>>>),
24    #[error("error code mismatch: got code {0}")]
25    ErrorCodeMismatch(xwt_core::stream::ErrorCode),
26}
27
28pub async fn run<Endpoint>(
29    endpoint: Endpoint,
30    url: &str,
31    expected_error_code: xwt_core::stream::ErrorCode,
32) -> Result<(), Error<Endpoint>>
33where
34    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
35    Endpoint::Connecting: std::fmt::Debug,
36    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenBi + std::fmt::Debug,
37{
38    let session = crate::utils::connect(&endpoint, url)
39        .await
40        .map_err(Error::Connect)?;
41
42    let (_send_stream, recv_stream) = crate::utils::open_bi(&session).await.map_err(Error::Open)?;
43
44    let error_code = recv_stream
45        .aborted()
46        .await
47        .map_err(Error::ReadStreamAborted)?;
48
49    if error_code != expected_error_code {
50        return Err(Error::ErrorCodeMismatch(error_code));
51    }
52
53    Ok(())
54}