Add unit tests and use Rust 2021

This commit is contained in:
KernelErr
2021-10-25 22:12:44 +08:00
parent 4352050b04
commit 7a5ce8e1b6
6 changed files with 120 additions and 6 deletions

View File

@@ -206,3 +206,30 @@ where
Err(_) => Ok(0),
}
}
#[cfg(test)]
mod test {
use std::thread::{self, sleep};
use std::time::Duration;
use super::*;
#[tokio::test]
async fn test_echo_server() {
use crate::config::Config;
let config = Config::new("tests/config.yaml").unwrap();
let mut server = Server::new(config.base);
thread::spawn(move || {
let _ = server.run();
});
sleep(Duration::from_secs(1)); // wait for server to start
let mut conn = TcpStream::connect("127.0.0.1:54956").await.unwrap();
let mut buf = [0u8; 1];
for i in 0..=255u8 {
conn.write(&[i]).await.unwrap();
conn.read(&mut buf).await.unwrap();
assert_eq!(&buf, &[i]);
}
conn.shutdown().await.unwrap();
}
}