From 2dc7ac10731c348b1defd36d841b2f3fea449548 Mon Sep 17 00:00:00 2001 From: 0xBradock <0xbradock@proton.me> Date: Wed, 14 Feb 2024 21:40:15 +0100 Subject: [PATCH] feat: mutualized build for test --- README.md | 1 - src/main.rs | 12 ++++++------ src/run.rs | 11 ++++++++++- tests/health.rs | 25 ++++++++++--------------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 74d4155..1cb1b05 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ OpenAPI Docs MIT -

diff --git a/src/main.rs b/src/main.rs index d0f17d9..a4fa129 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ -use std::net::TcpListener; - -use actix_real_world::{config, run::run}; +use actix_real_world::{config, run::build}; +/// main instantiates the configuration and build the server #[actix_web::main] async fn main() -> std::io::Result<()> { let conf = config::Settings::new().expect("Failed to read config file"); - let addr = format!("{}:{}", conf.server.host, conf.server.port); - let listener = TcpListener::bind(addr)?; + let server = build(&conf).await?; + + server.await?; - run(listener)?.await + Ok(()) } diff --git a/src/run.rs b/src/run.rs index 1829908..890c619 100644 --- a/src/run.rs +++ b/src/run.rs @@ -2,8 +2,17 @@ use std::net::TcpListener; use actix_web::{dev::Server, App, HttpServer}; -use crate::routes::health; +use crate::{config::Settings, routes::health}; +/// build is responsible to start all services and call the server runner +pub async fn build(conf: &Settings) -> Result { + let addr = format!("{}:{}", conf.server.host, conf.server.port); + let listener = TcpListener::bind(addr)?; + + run(listener) +} + +/// run only runs the server with the services provided as parameters pub fn run(listener: TcpListener) -> Result { let s = HttpServer::new(|| App::new().service(health)) .listen(listener)? diff --git a/tests/health.rs b/tests/health.rs index f928d5f..8d3a764 100644 --- a/tests/health.rs +++ b/tests/health.rs @@ -1,30 +1,25 @@ -// use actix_real_world::routes::health; +use actix_real_world::{config, run::build}; -use std::net::TcpListener; - -use actix_real_world::{config, run::run}; - -fn spawn_app() -> String { +async fn spawn_app() -> String { let conf = config::Settings::new().expect("Failed to read config file"); + let server = build(&conf).await; + match server { + Ok(server) => tokio::spawn(server), + Err(_) => panic!("Failed to create server"), + }; - let listener = - TcpListener::bind(format!("{}:0", conf.server.host)).expect("Failed to bind to port"); - let port = listener.local_addr().unwrap().port(); - let server = run(listener).expect("Failed to load server"); - let _ = tokio::spawn(server); - - format!("http://{}:{}", conf.server.host, port) + format!("http://{}:{}", conf.server.host, conf.server.port) } #[actix_web::test] async fn health_check_works() { // Arrange - let address = spawn_app(); + let server = spawn_app().await; let client = reqwest::Client::new(); // Act let response = client - .get(&format!("{}/health", &address)) + .get(&format!("{}/health", server)) .send() .await .expect("Failed to execute response");