forked from lavina/lavina
Compare commits
No commits in common. "aea11b0400e311004950870ee5132003ad5be0f5" and "45fda1adb0786b5c95e17926ccb9e8735f67c4de" have entirely different histories.
aea11b0400
...
45fda1adb0
|
@ -1,14 +1,14 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::Result;
|
||||||
use prometheus::Registry as MetricsRegistry;
|
use prometheus::Registry as MetricsRegistry;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
|
use tokio::io::{AsyncWriteExt, BufReader};
|
||||||
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
use lavina_core::repo::{Storage, StorageConfig};
|
use lavina_core::repo::{Storage, StorageConfig};
|
||||||
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
|
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
|
||||||
use projection_irc::{launch, read_irc_message, RunningServer, ServerConfig};
|
use projection_irc::{launch, read_irc_message, ServerConfig};
|
||||||
|
|
||||||
struct TestScope<'a> {
|
struct TestScope<'a> {
|
||||||
reader: BufReader<ReadHalf<'a>>,
|
reader: BufReader<ReadHalf<'a>>,
|
||||||
|
@ -44,34 +44,10 @@ impl<'a> TestScope<'a> {
|
||||||
self.buffer.clear();
|
self.buffer.clear();
|
||||||
Ok(())
|
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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn expect_nothing(&mut self) -> Result<()> {
|
#[tokio::test]
|
||||||
let mut buf = [0; 1];
|
async fn scenario_basic() -> Result<()> {
|
||||||
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 {
|
let config = ServerConfig {
|
||||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||||
server_name: "testserver".into(),
|
server_name: "testserver".into(),
|
||||||
|
@ -83,27 +59,14 @@ impl TestServer {
|
||||||
.await?;
|
.await?;
|
||||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
||||||
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
||||||
let server = launch(config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
let server = launch(config, players, rooms, metrics, 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
|
// test scenario
|
||||||
|
|
||||||
server.storage.create_user("tester").await?;
|
storage.create_user("tester").await?;
|
||||||
server.storage.set_password("tester", "password").await?;
|
storage.set_password("tester", "password").await?;
|
||||||
|
|
||||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
let mut stream = TcpStream::connect(server.addr).await?;
|
||||||
let mut s = TestScope::new(&mut stream);
|
let mut s = TestScope::new(&mut stream);
|
||||||
|
|
||||||
s.send("PASS password").await?;
|
s.send("PASS password").await?;
|
||||||
|
@ -115,15 +78,11 @@ async fn scenario_basic() -> Result<()> {
|
||||||
s.expect(":testserver 003 tester :Welcome to Kek Server").await?;
|
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 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(":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?;
|
stream.shutdown().await?;
|
||||||
|
|
||||||
// wrap up
|
// wrap up
|
||||||
|
|
||||||
server.server.terminate().await?;
|
server.terminate().await.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue