forked from lavina/lavina
1
0
Fork 0

irc: improve integration test

This commit is contained in:
Nikita Vilunov 2023-10-13 16:30:31 +02:00
parent 013a20cbea
commit fea64e4232
1 changed files with 60 additions and 19 deletions

View File

@ -1,14 +1,14 @@
use std::time::Duration;
use anyhow::Result;
use anyhow::{anyhow, Result};
use prometheus::Registry as MetricsRegistry;
use tokio::io::{AsyncWriteExt, BufReader};
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::tcp::{ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use lavina_core::repo::{Storage, StorageConfig};
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
use projection_irc::{launch, read_irc_message, ServerConfig};
use projection_irc::{launch, read_irc_message, RunningServer, ServerConfig};
struct TestScope<'a> {
reader: BufReader<ReadHalf<'a>>,
@ -44,10 +44,34 @@ impl<'a> TestScope<'a> {
self.buffer.clear();
Ok(())
}
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??;
if len != 0 {
return Err(anyhow!("not a eof"));
}
Ok(())
}
#[tokio::test]
async fn scenario_basic() -> Result<()> {
async fn expect_nothing(&mut self) -> Result<()> {
let mut buf = [0; 1];
match tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await {
Ok(res) => Err(anyhow!("received something: {:?}", res)),
Err(_) => Ok(()),
}
}
}
struct TestServer {
metrics: MetricsRegistry,
storage: Storage,
rooms: RoomRegistry,
players: PlayerRegistry,
server: RunningServer,
}
impl TestServer {
async fn start() -> Result<TestServer> {
let config = ServerConfig {
listen_on: "127.0.0.1:0".parse().unwrap(),
server_name: "testserver".into(),
@ -59,14 +83,27 @@ async fn scenario_basic() -> Result<()> {
.await?;
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
let server = launch(config, players, rooms, metrics, storage.clone()).await.unwrap();
let server = launch(config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await.unwrap();
Ok(TestServer {
metrics,
storage,
rooms,
players,
server,
})
}
}
#[tokio::test]
async fn scenario_basic() -> Result<()> {
let mut server = TestServer::start().await?;
// test scenario
storage.create_user("tester").await?;
storage.set_password("tester", "password").await?;
server.storage.create_user("tester").await?;
server.storage.set_password("tester", "password").await?;
let mut stream = TcpStream::connect(server.addr).await?;
let mut stream = TcpStream::connect(server.server.addr).await?;
let mut s = TestScope::new(&mut stream);
s.send("PASS password").await?;
@ -78,11 +115,15 @@ async fn scenario_basic() -> Result<()> {
s.expect(":testserver 003 tester :Welcome to Kek Server").await?;
s.expect(":testserver 004 tester testserver kek-0.1.alpha.3 r CFILPQbcefgijklmnopqrstvz").await?;
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
s.expect_nothing().await?;
s.send("QUIT :Leaving").await?;
s.expect(":testserver ERROR :Leaving the server").await?;
s.expect_eof().await?;
stream.shutdown().await?;
// wrap up
server.terminate().await.unwrap();
server.server.terminate().await?;
Ok(())
}