Skip to content

Commit

Permalink
feat: emit new event and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PanGan21 committed Aug 11, 2024
1 parent 2357b04 commit e7003f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
8 changes: 7 additions & 1 deletion protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,13 +2562,19 @@ where
// Drain applied pending entries from the routing table.
if let Some(entry) = self.kbuckets.take_applied_pending() {
let kbucket::Node { key, value } = entry.inserted;
let peer_id = key.into_preimage();
self.queued_events
.push_back(ToSwarm::NewExternalAddrOfPeer {
peer_id,
address: value.first().clone(),
});
let event = Event::RoutingUpdated {
bucket_range: self
.kbuckets
.bucket(&key)
.map(|b| b.range())
.expect("Self to never be applied from pending."),
peer: key.into_preimage(),
peer: peer_id,
is_new_peer: true,
addresses: value,
old_peer: entry.evicted.map(|n| n.key.into_preimage()),
Expand Down
30 changes: 26 additions & 4 deletions protocols/kad/tests/client_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ async fn server_gets_added_to_routing_table_by_client() {
let server_peer_id = *server.local_peer_id();
async_std::task::spawn(server.loop_on_next());

let peer = client
let external_event_peer = client
.wait(|e| match e {
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
_ => None,
})
.await;
let routing_updated_peer = client
.wait(|e| match e {
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
_ => None,
})
.await;

assert_eq!(peer, server_peer_id);
assert_eq!(external_event_peer, server_peer_id);
assert_eq!(routing_updated_peer, server_peer_id);
}

#[async_std::test]
Expand Down Expand Up @@ -64,14 +71,22 @@ async fn two_servers_add_each_other_to_routing_table() {

async_std::task::spawn(server1.loop_on_next());

let peer = server2
let external_event_peer = server2
.wait(|e| match e {
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
_ => None,
})
.await;

let routing_updated_peer = server2
.wait(|e| match e {
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
_ => None,
})
.await;

assert_eq!(peer, server1_peer_id);
assert_eq!(external_event_peer, server1_peer_id);
assert_eq!(routing_updated_peer, server1_peer_id);
}

#[async_std::test]
Expand Down Expand Up @@ -126,6 +141,12 @@ async fn set_client_to_server_mode() {

let server_peer_id = *server.local_peer_id();

let peer_id = client
.wait(|e| match e {
SwarmEvent::NewExternalAddrOfPeer { peer_id, .. } => Some(peer_id),
_ => None,
})
.await;
let client_event = client.wait(|e| match e {
SwarmEvent::Behaviour(Kad(RoutingUpdated { peer, .. })) => Some(peer),
_ => None,
Expand All @@ -138,6 +159,7 @@ async fn set_client_to_server_mode() {
let (peer, info) = futures::future::join(client_event, server_event).await;

assert_eq!(peer, server_peer_id);
assert_eq!(peer_id, server_peer_id);
assert!(info
.protocols
.iter()
Expand Down

0 comments on commit e7003f5

Please sign in to comment.