Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: improve test utils #122

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions elfo-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ futures-intrusive = "0.5"
futures = "0.3.12"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
once_cell = { version = "1.8.0" }
stability = "0.1.1"
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!(
r#"unexpected message: expected "{}", got "{}""#,
extract_name_by_type::<T>(),
msg.message().name()
),
})
}

#[track_caller]
pub fn expect_request<T: Request>(msg: Envelope) -> (T, ResponseToken<T>) {
msg!(match msg {
(msg @ T, token) => (msg, token),
msg => panic!(
r#"unexpected request: expected "{}", got "{}""#,
extract_name_by_type::<T>(),
msg.message().name()
),
})
}
5 changes: 5 additions & 0 deletions elfo-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![warn(rust_2018_idioms, unreachable_pub)]

pub use expect::{expect_message, expect_request};
pub use proxy::{proxy, Proxy};

#[cfg(feature = "unstable")]
pub use proxy::proxy_with_route;

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

pub async fn proxy(blueprint: Blueprint, config: impl for<'de> Deserializer<'de>) -> Proxy {
#[doc(hidden)]
#[stability::unstable]
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 +253,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 +280,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
Loading