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);
}
Aggregations