Skip to content

Commit

Permalink
fix(test): adding test and code reformating
Browse files Browse the repository at this point in the history
  • Loading branch information
Christiantyemele committed Jul 2, 2024
1 parent 4161afb commit 2b6b60a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions mediator-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ uuid = { version = "1", features = ["fast-rng", "v4"] }

csv = "1.1.6"
tokio-util = {version = "0.7.7", features = ["rt"]}
once_cell = "1.19.0"



Expand Down
46 changes: 40 additions & 6 deletions mediator-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use axum::{
response::{IntoResponse, Response},
Json, Router,
};
use once_cell::sync::Lazy;
use serde_json::json;
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::Arc};
use tokio::sync::{mpsc::Sender, Mutex};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;

Expand All @@ -23,9 +25,15 @@ pub use self::{
ctx::Ctx,
error::{ClientError, Error, Result},
};
// create a global signal shutdown signal transmitter
static SHUTDOWN: Lazy<Arc<Mutex<Option<Sender<String>>>>> = Lazy::new(|| Arc::new(Mutex::new(None)));

#[tokio::main]
async fn main() -> Result<()> {
run_shutdown().await;
Ok(())
}
async fn run_shutdown() {
let mc = RecipientController::new().await;
let routes_mediate_request = coordinate_mediation::routes_mediate_request::routes(mc.clone());
let routes_all = Router::new()
Expand All @@ -39,14 +47,18 @@ async fn main() -> Result<()> {
// any process which wishes to stop the server can send a shutdown message to the shutdown transmitter
let (shutdown_tx, mut shutdown_rx) = tokio::sync::mpsc::channel::<String>(2);

// create cancellation tokens which when closed will tell processes to shutdown
let token = CancellationToken::new();
// create cancellation tokens which when closed will tell processes to shutdown
let token = CancellationToken::new();

// region: --- Start Server
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("->> Listening on {addr}\n");

// spawning task tracker on server to handle server's shutdown
// initialising global shutdown transmitter
let mut lock = SHUTDOWN.lock().await;
lock.replace(shutdown_tx);

// spawning task on server to handle server's shutdown
tokio::spawn(async move {
axum::Server::bind(&addr)
.serve(routes_all.into_make_service())
Expand All @@ -61,9 +73,8 @@ async fn main() -> Result<()> {
_shutdown_message = shutdown_rx.recv() => {eprintln!("shutting down"); token.cancel()},
_ = tokio::signal::ctrl_c() => {eprintln!("shutting down"); token.cancel()},
}

Ok(())
}

async fn main_response_mapper(uri: Uri, req_method: Method, res: Response) -> Response {
println!("->> {:<12} - main_response_mapper", "RESPONSE_MAPPER");
let uuid = Uuid::new_v4();
Expand Down Expand Up @@ -96,3 +107,26 @@ async fn main_response_mapper(uri: Uri, req_method: Method, res: Response) -> Re

error_response.unwrap_or(res)
}
#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_server_shutdown_with_shutdown_signal() {

// run server in background
tokio::spawn(run_shutdown());

// send shutdown signal
let mut lock = SHUTDOWN.lock().await;
let sender = lock.as_mut();
match sender {
Some(sender) => {
sender.send("Shutdown".to_owned()).await.unwrap();

}
None => {}
}

}
}

0 comments on commit 2b6b60a

Please sign in to comment.