Skip to content

Commit

Permalink
feat(test): Cover the test for list timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Feb 21, 2024
1 parent 332a437 commit 33eeb7e
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion core/src/layers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ mod tests {

use async_trait::async_trait;
use bytes::Bytes;
use futures::StreamExt;
use tokio::time::sleep;
use tokio::time::timeout;

Expand All @@ -397,7 +398,7 @@ mod tests {
impl Accessor for MockService {
type Reader = MockReader;
type Writer = ();
type Lister = ();
type Lister = MockLister;
type BlockingReader = ();
type BlockingWriter = ();
type BlockingLister = ();
Expand All @@ -424,6 +425,10 @@ mod tests {

Ok(RpDelete::default())
}

async fn list(&self, _: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
Ok((RpList::default(), MockLister))
}
}

#[derive(Debug, Clone, Default)]
Expand All @@ -443,6 +448,15 @@ mod tests {
}
}

#[derive(Debug, Clone, Default)]
struct MockLister;

impl oio::List for MockLister {
fn poll_next(&mut self, _: &mut Context<'_>) -> Poll<Result<Option<oio::Entry>>> {
Poll::Pending
}
}

#[tokio::test]
async fn test_operation_timeout() {
let acc = Arc::new(TypeEraseLayer.layer(MockService)) as FusedAccessor;
Expand Down Expand Up @@ -482,4 +496,28 @@ mod tests {
.await
.expect("this test should not exceed 2 seconds")
}

#[tokio::test]
async fn test_list_timeout() {
let acc = Arc::new(TypeEraseLayer.layer(MockService)) as FusedAccessor;
let op = Operator::from_inner(acc).layer(
TimeoutLayer::new()
.with_timeout(Duration::from_secs(1))
.with_io_timeout(Duration::from_secs(1)),
);

let fut = async {
let mut lister = op.lister("test").await.unwrap();

let res = lister.next().await.unwrap();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), ErrorKind::Unexpected);
assert!(err.to_string().contains("timeout"))
};

timeout(Duration::from_secs(2), fut)
.await
.expect("this test should not exceed 2 seconds")
}
}

0 comments on commit 33eeb7e

Please sign in to comment.