use std::future::IntoFuture;
use crate::client::{Client, Error};
use crate::future::BoxedFuture;
#[must_use = "Does nothing unless you await!"]
pub struct RemoveAllRelays<'client> {
client: &'client Client,
force: bool,
}
impl<'client> RemoveAllRelays<'client> {
pub(crate) fn new(client: &'client Client) -> Self {
Self {
client,
force: false,
}
}
#[inline]
pub fn force(mut self) -> Self {
self.force = true;
self
}
}
impl<'client> IntoFuture for RemoveAllRelays<'client> {
type Output = Result<(), Error>;
type IntoFuture = BoxedFuture<'client, Self::Output>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
self.client.pool().remove_all_relays(self.force).await;
Ok(())
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::relay::RelayCapabilities;
#[tokio::test]
async fn test_remove_all_relays() {
let client = Client::default();
client.add_relay("ws://127.0.0.1:6666").await.unwrap();
client.add_relay("ws://127.0.0.1:7777").await.unwrap();
client
.add_relay("ws://127.0.0.1:8888")
.capabilities(RelayCapabilities::default() | RelayCapabilities::GOSSIP)
.await
.unwrap();
assert_eq!(client.relays().await.len(), 3);
assert_eq!(client.pool().all_relays().await.len(), 3);
client.remove_all_relays().await.unwrap();
assert!(client.relay("ws://127.0.0.1:6666").await.unwrap().is_none());
assert!(client.relay("ws://127.0.0.1:7777").await.unwrap().is_none());
assert!(client.relay("ws://127.0.0.1:8888").await.is_ok()); assert!(client.relays().await.is_empty()); assert_eq!(client.pool().all_relays().await.len(), 1); }
#[tokio::test]
async fn test_force_remove_all_relays() {
let client = Client::default();
client.add_relay("ws://127.0.0.1:6666").await.unwrap();
client.add_relay("ws://127.0.0.1:7777").await.unwrap();
client
.add_relay("ws://127.0.0.1:8888")
.capabilities(RelayCapabilities::default() | RelayCapabilities::GOSSIP)
.await
.unwrap();
assert_eq!(client.relays().await.len(), 3);
assert_eq!(client.pool().all_relays().await.len(), 3);
client.remove_all_relays().force().await.unwrap();
assert!(client.relays().await.is_empty());
assert!(client.pool().all_relays().await.is_empty());
assert!(client.relay("ws://127.0.0.1:6666").await.unwrap().is_none());
assert!(client.relay("ws://127.0.0.1:7777").await.unwrap().is_none());
assert!(client.relay("ws://127.0.0.1:8888").await.unwrap().is_none());
}
}