Skip to content

Commit

Permalink
unelegant test
Browse files Browse the repository at this point in the history
  • Loading branch information
divagant-martian committed Nov 18, 2021
1 parent 2e73f1d commit ba4df01
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,4 +1803,58 @@ mod tests {
}
}))
}

#[test]
fn test_banning_respects_contract() {
let handler_proto = DummyProtocolsHandler {
keep_alive: KeepAlive::Yes,
};

let mut swarm1 = new_test_swarm::<_, ()>(handler_proto.clone());
let mut swarm2 = new_test_swarm::<_, ()>(handler_proto);

let addr1: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();
let addr2: Multiaddr = multiaddr::Protocol::Memory(rand::random::<u64>()).into();

swarm1.listen_on(addr1.clone().into()).unwrap();
swarm2.listen_on(addr2.clone().into()).unwrap();

let swarm1_id = *swarm1.local_peer_id();

async fn poll(
id: usize,
swarm: &mut Swarm<CallTraceBehaviour<MockBehaviour<DummyProtocolsHandler, ()>>>,
) {
let ev = swarm.select_next_some().await;
log::info!("[{}] {:?}", id, ev);
}

executor::block_on(async {
// Wait for the swarms to establish listeners
poll(1, &mut swarm1).await;
poll(2, &mut swarm2).await;

swarm1.dial(addr2.clone()).unwrap();
log::info!("Swarm 2 banning swarm 1\n\n");
swarm2.ban_peer_id(swarm1_id);

// Incoming connection
poll(2, &mut swarm2).await;

// Connection established
poll(2, &mut swarm2).await;
// Connection established
poll(1, &mut swarm1).await;

// Both see connections as closed
poll(1, &mut swarm1).await;
poll(2, &mut swarm2).await;

// Check what swarm2's behaviour got
assert_eq!(
swarm2.behaviour.inject_connection_established,
swarm2.behaviour.inject_connection_closed
);
})
}
}

0 comments on commit ba4df01

Please sign in to comment.