Skip to content

Commit

Permalink
feat: mutualized build for test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBradock committed Feb 14, 2024
1 parent 510cb65 commit 2dc7ac1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</a>
<a href="https://github.com/0xBradock/actix-real-world/blob/master/openapi.yaml"><img src="https://img.shields.io/badge/Docs-OpenAPI%203.0-success" alt="OpenAPI Docs"></a>
<a href="https://github.com/qdrant/qdrant/blob/master/LICENSE"><img src="https://img.shields.io/github/license/0xBradock/actix-real-world" alt="MIT"></a>

</p>


Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
11 changes: 10 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server, std::io::Error> {
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<Server, std::io::Error> {
let s = HttpServer::new(|| App::new().service(health))
.listen(listener)?
Expand Down
25 changes: 10 additions & 15 deletions tests/health.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down

0 comments on commit 2dc7ac1

Please sign in to comment.