use std::{io, sync::atomic::Ordering, time::Duration};
use bytes::Bytes;
use tokio::time::timeout;
use super::{
send_streaming::assert_io_error,
send_streaming_infra::{
blocking_reader,
create_send_client,
create_send_client_with_error_hook,
spawn_receiving_server,
test_body,
},
};
use crate::client::SendStreamingConfig;
type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
const TIMEOUT_WATCHDOG: Duration = Duration::from_secs(2);
#[tokio::test]
async fn timeout_returns_timed_out() -> TestResult {
let mut server = spawn_receiving_server().await?;
let mut client = create_send_client(server.addr).await?;
let (reader, _tx) = blocking_reader();
let config = SendStreamingConfig::default()
.with_chunk_size(10)
.with_timeout(Duration::from_millis(50));
let result = timeout(
TIMEOUT_WATCHDOG,
client.send_streaming(b"\x01", reader, config),
)
.await
.map_err(|_| "send_streaming ignored its configured timeout")?;
assert_io_error(result, io::ErrorKind::TimedOut)?;
drop(client);
let received = server.collect_frames().await?;
if !received.is_empty() {
return Err(format!(
"a timeout before the first read must emit no frames, got {received:?}"
)
.into());
}
Ok(())
}
#[tokio::test]
async fn timeout_after_partial_progress_stops_further_frames() -> TestResult {
let mut server = spawn_receiving_server().await?;
let mut client = create_send_client(server.addr).await?;
let chunk_size = 10;
let first_chunk = Bytes::from(test_body(chunk_size));
let (reader, sender) = blocking_reader();
sender
.send(Ok(first_chunk.clone()))
.await
.map_err(|_| "the pre-queued chunk should be accepted")?;
let config = SendStreamingConfig::default()
.with_chunk_size(chunk_size)
.with_timeout(Duration::from_millis(200));
let result = timeout(
TIMEOUT_WATCHDOG,
client.send_streaming(b"\x01", reader, config),
)
.await
.map_err(|_| "send_streaming ignored its configured timeout")?;
assert_io_error(result, io::ErrorKind::TimedOut)?;
drop(sender);
drop(client);
let received = server.collect_frames().await?;
if received.len() != 1 {
return Err(format!(
"the timeout must stop emission after the one complete frame, got {} frames",
received.len()
)
.into());
}
let expected_frame = [b"\x01".as_slice(), first_chunk.as_ref()].concat();
if received.first() != Some(&expected_frame) {
return Err(format!(
"the emitted frame must carry the protocol header and the first chunk, got {:?}",
received.first()
)
.into());
}
Ok(())
}
#[tokio::test]
async fn invokes_error_hook_on_timeout() -> TestResult {
let server = spawn_receiving_server().await?;
let (mut client, hook_invoked) = create_send_client_with_error_hook(server.addr).await?;
let (reader, _tx) = blocking_reader();
let config = SendStreamingConfig::default()
.with_chunk_size(10)
.with_timeout(Duration::from_millis(50));
let result = timeout(
TIMEOUT_WATCHDOG,
client.send_streaming(b"\x01", reader, config),
)
.await
.map_err(|_| "send_streaming ignored its configured timeout")?;
if result.is_ok() {
return Err("expected timeout error, got Ok".into());
}
if !hook_invoked.load(Ordering::SeqCst) {
return Err("error hook should be invoked on timeout".into());
}
Ok(())
}