-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ routing_operations
- Loading branch information
Showing
4 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use axum::http::StatusCode; | ||
|
||
use crate::{ | ||
chat::{Chat, Message}, | ||
AppState, | ||
}; | ||
|
||
pub async fn receive_message( | ||
sender: impl ToString, | ||
data: impl ToString, | ||
room_id: impl ToString, | ||
state: &AppState, | ||
) { | ||
let message = Message::new(sender.to_string(), data.to_string()); | ||
match state.is_chat_exists(&room_id.to_string()).await { | ||
Some(index) => { | ||
let mut chats = state.chats.lock().await; | ||
chats[index].add_message(message, state.max_message_counter); | ||
} | ||
None => { | ||
let mut new_chat = Chat::new(room_id.to_string()); | ||
new_chat.add_message(message, state.max_message_counter); | ||
let mut chats = state.chats.lock().await; | ||
let room_id = new_chat.room_id.clone(); | ||
chats.push(new_chat); | ||
drop(chats); | ||
tokio::spawn(AppState::chat_destroyer( | ||
state.chats.clone(), | ||
room_id, | ||
state.chat_cleaning_timeout, | ||
)); | ||
} | ||
} | ||
} | ||
|
||
pub async fn send_message( | ||
room_id: impl ToString, | ||
state: AppState, | ||
) -> (axum::http::StatusCode, std::string::String) { | ||
match state.is_chat_exists(&room_id.to_string()).await { | ||
Some(index) => { | ||
let chats = state.chats.lock().await; | ||
( | ||
StatusCode::OK, | ||
serde_json::to_string(&chats[index]).unwrap(), | ||
) | ||
} | ||
None => (StatusCode::BAD_REQUEST, serde_json::to_string("").unwrap()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,96 @@ | ||
#[cfg(test)] | ||
use crate::{routing_operations::receive_message, AppState}; | ||
#[cfg(test)] | ||
use std::sync::Arc; | ||
#[cfg(test)] | ||
use std::time::Duration; | ||
#[cfg(test)] | ||
use tokio::sync::Mutex; | ||
|
||
#[tokio::test] | ||
async fn new_message_no_chat() { | ||
let state = AppState { | ||
chats: Arc::new(Mutex::new(vec![])), | ||
max_message_counter: 5, | ||
chat_cleaning_timeout: 10, | ||
}; | ||
receive_message("Tahinli", "Hi", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
} | ||
|
||
#[tokio::test] | ||
async fn new_message_with_chat() { | ||
let state = AppState { | ||
chats: Arc::new(Mutex::new(vec![])), | ||
max_message_counter: 5, | ||
chat_cleaning_timeout: 10, | ||
}; | ||
receive_message("Tahinli", "Hi", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
drop(chats); | ||
|
||
receive_message("Tahinli", "Hi Again", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 2); | ||
} | ||
|
||
#[tokio::test] | ||
async fn chat_auto_deletion_with_timeout() { | ||
let state = AppState { | ||
chats: Arc::new(Mutex::new(vec![])), | ||
max_message_counter: 5, | ||
chat_cleaning_timeout: 1, | ||
}; | ||
receive_message("Tahinli", "Hi", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
drop(chats); | ||
|
||
tokio::time::sleep(Duration::from_secs(2)).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 0); | ||
} | ||
|
||
#[tokio::test] | ||
async fn message_auto_deletion_with_counter() { | ||
let state = AppState { | ||
chats: Arc::new(Mutex::new(vec![])), | ||
max_message_counter: 1, | ||
chat_cleaning_timeout: 10, | ||
}; | ||
receive_message("Tahinli", "Hi", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
drop(chats); | ||
|
||
receive_message("Tahinli", "Hi", "Tahinli-Chat", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
} | ||
|
||
#[tokio::test] | ||
async fn create_multiple_chats() { | ||
let state = AppState { | ||
chats: Arc::new(Mutex::new(vec![])), | ||
max_message_counter: 1, | ||
chat_cleaning_timeout: 10, | ||
}; | ||
receive_message("Tahinli", "Hi", "Tahinli-Chat-1", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 1); | ||
assert_eq!(chats[0].messages.len(), 1); | ||
drop(chats); | ||
|
||
receive_message("Tahinli", "Hi", "Tahinli-Chat-2", &state).await; | ||
let chats = state.chats.lock().await; | ||
assert_eq!(chats.len(), 2); | ||
assert_eq!(chats[1].messages.len(), 1); | ||
} |