xwt_tests/tests/
session_drop.rs

1//! This test ensures that when the session is dropped we get the expected
2//! effect - which is that the streams, their readers and writers and
3//! the datagrams readers and writers are closed.
4
5use xwt_core::prelude::*;
6
7#[derive(Debug, thiserror::Error)]
8pub enum Error<Endpoint>
9where
10    Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
11    Endpoint::Connecting: std::fmt::Debug,
12    ConnectSessionFor<Endpoint>: xwt_core::session::stream::OpenBi + std::fmt::Debug,
13{
14    #[error("connect: {0}")]
15    Connect(#[source] xwt_error::Connect<Endpoint>),
16    #[error("open: {0}")]
17    Open(#[source] xwt_error::OpenBi<ConnectSessionFor<Endpoint>>),
18    #[error("read has not failed")]
19    ReadNotFailed,
20    #[error("got a read error that was not expected: {0}")]
21    UnexpectedReadError(ReadErrorFor<RecvStreamFor<ConnectSessionFor<Endpoint>>>),
22}
23
24pub async fn run<Endpoint>(
25    endpoint: Endpoint,
26    url: &str,
27    checker: impl FnOnce(&ReadErrorFor<RecvStreamFor<ConnectSessionFor<Endpoint>>>) -> bool,
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{
34    let session = crate::utils::connect(&endpoint, url)
35        .await
36        .map_err(Error::Connect)?;
37
38    let (mut _send_stream, mut recv_stream) =
39        crate::utils::open_bi(&session).await.map_err(Error::Open)?;
40
41    drop(session);
42
43    let mut buf = [0; 1024];
44    let result = recv_stream.read(&mut buf).await;
45
46    let Err(error) = result else {
47        return Err(Error::ReadNotFailed);
48    };
49
50    if !(checker)(&error) {
51        tracing::info!(message = "The error on the session drop was", ?error);
52        return Err(Error::UnexpectedReadError(error));
53    }
54
55    Ok(())
56}