irc: split test timeouts and increase for password verification (#75)

Reviewed-on: lavina/lavina#75
This commit is contained in:
Nikita Vilunov 2024-06-09 09:22:35 +00:00
parent 26cc2f178c
commit c0aaa26010
1 changed files with 18 additions and 7 deletions

View File

@ -20,7 +20,8 @@ struct TestScope<'a> {
reader: BufReader<ReadHalf<'a>>, reader: BufReader<ReadHalf<'a>>,
writer: WriteHalf<'a>, writer: WriteHalf<'a>,
buffer: Vec<u8>, buffer: Vec<u8>,
pub timeout: Duration, pub timeout_optimistic: Duration,
pub timeout_pessimistic: Duration,
} }
impl<'a> TestScope<'a> { impl<'a> TestScope<'a> {
@ -28,12 +29,14 @@ impl<'a> TestScope<'a> {
let (reader, writer) = stream.split(); let (reader, writer) = stream.split();
let reader = BufReader::new(reader); let reader = BufReader::new(reader);
let buffer = vec![]; let buffer = vec![];
let timeout = Duration::from_millis(1000); let timeout_optimistic = Duration::from_millis(50);
let timeout_pessimistic = Duration::from_millis(2000);
TestScope { TestScope {
reader, reader,
writer, writer,
buffer, buffer,
timeout, timeout_optimistic,
timeout_pessimistic,
} }
} }
@ -46,14 +49,22 @@ impl<'a> TestScope<'a> {
async fn expect(&mut self, str: &str) -> Result<()> { async fn expect(&mut self, str: &str) -> Result<()> {
tracing::debug!("Expecting {}", str); 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); assert_eq!(std::str::from_utf8(&self.buffer[..len - 2])?, str);
self.buffer.clear(); self.buffer.clear();
Ok(()) Ok(())
} }
async fn expect_that(&mut self, validate: impl FnOnce(&str) -> bool) -> Result<()> { 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])?; let msg = std::str::from_utf8(&self.buffer[..len - 2])?;
if !validate(msg) { if !validate(msg) {
return Err(anyhow!("unexpected message: {:?}", msg)); return Err(anyhow!("unexpected message: {:?}", msg));
@ -79,7 +90,7 @@ impl<'a> TestScope<'a> {
async fn expect_eof(&mut self) -> Result<()> { async fn expect_eof(&mut self) -> Result<()> {
let mut buf = [0; 1]; 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 { if len != 0 {
return Err(anyhow!("not a eof")); return Err(anyhow!("not a eof"));
} }
@ -88,7 +99,7 @@ impl<'a> TestScope<'a> {
async fn expect_nothing(&mut self) -> Result<()> { async fn expect_nothing(&mut self) -> Result<()> {
let mut buf = [0; 1]; 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)), Ok(res) => Err(anyhow!("received something: {:?}", res)),
Err(_) => Ok(()), Err(_) => Ok(()),
} }