Skip to content

Commit

Permalink
feat(elfo-test): improve test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sargarass committed Feb 23, 2024
1 parent 7f8d746 commit 4a65735
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
25 changes: 25 additions & 0 deletions elfo-test/src/expect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use elfo_core::{dumping::extract_name_by_type, msg, Envelope, Message, Request, ResponseToken};

#[track_caller]
pub fn expect_message<T: Message>(msg: Envelope) -> T {
msg!(match msg {
msg @ T => msg,
msg => panic!(
"unexpected message: expected {}, got: {:?}",
extract_name_by_type::<T>(),
msg
),
})
}

#[track_caller]
pub fn expect_request<T: Request>(msg: Envelope) -> (T, ResponseToken<T>) {
msg!(match msg {
(msg @ T, token) => (msg, token),
msg => panic!(
"unexpected request: expected {}, got: {:?}",
extract_name_by_type::<T>(),
msg
),
})
}
4 changes: 3 additions & 1 deletion 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 expect::{expect_message, expect_request};
pub use proxy::{proxy, proxy_with_route, Proxy};

mod expect;
mod proxy;
15 changes: 13 additions & 2 deletions elfo-test/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,14 @@ fn testers(tx: shared::OneshotSender<ProxyContext>) -> Blueprint {
})
}

pub async fn proxy(blueprint: Blueprint, config: impl for<'de> Deserializer<'de>) -> Proxy {
pub async fn proxy_with_route<F>(
blueprint: Blueprint,
route_filter: F,
config: impl for<'de> Deserializer<'de>,
) -> Proxy
where
F: Fn(&Envelope) -> bool + Send + Sync + 'static,
{
let _ = tracing_subscriber::fmt()
.with_target(false)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
Expand All @@ -244,7 +251,7 @@ pub async fn proxy(blueprint: Blueprint, config: impl for<'de> Deserializer<'de>
let subject_addr = subject.addr();

testers.route_all_to(&subject);
subject.route_all_to(&testers);
subject.route_to(&testers, route_filter);

// TODO: capture log messages.
// TODO: capture metrics.
Expand All @@ -271,6 +278,10 @@ pub async fn proxy(blueprint: Blueprint, config: impl for<'de> Deserializer<'de>
}
}

pub async fn proxy(blueprint: Blueprint, config: impl for<'de> Deserializer<'de>) -> Proxy {
proxy_with_route(blueprint, |_| true, config).await
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 4a65735

Please sign in to comment.