Search in sources :

Example 1 with PeerEvent

use of com.radixdlt.network.p2p.PeerEvent in project radixdlt by radixdlt.

the class MockP2PNetwork method createChannel.

void createChannel(int clientPeerIndex, RadixNodeUri serverPeerUri) {
    final var clientPeer = nodes.get(clientPeerIndex);
    final var serverPeerOpt = nodes.stream().filter(p -> p.uri.getHost().equals(serverPeerUri.getHost()) && p.uri.getPort() == serverPeerUri.getPort()).findAny();
    final var clientSocketChannel = mock(SocketChannel.class);
    final var clientChannel = new PeerChannel(clientPeer.injector.getInstance(P2PConfig.class), Addressing.ofNetwork(Network.LOCALNET), 1, "fork1", clientPeer.injector.getInstance(SystemCounters.class), clientPeer.injector.getInstance(Serialization.class), new SecureRandom(), ECKeyOps.fromKeyPair(clientPeer.keyPair), clientPeer.injector.getInstance(new Key<EventDispatcher<PeerEvent>>() {
    }), Optional.of(serverPeerUri), clientSocketChannel, Optional.empty());
    if (serverPeerOpt.isEmpty()) {
        clientChannel.channelActive(null);
        clientChannel.channelInactive(null);
        return;
    }
    final var serverPeer = serverPeerOpt.get();
    final var serverSocketChannel = mock(SocketChannel.class);
    final var serverChannel = new PeerChannel(serverPeer.injector.getInstance(P2PConfig.class), Addressing.ofNetwork(Network.LOCALNET), 1, "fork1", serverPeer.injector.getInstance(SystemCounters.class), serverPeer.injector.getInstance(Serialization.class), new SecureRandom(), ECKeyOps.fromKeyPair(serverPeer.keyPair), serverPeer.injector.getInstance(new Key<EventDispatcher<PeerEvent>>() {
    }), Optional.empty(), serverSocketChannel, Optional.empty());
    when(clientSocketChannel.writeAndFlush(any())).thenAnswer(inv -> {
        final var rawData = inv.getArgument(0);
        serverChannel.channelRead0(null, (ByteBuf) rawData);
        return null;
    });
    when(serverSocketChannel.writeAndFlush(any())).thenAnswer(inv -> {
        final var rawData = inv.getArgument(0);
        clientChannel.channelRead0(null, (ByteBuf) rawData);
        return null;
    });
    when(clientSocketChannel.close()).thenAnswer(inv -> {
        final var mockChannel = mock(ChannelHandlerContext.class);
        when(mockChannel.channel()).thenReturn(mock(Channel.class));
        clientChannel.channelInactive(mockChannel);
        serverChannel.channelInactive(mockChannel);
        return null;
    });
    when(serverSocketChannel.close()).thenAnswer(inv -> {
        final var mockChannel = mock(ChannelHandlerContext.class);
        when(mockChannel.channel()).thenReturn(mock(Channel.class));
        serverChannel.channelInactive(mockChannel);
        clientChannel.channelInactive(mockChannel);
        return null;
    });
    serverChannel.channelActive(null);
    clientChannel.channelActive(null);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SystemCounters(com.radixdlt.counters.SystemCounters) Addressing(com.radixdlt.networks.Addressing) EventDispatcher(com.radixdlt.environment.EventDispatcher) Network(com.radixdlt.networks.Network) Serialization(com.radixdlt.serialization.Serialization) Key(com.google.inject.Key) ECKeyOps(com.radixdlt.crypto.ECKeyOps) RadixNodeUri(com.radixdlt.network.p2p.RadixNodeUri) Mockito.when(org.mockito.Mockito.when) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Channel(io.netty.channel.Channel) SecureRandom(java.security.SecureRandom) P2PConfig(com.radixdlt.network.p2p.P2PConfig) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PeerEvent(com.radixdlt.network.p2p.PeerEvent) ImmutableList(com.google.common.collect.ImmutableList) ByteBuf(io.netty.buffer.ByteBuf) Optional(java.util.Optional) SocketChannel(io.netty.channel.socket.SocketChannel) Mockito.mock(org.mockito.Mockito.mock) Serialization(com.radixdlt.serialization.Serialization) PeerEvent(com.radixdlt.network.p2p.PeerEvent) P2PConfig(com.radixdlt.network.p2p.P2PConfig) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) SystemCounters(com.radixdlt.counters.SystemCounters) SecureRandom(java.security.SecureRandom) PeerChannel(com.radixdlt.network.p2p.transport.PeerChannel) Key(com.google.inject.Key)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Key (com.google.inject.Key)1 SystemCounters (com.radixdlt.counters.SystemCounters)1 ECKeyOps (com.radixdlt.crypto.ECKeyOps)1 EventDispatcher (com.radixdlt.environment.EventDispatcher)1 P2PConfig (com.radixdlt.network.p2p.P2PConfig)1 PeerEvent (com.radixdlt.network.p2p.PeerEvent)1 RadixNodeUri (com.radixdlt.network.p2p.RadixNodeUri)1 PeerChannel (com.radixdlt.network.p2p.transport.PeerChannel)1 Addressing (com.radixdlt.networks.Addressing)1 Network (com.radixdlt.networks.Network)1 Serialization (com.radixdlt.serialization.Serialization)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 SecureRandom (java.security.SecureRandom)1 Optional (java.util.Optional)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.mock (org.mockito.Mockito.mock)1