xmpp-rs: Test that client builds correctly
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
721897f49b
commit
a3922138a3
1 changed files with 46 additions and 6 deletions
|
|
@ -167,21 +167,23 @@ impl ClientBuilder<'_> {
|
||||||
self,
|
self,
|
||||||
) -> Result<(Agent, impl Stream<Item = Event, Error = tokio_xmpp::Error>), JidParseError> {
|
) -> Result<(Agent, impl Stream<Item = Event, Error = tokio_xmpp::Error>), JidParseError> {
|
||||||
let client = TokioXmppClient::new(self.jid, self.password)?;
|
let client = TokioXmppClient::new(self.jid, self.password)?;
|
||||||
Ok(self.build_impl(client))
|
let (sender_tx, sender_rx) = mpsc::unbounded();
|
||||||
|
Ok(self.build_impl(client, sender_tx, sender_rx)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is meant to be used for testing build
|
// This function is meant to be used for testing build
|
||||||
pub(crate) fn build_impl<S>(
|
pub(crate) fn build_impl<S>(
|
||||||
self,
|
self,
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> (Agent, impl Stream<Item = Event, Error = tokio_xmpp::Error>)
|
sender_tx: mpsc::UnboundedSender<Packet>,
|
||||||
|
sender_rx: mpsc::UnboundedReceiver<Packet>,
|
||||||
|
) -> Result<(Agent, impl Stream<Item = Event, Error = tokio_xmpp::Error>), JidParseError>
|
||||||
where
|
where
|
||||||
S: Stream<Item = tokio_xmpp::Event, Error = tokio_xmpp::Error>
|
S: Stream<Item = tokio_xmpp::Event, Error = tokio_xmpp::Error>
|
||||||
+ Sink<SinkItem = tokio_xmpp::Packet, SinkError = tokio_xmpp::Error>,
|
+ Sink<SinkItem = tokio_xmpp::Packet, SinkError = tokio_xmpp::Error>,
|
||||||
{
|
{
|
||||||
let disco = self.make_disco();
|
let disco = self.make_disco();
|
||||||
let node = self.website;
|
let node = self.website;
|
||||||
let (sender_tx, sender_rx) = mpsc::unbounded();
|
|
||||||
|
|
||||||
let client = stream;
|
let client = stream;
|
||||||
let (sink, stream) = client.split();
|
let (sink, stream) = client.split();
|
||||||
|
|
@ -322,11 +324,11 @@ impl ClientBuilder<'_> {
|
||||||
default_nick: Rc::new(RefCell::new(self.default_nick)),
|
default_nick: Rc::new(RefCell::new(self.default_nick)),
|
||||||
};
|
};
|
||||||
|
|
||||||
(agent, future)
|
Ok((agent, future))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
sender_tx: mpsc::UnboundedSender<Packet>,
|
sender_tx: mpsc::UnboundedSender<Packet>,
|
||||||
default_nick: Rc<RefCell<String>>,
|
default_nick: Rc<RefCell<String>>,
|
||||||
|
|
@ -360,3 +362,41 @@ impl Agent {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use futures::prelude::*;
|
||||||
|
use tokio_xmpp::Client as TokioXmppClient;
|
||||||
|
use tokio::runtime::current_thread::Runtime;
|
||||||
|
use super::{Agent, ClientBuilder, ClientType, ClientFeature, Event};
|
||||||
|
use futures::sync::mpsc;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_simple() {
|
||||||
|
// tokio_core context
|
||||||
|
let mut rt = Runtime::new().unwrap();
|
||||||
|
let client = TokioXmppClient::new("foo@bar", "meh").unwrap();
|
||||||
|
let (sender_tx, sender_rx) = mpsc::unbounded();
|
||||||
|
|
||||||
|
// Client instance
|
||||||
|
let client_builder = ClientBuilder::new("foo@bar", "meh")
|
||||||
|
.set_client(ClientType::Bot, "xmpp-rs")
|
||||||
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
||||||
|
.set_default_nick("bot")
|
||||||
|
.enable_feature(ClientFeature::Avatars)
|
||||||
|
.enable_feature(ClientFeature::ContactList);
|
||||||
|
|
||||||
|
let (_agent, stream): (Agent, _) =
|
||||||
|
client_builder
|
||||||
|
.build_impl(client, sender_tx.clone(), sender_rx)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let handler = stream.map_err(Some).for_each(|_evt: Event| {
|
||||||
|
return Err(None);
|
||||||
|
});
|
||||||
|
|
||||||
|
rt.block_on(handler).unwrap_or_else(|e| match e {
|
||||||
|
_ => (),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue