Skip to content

Commit

Permalink
✅ Add a test case for ResponseNotify
Browse files Browse the repository at this point in the history
  • Loading branch information
zeenix committed Jul 9, 2023
1 parent c7643ce commit a0d65e4
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions zbus/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::HashMap, convert::TryInto};
use tokio::net::UnixStream;

use event_listener::Event;
use futures_util::StreamExt;
use futures_util::{StreamExt, TryStreamExt};
use ntest::timeout;
use serde::{Deserialize, Serialize};
use test_log::test;
Expand All @@ -15,7 +15,7 @@ use tracing::{debug, instrument};
use zbus::{
block_on,
fdo::{ObjectManager, ObjectManagerProxy},
DBusError,
DBusError, MessageBuilder, MessageStream, NotifyResponse,
};
use zvariant::{DeserializeDict, OwnedValue, SerializeDict, Str, Type, Value};

Expand Down Expand Up @@ -62,6 +62,8 @@ trait MyIface {

fn test_multi_ret(&self) -> zbus::Result<(i32, String)>;

fn test_response_notify(&self) -> zbus::Result<String>;

fn test_hashmap_return(&self) -> zbus::Result<HashMap<String, String>>;

fn create_obj(&self, key: &str) -> zbus::Result<()>;
Expand Down Expand Up @@ -210,6 +212,32 @@ impl MyIfaceImpl {
Ok((42, String::from("Meaning of life")))
}

#[instrument]
fn test_response_notify(
&self,
#[zbus(connection)] conn: &Connection,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
) -> zbus::fdo::Result<NotifyResponse<String>> {
debug!("`TestResponseNotify` called.");
let (response, listener) = NotifyResponse::new(String::from("Meaning of life"));
let ctxt = ctxt.to_owned();
conn.executor()
.spawn(
async move {
listener.await;

Self::test_response_notified(ctxt).await.unwrap();
},
"TestResponseNotify",
)
.detach();

Ok(response)
}

#[dbus_interface(signal)]
async fn test_response_notified(ctxt: SignalContext<'_>) -> zbus::Result<()>;

#[instrument]
async fn test_hashmap_return(&self) -> zbus::fdo::Result<HashMap<String, String>> {
debug!("`TestHashmapReturn` called.");
Expand Down Expand Up @@ -400,6 +428,33 @@ fn check_ipv4_address(address: IP4Adress) {
#[instrument]
async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result<u32> {
debug!("client side starting..");
// Use low-level API for `TestResponseNotify` because we need to ensure that the signal is
// always received after the response.
let mut stream = MessageStream::from(&conn);
let method = MessageBuilder::method_call("/org/freedesktop/MyService", "TestResponseNotify")?
.interface("org.freedesktop.MyIface")?
.destination("org.freedesktop.MyService")?
.build(&())?;
let serial = conn.send_message(method).await?;
let mut method_returned = false;
let mut signal_received = false;
while !method_returned && !signal_received {
let msg = stream.try_next().await?.unwrap();

let hdr = msg.header()?;
if hdr.message_type()? == MessageType::MethodReturn && hdr.reply_serial()? == Some(serial) {
assert!(!signal_received);
method_returned = true;
} else if hdr.message_type()? == MessageType::Signal
&& hdr.interface()?.unwrap() == "org.freedesktop.MyService"
&& hdr.member()?.unwrap() == "TestResponseNotified"
{
assert!(method_returned);
signal_received = true;
}
}
drop(stream);

let proxy = MyIfaceProxy::builder(&conn)
.destination("org.freedesktop.MyService")?
.path("/org/freedesktop/MyService")?
Expand Down

0 comments on commit a0d65e4

Please sign in to comment.