Skip to content

Commit

Permalink
fix(iroh): Implement Clone for StaticProvider discovery
Browse files Browse the repository at this point in the history
This is not very useful without Clone, it already is Arc<RwLock<T>> on
the inside to this clearly was intended.  Write a test demonstrating
why this is needed.
  • Loading branch information
flub committed Jan 8, 2025
1 parent 4d28dfa commit 07de2c8
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion iroh/src/discovery/static_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iroh_base::{NodeAddr, NodeId, RelayUrl};
use super::{Discovery, DiscoveryItem};

/// A static discovery implementation that allows providing info for nodes manually.
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
#[repr(transparent)]
pub struct StaticProvider {
nodes: Arc<RwLock<BTreeMap<NodeId, NodeInfo>>>,
Expand Down Expand Up @@ -170,3 +170,34 @@ impl Discovery for StaticProvider {
}
}
}

#[cfg(test)]
mod tests {
use iroh_base::SecretKey;
use testresult::TestResult;

use super::*;
use crate::Endpoint;

#[tokio::test]
async fn test_basic() -> TestResult {
let discovery = StaticProvider::new();

let _ep = Endpoint::builder()
.add_discovery({
let discovery = discovery.clone();
move |_| Some(discovery)
})
.bind()
.await?;

let key = SecretKey::from_bytes(&[0u8; 32]);
discovery.add_node_addr(NodeAddr {
node_id: key.public(),
relay_url: Some("https://example.com".parse()?),
direct_addresses: Default::default(),
});

Ok(())
}
}

0 comments on commit 07de2c8

Please sign in to comment.