#![cfg(all(feature = "proxy", feature = "service"))]
use std::{io::ErrorKind, time::Duration};
use ntest::timeout;
use zbus::{Connection, connection::Builder, proxy::CacheProperties};
use crate::runtime::Runtime;
#[test]
#[timeout(15000)]
fn a_timed_out_call_on_the_runtime_is_cancelled() {
let runtime = Runtime::new().unwrap();
let handle = runtime.handle();
let probe = runtime.probe();
runtime.run(async {
let conn = Builder::session()
.runtime(handle)
.method_timeout(Duration::from_millis(200))
.serve_at(PATH, Answers)
.build()
.await
.unwrap();
let proxy = proxy(&conn).await;
match proxy.never_replies().await {
Err(zbus::Error::InputOutput(e)) => assert_eq!(e.kind(), ErrorKind::TimedOut),
other => panic!("a call nobody answers should time out, got {other:?}"),
}
});
assert_eq!(
runtime.pending_timers(),
0,
"the timed-out call left a timer"
);
drop(runtime);
assert!(
probe.is_released(),
"the runtime's state outlived it: something it handed out still holds it",
);
}
#[test]
#[timeout(15000)]
fn a_call_that_returns_leaves_no_timer_on_the_runtime() {
let runtime = Runtime::new().unwrap();
let handle = runtime.handle();
let probe = runtime.probe();
runtime.run(async {
let conn = Builder::session()
.runtime(handle)
.method_timeout(Duration::from_secs(5))
.serve_at(PATH, Answers)
.build()
.await
.unwrap();
let proxy = proxy(&conn).await;
assert_eq!(proxy.greet("world").await.unwrap(), "Hello world!");
assert_eq!(
runtime.pending_timers(),
0,
"the call that returned left its timer behind",
);
});
drop(runtime);
assert!(
probe.is_released(),
"the runtime's state outlived it: something it handed out still holds it",
);
}
const PATH: &str = "/org/zbus/PollingRuntime";
async fn proxy(conn: &Connection) -> AnswersProxy<'_> {
let name = conn
.unique_name()
.expect("a bus connection has a unique name")
.to_string();
AnswersProxy::builder(conn)
.destination(name)
.cache_properties(CacheProperties::No)
.build()
.await
.unwrap()
}
struct Answers;
#[zbus::interface(
interface = "org.zbus.PollingRuntime.Answers",
proxy(default_path = "/org/zbus/PollingRuntime")
)]
impl Answers {
async fn greet(&self, name: &str) -> String {
format!("Hello {name}!")
}
async fn never_replies(&self) {
std::future::pending::<()>().await
}
}