xmpp::Agent::wait_for_events cannot error

This commit is contained in:
xmppftw xmppftw 2024-08-08 15:14:57 +02:00
commit 23e943825f
6 changed files with 18 additions and 22 deletions

View file

@ -85,13 +85,8 @@ impl<C: ServerConnector> Agent<C> {
muc::private_message::send_room_private_message(self, room, recipient, lang, text).await
}
/// Wait for new events.
///
/// # Returns
///
/// - `Some(events)` if there are new events; multiple may be returned at once.
/// - `None` if the underlying stream is closed.
pub async fn wait_for_events(&mut self) -> Option<Vec<Event>> {
/// Wait for new events, or Error::Disconnected when connection is closed and will not reconnect.
pub async fn wait_for_events(&mut self) -> Vec<Event> {
event_loop::wait_for_events(self).await
}

View file

@ -148,7 +148,8 @@ impl<C: ServerConnector> ClientBuilder<'_, C> {
password: self.password.into(),
server: self.server_connector.clone(),
};
let client = TokioXmppClient::new_with_config(config);
let mut client = TokioXmppClient::new_with_config(config);
client.set_reconnect(true);
self.build_impl(client)
}

View file

@ -15,13 +15,8 @@ use tokio_xmpp::{
use crate::{iq, message, presence, Agent, Event};
/// Wait for new events.
///
/// # Returns
///
/// - `Some(events)` if there are new events; multiple may be returned at once.
/// - `None` if the underlying stream is closed.
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Option<Vec<Event>> {
/// Wait for new events, or Error::Disconnected when stream is closed and will not reconnect.
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Vec<Event> {
if let Some(event) = agent.client.next().await {
let mut events = Vec::new();
@ -72,8 +67,11 @@ pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Option
}
}
Some(events)
events
} else {
None
// Stream was closed and not opening again because TokioXmppClient reconnect is false
// However we set reconnect true in agent builder so this should never happen and indicates
// logic error in tokio_xmpp::AsyncClient::poll_next
panic!("xmpp::Agent should never receive None event (stream closed, no reconnect)");
}
}

View file

@ -63,7 +63,8 @@ mod tests {
let mut agent = client_builder.build_impl(client);
while let Some(events) = agent.wait_for_events().await {
loop {
let events = agent.wait_for_events().await;
assert!(match events[0] {
Event::Disconnected(_) => true,
_ => false,