Skip to content

Commit

Permalink
Merge pull request #7 from artemijan/fb-tests
Browse files Browse the repository at this point in the history
Fb tests
  • Loading branch information
artemijan authored Feb 22, 2025
2 parents 6f1b752 + e1a2943 commit 9ef5c80
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[workspace.package]
edition = "2021"

[workspace]
members = ["entities", "game", "l2-core", "login", "macro-common", "migration", "test-utils"]
resolver = "2"


[profile.dev.package.num-bigint-dig]
opt-level = 3

Expand Down
2 changes: 1 addition & 1 deletion entities/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "entities"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false

[features]
Expand Down
2 changes: 1 addition & 1 deletion game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "game"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false
[[bin]]
name = "game"
Expand Down
2 changes: 1 addition & 1 deletion game/src/packets/from_client/char_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use l2_core::shared_packets::common::ReadablePacket;
use l2_core::shared_packets::read::ReadablePacketBuffer;
use l2_core::traits::handlers::{PacketHandler, PacketSender};
use sea_orm::DbErr;
use std::sync::Arc;
use tracing::error;

#[allow(unused)]
Expand Down Expand Up @@ -142,6 +141,7 @@ mod tests {
use l2_core::traits::ServerConfig;
use ntest::timeout;
use std::net::Ipv4Addr;
use std::sync::Arc;
use test_utils::utils::get_test_db;
use tokio::io::{split, AsyncWriteExt};
fn build_packet() -> CreateCharRequest {
Expand Down
2 changes: 1 addition & 1 deletion l2-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "l2-core"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions l2-core/config
17 changes: 17 additions & 0 deletions l2-core/src/config/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ pub struct Client {
pub struct GSMessages {
pub timeout: u8,
}

#[cfg(test)]
mod tests {
use crate::config::login::LoginServer;
use crate::traits::ServerConfig;

#[test]
fn test_config_load() {
let cfg = LoginServer::load("../config/login.yaml");
assert_eq!(cfg.name, "Login server");
}
#[test]
#[should_panic(expected = "Failed to open config file: ./login.yaml. Error: No such file or directory (os error 2).")]
fn test_config_load_err() {
LoginServer::load("./login.yaml");
}
}
13 changes: 12 additions & 1 deletion l2-core/src/config/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pub trait ConfigDirLoader: Loadable + Default + LoadFileHandler {
#[cfg(test)]
mod test {
use super::*;
use crate as l2_core;
#[allow(unused_imports)]
use crate as l2_core; //hack to test l2_core crate
use macro_common::config_file;
use serde::Deserialize;

Expand All @@ -91,10 +92,20 @@ mod test {
struct TestConf {
name: String,
}
#[derive(Debug, Clone, Deserialize)]
#[config_file(path = "config/game.yaml", msg = "Loaded")]
struct TestConf2 {
name: String,
}

#[test]
fn test_config_file_loader() {
let conf = <TestConf as ConfigFileLoader>::load();
assert_eq!(conf.name, "test config");
}
#[test]
fn test_config_file_load() {
let conf = <TestConf2 as ConfigFileLoader>::load();
assert_eq!(conf.name, "Game server");
}
}
4 changes: 2 additions & 2 deletions l2-core/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Deserializer};
use std::net::Ipv4Addr;
use std::str::FromStr;

#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Database {
#[serde(rename = "url")]
pub url: String,
Expand All @@ -30,7 +30,7 @@ fn default_timeout() -> u64 {
fn default_max_lifetime() -> u64 {
60 * 60
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Runtime {
pub worker_threads: usize,
}
Expand Down
182 changes: 182 additions & 0 deletions l2-core/src/traits/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,185 @@ pub trait Server {
})
}
}
#[cfg(test)]
mod tests {
use crate::dto::{Database, InboundConnection, Runtime};
use crate::traits::handlers::{InboundHandler, PacketHandler, PacketSender};
use crate::traits::server::Server;
use crate::traits::{IpBan, ServerConfig, Shutdown};
use anyhow::Error;
use async_trait::async_trait;
use entities::DBPool;
use std::fmt::{Debug, Formatter};
use std::net::Ipv4Addr;
use std::sync::Arc;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::sync::{Mutex, Notify};

struct MockServer;
struct MockController;

struct MockConfigType {
rt: Runtime,
db: Database,
inbound: InboundConnection,
}
impl MockConfigType {
fn default() -> Self {
Self {
rt: Runtime { worker_threads: 2 },
db: Database {
url: "sqlite::memory:".to_string(),
max_connections: 4,
min_connections: 2,
connect_timeout: 5,
idle_timeout: 5,
max_lifetime: 60,
},
inbound: InboundConnection {
ip: "127.0.0.1".to_string(),
port: 2106,
reuse_addr: true,
reuse_port: true,
no_delay: true,
},
}
}
}

impl ServerConfig for MockConfigType {
fn load(_: &str) -> Self {
Self::default()
}

fn from_string(_: &str) -> Self {
Self::default()
}

fn runtime(&self) -> Option<&Runtime> {
Some(&self.rt)
}

fn database(&self) -> &Database {
&self.db
}
}
impl IpBan for MockController {
fn is_ip_banned(&self, _: &str) -> bool {
false
}
}
impl Server for MockServer {
type ConfigType = MockConfigType;
type ControllerType = MockController;
}

struct MockHandler;

#[async_trait]
impl PacketSender for MockHandler {
async fn encrypt(&self, _: &mut [u8]) -> anyhow::Result<()> {
todo!()
}

fn is_encryption_enabled(&self) -> bool {
todo!()
}

async fn get_stream_writer_mut(&self) -> &Arc<Mutex<dyn AsyncWrite + Send + Unpin>> {
todo!()
}
}

impl Debug for MockHandler {
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

impl Shutdown for MockHandler {
fn get_shutdown_listener(&self) -> Arc<Notify> {
todo!()
}
}

#[async_trait]
impl PacketHandler for MockHandler {
type ConfigType = MockConfigType;
type ControllerType = MockController;

fn get_handler_name() -> &'static str {
"Test handler"
}

fn get_controller(&self) -> &Arc<Self::ControllerType> {
todo!()
}

fn new<R, W>(_: R, _: W, _: Ipv4Addr, _: DBPool, _: Arc<Self::ControllerType>) -> Self
where
R: AsyncRead + Unpin + Send + 'static,
W: AsyncWrite + Unpin + Send + 'static,
{
todo!()
}

async fn on_connect(&mut self) -> anyhow::Result<()> {
todo!()
}

async fn on_disconnect(&mut self) {
todo!()
}

fn get_stream_reader_mut(&self) -> &Arc<Mutex<dyn AsyncRead + Send + Unpin>> {
todo!()
}

fn get_timeout(&self) -> Option<u64> {
todo!()
}

fn get_db_pool(&self) -> &DBPool {
todo!()
}

async fn on_receive_bytes(&mut self, _: usize, _: &mut [u8]) -> Result<(), Error> {
todo!()
}

async fn read_packet(&mut self) -> anyhow::Result<(usize, Vec<u8>)> {
todo!()
}

async fn handle_client(&mut self) -> anyhow::Result<()> {
todo!()
}
}

impl InboundHandler for MockHandler {
type ConfigType = MockConfigType;

fn get_connection_config(cfg: &Self::ConfigType) -> &InboundConnection {
&cfg.inbound
}
}
#[test]
fn test_bootstrap() {
MockServer::bootstrap("", |cfg, pool| async move {
assert_eq!(cfg.db.max_connections, 4);
assert_eq!(cfg.rt.worker_threads, 2);
assert!(pool.ping().await.is_ok());
});
}
#[test]
fn test_listener() {
//this is just a simple check if we can bind to local host on port 2106
// after it's bind, we just simply abort the task.
MockServer::bootstrap("", |cfg, pool| async move {
let l_loop =
MockServer::listener_loop::<MockHandler>(cfg, Arc::new(MockController), pool);
l_loop.abort();
});
}
}
2 changes: 1 addition & 1 deletion login/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "login"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false
[[bin]]
name = "login"
Expand Down
2 changes: 1 addition & 1 deletion macro-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "macro-common"
version = "0.1.0"
edition = "2021"
edition.workspace = true
[lib]
proc-macro = true

Expand Down
2 changes: 1 addition & 1 deletion migration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false

[lib]
Expand Down
2 changes: 1 addition & 1 deletion test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "test-utils"
version = "0.1.0"
edition = "2021"
edition.workspace = true

[dependencies]
migration = { path = "../migration" }
Expand Down

0 comments on commit 9ef5c80

Please sign in to comment.