use of com.radixdlt.network.p2p.addressbook.InMemoryAddressBookPersistence in project radixdlt by radixdlt.
the class PeerManagerStaticTest method should_cleanup_existing_entries_from_same_host_port_on_successful_connect.
@Test
public void should_cleanup_existing_entries_from_same_host_port_on_successful_connect() {
var self = makeNodeUri("10.0.0.1", 30000);
var addressBook = new AddressBook(self, mock(P2PConfig.class), cmock(new TypeLiteral<EventDispatcher<PeerEvent>>() {
}), new InMemoryAddressBookPersistence());
var peerManager = new PeerManager(self, P2PConfig.fromRuntimeProperties(defaultProperties()), Addressing.ofNetwork(Network.LOCALNET), () -> addressBook, () -> mock(PendingOutboundChannelsManager.class), mock(SystemCounters.class));
var peer1 = makeNodeUri("10.0.0.2", 30000);
// second address for same peer
var peer2 = makeNodeUri(peer1.getNodeId().getPublicKey(), "10.0.0.2", 30001);
addressBook.addUncheckedPeers(Set.of(peer1, peer2));
// Before connect
addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(2L, entry.getKnownAddresses().stream().count()), Assert::fail);
var peerChanel = mock(PeerChannel.class);
var inboundMessages = cmock(new TypeLiteral<Flowable<InboundMessage>>() {
});
// new key, but same host/port as peer2
var peer = makeNodeUri("10.0.0.2", 30000);
when(peerChanel.getUri()).thenReturn(Optional.of(peer));
when(peerChanel.inboundMessages()).thenReturn(inboundMessages);
when(inboundMessages.toObservable()).thenReturn(cmock(new TypeLiteral<Observable<InboundMessage>>() {
}));
when(peerChanel.isOutbound()).thenReturn(true);
when(peerChanel.getRemoteNodeId()).thenReturn(peer.getNodeId());
peerManager.peerEventProcessor().process(new PeerConnected(peerChanel));
// One address is removed
addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(1L, entry.getKnownAddresses().stream().count()), Assert::fail);
addressBook.findById(peer2.getNodeId()).ifPresentOrElse(entry -> assertEquals(peer2, entry.getKnownAddresses().stream().map(PeerAddressEntry::getUri).iterator().next()), Assert::fail);
}
Aggregations