Skip to content

Commit

Permalink
Few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Apr 5, 2024
1 parent a6aae74 commit 472d114
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/actors/utilities/test_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,51 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use tokio::{
sync::Mutex,
time::{sleep, Duration},
};

#[tokio::test]
async fn test_actor_receives_multiple_messages() {
let messages_received = Arc::new(Mutex::new(Vec::new()));
let (actor_ref, handle) =
TestActor::<String>::spawn(None, TestActor::default(), Some(messages_received.clone()))
.await
.expect("Failed to spawn TestActor");

let messages_to_send = vec!["Hello, Actor!", "Second Message", "Third Message"];
for msg in messages_to_send.iter() {
actor_ref
.send_message(msg.to_string())
.expect("Failed to send message");
}

tokio::spawn(async move {
sleep(Duration::from_millis(100)).await;
actor_ref.stop(None);
});

handle
.await
.expect("Actor task has been completed with an error");

let received_messages = messages_received.lock().await;
assert_eq!(
received_messages.len(),
messages_to_send.len(),
"Should have received all messages sent"
);
for (sent, received) in messages_to_send.iter().zip(received_messages.iter()) {
assert_eq!(
sent, received,
"Received message does not match the sent message"
);
}
}
}
6 changes: 6 additions & 0 deletions src/domain_objects/moderation_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ mod tests {
let violence = ModerationCategory::Violence;
assert_eq!(violence.nip69(), "VI");
}

#[test]
fn test_display() {
let sexual = ModerationCategory::Sexual;
assert_eq!(format!("{}", sexual), "sexual");
}
}
45 changes: 45 additions & 0 deletions src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,48 @@ fn simplify_type_name(input: &str) -> String {

result.replace(':', "")
}

#[cfg(test)]
mod service_manager_tests {
use super::*;
use crate::actors::utilities::test_actor::{TestActor, TestActorMessagesReceived};
use std::sync::Arc;
use tokio::{
sync::Mutex,
time::{sleep, Duration},
};

#[tokio::test]
async fn service_manager_spawns_and_manages_test_actor() {
let mut service_manager = ServiceManager::new();

let messages_received: TestActorMessagesReceived<String> = Arc::new(Mutex::new(Vec::new()));

let actor_args = Some(messages_received.clone());
let test_actor_ref = service_manager
.spawn_actor(TestActor::<String>::default(), actor_args)
.await
.expect("Failed to spawn TestActor");

let test_message = "Hello from ServiceManager!".to_string();
test_actor_ref
.send_message(test_message.clone())
.expect("Failed to send message");

sleep(Duration::from_millis(50)).await;

let locked_messages = messages_received.lock().await;

assert_eq!(
locked_messages.len(),
1,
"TestActor should have received exactly one message"
);
assert_eq!(
locked_messages[0], test_message,
"The received message should match the sent message"
);

service_manager.stop().await;
}
}

0 comments on commit 472d114

Please sign in to comment.