Skip to content

Commit

Permalink
Merge pull request #121 from RicoGit/elfo-test-improvements
Browse files Browse the repository at this point in the history
feat(test): `extract_message` and `extract_request`
  • Loading branch information
loyd committed Feb 26, 2024
2 parents 7f8d746 + c841d02 commit 9eb8c16
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
6 changes: 4 additions & 2 deletions elfo-core/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use tracing::{error, info, level_filters::LevelFilter, warn};
use elfo_utils::time::Instant;

#[cfg(target_os = "linux")]
use crate::{memory_tracker::MemoryTracker, time::Interval};
use crate::{
memory_tracker::{MemoryCheckResult, MemoryTracker},
time::Interval,
};

use crate::{
actor::{Actor, ActorMeta, ActorStartInfo, ActorStatus},
Expand All @@ -19,7 +22,6 @@ use crate::{
context::Context,
demux::Demux,
errors::{RequestError, StartError, StartGroupError},
memory_tracker::MemoryCheckResult,
message,
messages::{StartEntrypoint, Terminate, UpdateConfig},
object::Object,
Expand Down
2 changes: 2 additions & 0 deletions elfo-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(rust_2018_idioms, unreachable_pub)]

pub use proxy::{proxy, Proxy};
pub use utils::{extract_message, extract_request};

mod proxy;
mod utils;
84 changes: 84 additions & 0 deletions elfo-test/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use elfo_core::{msg, Envelope, Message, Request, ResponseToken};

/// Extracts message with type [`M`] from [`Envelope`], panics otherwise.
#[track_caller]
pub fn extract_message<M: Message>(envelope: Envelope) -> M {
msg!(match envelope {
msg @ M => msg,
msg => panic!(
r#"expected {}, got {:#?}"#,
elfo_core::dumping::extract_name_by_type::<M>(),
msg.message()
),
})
}

/// Extracts request message with type [`R`] from [`Envelope`], panics
/// otherwise.
#[track_caller]
pub fn extract_request<R: Request>(envelope: Envelope) -> (R, ResponseToken<R>) {
msg!(match envelope {
(req @ R, token) => (req, token),
msg => panic!(
r#"expected {}, got {:#?}"#,
elfo_core::dumping::extract_name_by_type::<R>(),
msg.message()
),
})
}

#[cfg(test)]
mod tests {
use elfo_core::{_priv::MessageKind, message, scope::Scope, ActorMeta, Addr};

use super::*;

#[message]
#[derive(PartialEq)]
struct TestMessage;

#[message(ret = ())]
#[derive(PartialEq)]
struct TestRequest;

#[test]
fn extract_message_test() {
create_scope().sync_within(|| {
let envelop =
Envelope::new(TestMessage, MessageKind::Regular { sender: Addr::NULL }).upcast();
let resp = extract_message::<TestMessage>(envelop);
assert_eq!(resp, TestMessage);
});
}

#[test]
#[should_panic(expected = "expected TestMessage, got TestRequest")]
fn extract_message_panic_test() {
create_scope().sync_within(|| {
let envelop =
Envelope::new(TestRequest, MessageKind::Regular { sender: Addr::NULL }).upcast();
extract_message::<TestMessage>(envelop);
});
}

#[test]
fn extract_request_test() {
create_scope().sync_within(|| {
let envelop =
Envelope::new(TestRequest, MessageKind::Regular { sender: Addr::NULL }).upcast();
let (resp, _token) = extract_request::<TestRequest>(envelop);
assert_eq!(resp, TestRequest);
});
}

fn create_scope() -> Scope {
Scope::test(
Addr::NULL,
ActorMeta {
group: "group".into(),
key: "key".into(),
}
.into(),
)
}
}

0 comments on commit 9eb8c16

Please sign in to comment.