Skip to content

Commit 07de2c8

Browse files
committed
fix(iroh): Implement Clone for StaticProvider discovery
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.
1 parent 4d28dfa commit 07de2c8

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

iroh/src/discovery/static_provider.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use iroh_base::{NodeAddr, NodeId, RelayUrl};
1212
use super::{Discovery, DiscoveryItem};
1313

1414
/// A static discovery implementation that allows providing info for nodes manually.
15-
#[derive(Debug, Default)]
15+
#[derive(Debug, Default, Clone)]
1616
#[repr(transparent)]
1717
pub struct StaticProvider {
1818
nodes: Arc<RwLock<BTreeMap<NodeId, NodeInfo>>>,
@@ -170,3 +170,34 @@ impl Discovery for StaticProvider {
170170
}
171171
}
172172
}
173+
174+
#[cfg(test)]
175+
mod tests {
176+
use iroh_base::SecretKey;
177+
use testresult::TestResult;
178+
179+
use super::*;
180+
use crate::Endpoint;
181+
182+
#[tokio::test]
183+
async fn test_basic() -> TestResult {
184+
let discovery = StaticProvider::new();
185+
186+
let _ep = Endpoint::builder()
187+
.add_discovery({
188+
let discovery = discovery.clone();
189+
move |_| Some(discovery)
190+
})
191+
.bind()
192+
.await?;
193+
194+
let key = SecretKey::from_bytes(&[0u8; 32]);
195+
discovery.add_node_addr(NodeAddr {
196+
node_id: key.public(),
197+
relay_url: Some("https://example.com".parse()?),
198+
direct_addresses: Default::default(),
199+
});
200+
201+
Ok(())
202+
}
203+
}

0 commit comments

Comments
 (0)