diff --git a/crates/projection-irc/tests/lib.rs b/crates/projection-irc/tests/lib.rs index 8ee8fdd..38fd0f7 100644 --- a/crates/projection-irc/tests/lib.rs +++ b/crates/projection-irc/tests/lib.rs @@ -20,7 +20,8 @@ struct TestScope<'a> { reader: BufReader>, writer: WriteHalf<'a>, buffer: Vec, - pub timeout: Duration, + pub timeout_optimistic: Duration, + pub timeout_pessimistic: Duration, } impl<'a> TestScope<'a> { @@ -28,12 +29,14 @@ impl<'a> TestScope<'a> { let (reader, writer) = stream.split(); let reader = BufReader::new(reader); let buffer = vec![]; - let timeout = Duration::from_millis(1000); + let timeout_optimistic = Duration::from_millis(50); + let timeout_pessimistic = Duration::from_millis(2000); TestScope { reader, writer, buffer, - timeout, + timeout_optimistic, + timeout_pessimistic, } } @@ -46,14 +49,22 @@ impl<'a> TestScope<'a> { async fn expect(&mut self, str: &str) -> Result<()> { tracing::debug!("Expecting {}", str); - let len = tokio::time::timeout(self.timeout, read_irc_message(&mut self.reader, &mut self.buffer)).await??; + let len = tokio::time::timeout( + self.timeout_pessimistic, + read_irc_message(&mut self.reader, &mut self.buffer), + ) + .await??; assert_eq!(std::str::from_utf8(&self.buffer[..len - 2])?, str); self.buffer.clear(); Ok(()) } async fn expect_that(&mut self, validate: impl FnOnce(&str) -> bool) -> Result<()> { - let len = tokio::time::timeout(self.timeout, read_irc_message(&mut self.reader, &mut self.buffer)).await??; + let len = tokio::time::timeout( + self.timeout_pessimistic, + read_irc_message(&mut self.reader, &mut self.buffer), + ) + .await??; let msg = std::str::from_utf8(&self.buffer[..len - 2])?; if !validate(msg) { return Err(anyhow!("unexpected message: {:?}", msg)); @@ -79,7 +90,7 @@ impl<'a> TestScope<'a> { async fn expect_eof(&mut self) -> Result<()> { let mut buf = [0; 1]; - let len = tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await??; + let len = tokio::time::timeout(self.timeout_pessimistic, self.reader.read(&mut buf)).await??; if len != 0 { return Err(anyhow!("not a eof")); } @@ -88,7 +99,7 @@ impl<'a> TestScope<'a> { async fn expect_nothing(&mut self) -> Result<()> { let mut buf = [0; 1]; - match tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await { + match tokio::time::timeout(self.timeout_optimistic, self.reader.read(&mut buf)).await { Ok(res) => Err(anyhow!("received something: {:?}", res)), Err(_) => Ok(()), }