#![cfg(feature = "mdns")]
use std::time::Duration;
use openadr::discovery::{Advertisement, VtnService, discover};
fn unique_instance() -> String {
format!(
"openadr-test-{}-{}",
std::process::id(),
openadr::model::Timestamp::now().as_nanosecond() % 1_000_000
)
}
#[tokio::test(flavor = "multi_thread")]
async fn a_ven_finds_a_vtn_that_advertises_itself() {
let instance = unique_instance();
let service = VtnService::new(&instance, 3000)
.with_base_path("/openadr3/3.1.0")
.with_program_names(["local-tariff", "curtailment"])
.requiring_auth(false)
.with_openapi_url("http://x.local:3000/openapi.json");
let Ok(advertisement) = Advertisement::start(&service) else {
eprintln!("skipping mDNS round trip: no usable network interface");
return;
};
assert_eq!(
advertisement.full_name(),
format!("{instance}._openadr3._tcp.local.")
);
let found = tokio::task::spawn_blocking(|| discover(Duration::from_secs(5)))
.await
.expect("the browse task panicked")
.expect("the browser could not start");
let Some(discovered) = found.iter().find(|s| s.instance == instance) else {
eprintln!(
"skipping mDNS round trip: the responder started but nothing was received \
(found {} other service(s)); multicast is not usable here",
found.len()
);
return;
};
assert_eq!(discovered.port, 3000);
assert_eq!(discovered.version, openadr::SPEC_VERSION);
assert_eq!(discovered.base_path, "openadr3/3.1.0");
assert!(!discovered.requires_auth);
assert_eq!(
discovered.program_names,
vec!["local-tariff".to_string(), "curtailment".to_string()]
);
assert_eq!(
discovered.openapi_url.as_deref(),
Some("http://x.local:3000/openapi.json")
);
assert!(
discovered.local_url().ends_with(":3000/openadr3/3.1.0"),
"a VEN would dial {}",
discovered.local_url()
);
}