Skip to content

Commit 65cf688

Browse files
authored
fix(iroh): Implement Clone for StaticProvider discovery (#3108)
## Description This is not very useful without Clone, it already is Arc<RwLock<T>> on the inside so this clearly was intended. Write a test demonstrating why this is needed. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 04d43a6 commit 65cf688

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

iroh/src/discovery.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ mod tests {
448448
use anyhow::Context;
449449
use iroh_base::SecretKey;
450450
use rand::Rng;
451+
use testresult::TestResult;
451452
use tokio_util::task::AbortOnDropHandle;
452453

453454
use super::*;
@@ -734,6 +735,21 @@ mod tests {
734735
.expect("time drift")
735736
.as_micros() as u64
736737
}
738+
739+
#[tokio::test]
740+
async fn test_arc_discovery() -> TestResult {
741+
let discovery = Arc::new(EmptyDiscovery);
742+
743+
let _ep = Endpoint::builder()
744+
.add_discovery({
745+
let discovery = discovery.clone();
746+
move |_| Some(discovery)
747+
})
748+
.bind()
749+
.await?;
750+
751+
Ok(())
752+
}
737753
}
738754

739755
/// This module contains end-to-end tests for DNS node discovery.

iroh/src/discovery/static_provider.rs

Lines changed: 45 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,47 @@ impl Discovery for StaticProvider {
170170
}
171171
}
172172
}
173+
174+
#[cfg(test)]
175+
mod tests {
176+
use anyhow::Context;
177+
use iroh_base::SecretKey;
178+
use testresult::TestResult;
179+
180+
use super::*;
181+
use crate::Endpoint;
182+
183+
#[tokio::test]
184+
async fn test_basic() -> TestResult {
185+
let discovery = StaticProvider::new();
186+
187+
let _ep = Endpoint::builder()
188+
.add_discovery({
189+
let discovery = discovery.clone();
190+
move |_| Some(discovery)
191+
})
192+
.bind()
193+
.await?;
194+
195+
let key = SecretKey::from_bytes(&[0u8; 32]);
196+
let addr = NodeAddr {
197+
node_id: key.public(),
198+
relay_url: Some("https://example.com".parse()?),
199+
direct_addresses: Default::default(),
200+
};
201+
discovery.add_node_addr(addr.clone());
202+
203+
let back = discovery.get_node_addr(key.public()).context("no addr")?;
204+
205+
assert_eq!(back, addr);
206+
207+
let removed = discovery
208+
.remove_node_addr(key.public())
209+
.context("nothing removed")?;
210+
assert_eq!(removed, addr);
211+
let res = discovery.get_node_addr(key.public());
212+
assert!(res.is_none());
213+
214+
Ok(())
215+
}
216+
}

0 commit comments

Comments
 (0)