use of org.apache.ignite.internal.network.handshake.HandshakeResult in project ignite-3 by apache.
the class NettyServerTest method testHandshakeManagerInvoked.
/**
* Tests that handshake manager is invoked upon a client connecting to a server.
*
* @throws Exception If failed.
*/
@Test
public void testHandshakeManagerInvoked() throws Exception {
HandshakeManager handshakeManager = mock(HandshakeManager.class);
when(handshakeManager.handshakeFuture()).thenReturn(CompletableFuture.completedFuture(mock(NettySender.class)));
HandshakeResult noOp = HandshakeResult.noOp();
when(handshakeManager.init(any())).thenReturn(noOp);
when(handshakeManager.onConnectionOpen(any())).thenReturn(noOp);
when(handshakeManager.onMessage(any(), any())).thenReturn(noOp);
MessageSerializationRegistry registry = mock(MessageSerializationRegistry.class);
when(registry.createDeserializer(anyShort(), anyShort())).thenReturn(new MessageDeserializer<>() {
/**
* {@inheritDoc}
*/
@Override
public boolean readMessage(MessageReader reader) throws MessageMappingException {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public Class<NetworkMessage> klass() {
return NetworkMessage.class;
}
/**
* {@inheritDoc}
*/
@Override
public NetworkMessage getMessage() {
return mock(NetworkMessage.class);
}
});
bootstrapFactory = new NettyBootstrapFactory(serverCfg, "");
bootstrapFactory.start();
server = new NettyServer(serverCfg.value(), () -> handshakeManager, sender -> {
}, (message) -> {
}, new SerializationService(registry, mock(UserObjectSerializationContext.class)), bootstrapFactory);
server.start().get(3, TimeUnit.SECONDS);
CompletableFuture<Channel> connectFut = NettyUtils.toChannelCompletableFuture(new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup()).handler(new ChannelInitializer<>() {
/**
* {@inheritDoc}
*/
@Override
protected void initChannel(Channel ch) throws Exception {
// No-op.
}
}).connect(server.address()));
Channel channel = connectFut.get(3, TimeUnit.SECONDS);
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
// One message only.
for (int i = 0; i < (NetworkMessage.MSG_TYPE_SIZE_BYTES + 1); i++) {
buffer.writeByte(1);
}
channel.writeAndFlush(buffer).get(3, TimeUnit.SECONDS);
channel.close().get(3, TimeUnit.SECONDS);
InOrder order = Mockito.inOrder(handshakeManager);
order.verify(handshakeManager, timeout()).init(any());
order.verify(handshakeManager, timeout()).handshakeFuture();
order.verify(handshakeManager, timeout()).onConnectionOpen(any());
order.verify(handshakeManager, timeout()).onMessage(any(), any());
}
use of org.apache.ignite.internal.network.handshake.HandshakeResult in project ignite-3 by apache.
the class HandshakeHandler method handlerAdded.
/**
* {@inheritDoc}
*/
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
HandshakeResult handshakeResult = manager.init(ctx.channel());
handleHandshakeAction(handshakeResult, ctx);
}
use of org.apache.ignite.internal.network.handshake.HandshakeResult in project ignite-3 by apache.
the class HandshakeHandler method channelRead.
/**
* {@inheritDoc}
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
HandshakeResult handshakeResult = manager.onMessage(ctx.channel(), (NetworkMessage) msg);
handleHandshakeAction(handshakeResult, ctx);
// No need to forward the message to the next handler as this message only matters for a handshake.
}
use of org.apache.ignite.internal.network.handshake.HandshakeResult in project ignite-3 by apache.
the class HandshakeHandler method channelActive.
/**
* {@inheritDoc}
*/
@Override
public void channelActive(ChannelHandlerContext ctx) {
HandshakeResult handshakeResult = manager.onConnectionOpen(ctx.channel());
manager.handshakeFuture().whenComplete((unused, throwable) -> {
if (throwable != null) {
LOG.debug("Error when performing handshake", throwable);
ctx.close();
}
});
handleHandshakeAction(handshakeResult, ctx);
ctx.fireChannelActive();
}
Aggregations