use super::*;
use crate::registry::presence::Presence;
use std::time::Instant;
struct Watcher(Presence);
impl Answerer for Watcher {
fn answer(&self, _client: &Client, _request: Value) -> Box<dyn Iterator<Item = Value>> {
Box::new(std::iter::once(
json!({"live": self.0.live().into_iter().collect::<Vec<String>>()}),
))
}
}
#[test]
fn a_live_connection_is_present_and_a_closed_one_is_not() {
let tmp = TempDir::new().expect("tmp");
mint(tmp.path());
let presence = Presence::default();
let listener = Listener::bind(
&material(
tmp.path(),
Role::Server,
crate::test_support::wire::EPHEMERAL,
),
Arc::new(Watcher(presence.clone())),
presence.clone(),
)
.expect("bind");
let seat = Seat::open(&material(tmp.path(), Role::Client, &listener.address())).expect("seat");
let stream = seat.ask(&json!({"op": "workspaces"})).expect("answered");
assert_eq!(stream[0]["live"], json!(["yog-client"]));
let deadline = Instant::now() + Duration::from_secs(5);
while !presence.live().is_empty() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(10));
}
assert!(presence.live().is_empty(), "the connection's guard dropped");
}