-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(swarm): report outcome of handling SwarmEvent
#3865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
478dc6b
11dcdef
dc0493e
2c17008
0aa6d9d
7b5f281
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,21 +32,67 @@ impl ExternalAddresses { | |
} | ||
|
||
/// Feed a [`FromSwarm`] event to this struct. | ||
/// | ||
/// Returns whether the event changed our set of external addresses. | ||
#[allow(deprecated)] | ||
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) | ||
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) -> bool | ||
where | ||
THandler: IntoConnectionHandler, | ||
{ | ||
match event { | ||
FromSwarm::NewExternalAddr(NewExternalAddr { addr, .. }) => { | ||
if self.addresses.len() < self.limit { | ||
self.addresses.insert((*addr).clone()); | ||
return self.addresses.insert((*addr).clone()); | ||
} | ||
} | ||
FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr, .. }) => { | ||
self.addresses.remove(addr); | ||
return self.addresses.remove(addr) | ||
} | ||
_ => {} | ||
} | ||
|
||
false | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::dummy; | ||
use libp2p_core::multiaddr::Protocol; | ||
use once_cell::sync::Lazy; | ||
|
||
#[test] | ||
fn new_external_addr_returns_correct_changed_value() { | ||
let mut addresses = ExternalAddresses::default(); | ||
|
||
let changed = addresses.on_swarm_event(&new_external_addr()); | ||
assert!(changed); | ||
|
||
let changed = addresses.on_swarm_event(&new_external_addr()); | ||
assert!(!changed) | ||
} | ||
|
||
#[test] | ||
fn expired_external_addr_returns_correct_changed_value() { | ||
let mut addresses = ExternalAddresses::default(); | ||
addresses.on_swarm_event(&new_external_addr()); | ||
|
||
let changed = addresses.on_swarm_event(&expired_external_addr()); | ||
assert!(changed); | ||
|
||
let changed = addresses.on_swarm_event(&expired_external_addr()); | ||
assert!(!changed) | ||
} | ||
|
||
fn new_external_addr() -> FromSwarm<'static, dummy::ConnectionHandler> { | ||
FromSwarm::NewExternalAddr(NewExternalAddr { addr: &MEMORY_ADDR }) | ||
} | ||
|
||
fn expired_external_addr() -> FromSwarm<'static, dummy::ConnectionHandler> { | ||
FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr: &MEMORY_ADDR }) | ||
} | ||
|
||
static MEMORY_ADDR: Lazy<Multiaddr> = | ||
Lazy::new(|| Multiaddr::empty().with(Protocol::Memory(1000))); | ||
Comment on lines
+96
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the value of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. I missed that. I am hesitant to introduce a dependency just for the sake of more succinct test code. That said, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is on the pathway to being stabilized and part of std: rust-lang/rust#105587 |
||
} |
Uh oh!
There was an error while loading. Please reload this page.