xwt_tests/tests/
closed_bi_read_stream.rs

1use xwt_core::prelude::*;
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error<Endpoint>
5where
6    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
7    Endpoint::Connecting: std::fmt::Debug,
8    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenBi + std::fmt::Debug,
9{
10    #[error("connect: {0}")]
11    Connect(#[source] xwt_error::Connect<Endpoint>),
12    #[error("open bi stream: {0}")]
13    OpenBiStream(#[source] BiStreamOpenErrorFor<ConnectSessionFor<Endpoint>>),
14    #[error("opening bi stream: {0}")]
15    OpeningBiStream(#[source] BiStreamOpeningErrorFor<ConnectSessionFor<Endpoint>>),
16    #[error("write stream aborted: {0}")]
17    WriteStreamAborted(#[source] WriteAbortedErrorFor<SendStreamFor<ConnectSessionFor<Endpoint>>>),
18    #[error("error code conversion to u32 failed")]
19    ErrorCodeConversion,
20    #[error("error code mismatch: got code {0}")]
21    ErrorCodeMismatch(u32),
22}
23
24pub async fn run<Endpoint>(
25    endpoint: Endpoint,
26    url: &str,
27    expected_error_code: u32,
28) -> Result<(), Error<Endpoint>>
29where
30    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
31    Endpoint::Connecting: std::fmt::Debug,
32    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenBi + std::fmt::Debug,
33    SendStreamFor<ConnectSessionFor<Endpoint>>: xwt_core::stream::WriteAborted,
34{
35    let session = crate::utils::connect(&endpoint, url)
36        .await
37        .map_err(Error::Connect)?;
38
39    let opening = session.open_bi().await.map_err(Error::OpenBiStream)?;
40    let (send_stream, _recv_stream) = opening.wait_bi().await.map_err(Error::OpeningBiStream)?;
41
42    let error_code = send_stream
43        .aborted()
44        .await
45        .map_err(Error::WriteStreamAborted)?;
46
47    let error_code: u32 = error_code
48        .try_into()
49        .map_err(|_| Error::ErrorCodeConversion)?;
50
51    if error_code != expected_error_code {
52        return Err(Error::ErrorCodeMismatch(error_code));
53    }
54
55    Ok(())
56}