use std::time::Duration;
mod util;
use util::{admin_config, endpoint};
const TOKEN: &str = "v1/h-cccccccccccc/state/demo/alive";
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_declared_token_attaches_its_origin() {
let file = admin_config();
let endpoint = endpoint();
let serving = zenkey_fleet::open_with_config(
Some(&file),
&[],
std::slice::from_ref(&endpoint),
Some(false),
)
.await
.expect("serving session");
let asking = zenkey_fleet::bus::session::open(std::slice::from_ref(&endpoint), &[], false)
.await
.expect("asking session");
let _token = serving
.liveliness()
.declare_token(TOKEN)
.await
.expect("declare token");
let _other = serving
.liveliness()
.declare_token("v1/h-cccccccccccc/state/demo/lease")
.await
.expect("declare non-alive token");
let deadline = std::time::Instant::now() + Duration::from_secs(10);
let attachments = loop {
let a = zenkey_fleet::origin_attachments(
&zenkey_fleet::Fleet::new(&asking, ""),
Duration::from_millis(500),
)
.await
.expect("a quiet admin space is a reading, not an error");
if !a.is_empty() {
break a;
}
assert!(
std::time::Instant::now() < deadline,
"the admin space never served the token"
);
};
assert_eq!(
attachments.len(),
1,
"only the alive leaf attaches: {attachments:?}"
);
let a = &attachments[0];
assert_eq!(a.origin, "h-cccccccccccc");
assert_eq!(a.token_key, TOKEN);
let serving_zid = serving.zid().to_string();
assert_eq!(
a.reporter_zid, serving_zid,
"the peer's own admin space is the reporter"
);
if let Some(z) = &a.session_zid {
assert_eq!(z, &serving_zid, "an attachment is evidence, never a guess");
}
std::fs::remove_file(file).ok();
}