Search in sources :

Example 1 with HandshakeResult

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) CompletableFuture(java.util.concurrent.CompletableFuture) MessageReader(org.apache.ignite.network.serialization.MessageReader) ArgumentMatchers.anyShort(org.mockito.ArgumentMatchers.anyShort) InjectConfiguration(org.apache.ignite.internal.configuration.testframework.InjectConfiguration) HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult) ByteBuf(io.netty.buffer.ByteBuf) ChannelPromise(io.netty.channel.ChannelPromise) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ConfigurationExtension(org.apache.ignite.internal.configuration.testframework.ConfigurationExtension) MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) InOrder(org.mockito.InOrder) ChannelInitializer(io.netty.channel.ChannelInitializer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkMessage(org.apache.ignite.network.NetworkMessage) Mockito.when(org.mockito.Mockito.when) ServerChannel(io.netty.channel.ServerChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) VerificationMode(org.mockito.verification.VerificationMode) Bootstrap(io.netty.bootstrap.Bootstrap) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HandshakeManager(org.apache.ignite.internal.network.handshake.HandshakeManager) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) Mockito.mock(org.mockito.Mockito.mock) HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) InOrder(org.mockito.InOrder) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) MessageReader(org.apache.ignite.network.serialization.MessageReader) ByteBuf(io.netty.buffer.ByteBuf) MessageMappingException(org.apache.ignite.network.serialization.MessageMappingException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) HandshakeManager(org.apache.ignite.internal.network.handshake.HandshakeManager) Bootstrap(io.netty.bootstrap.Bootstrap) NetworkMessage(org.apache.ignite.network.NetworkMessage) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 2 with HandshakeResult

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);
}
Also used : HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult)

Example 3 with HandshakeResult

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.
}
Also used : HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult)

Example 4 with HandshakeResult

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();
}
Also used : HandshakeResult(org.apache.ignite.internal.network.handshake.HandshakeResult)

Aggregations

HandshakeResult (org.apache.ignite.internal.network.handshake.HandshakeResult)4 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelPromise (io.netty.channel.ChannelPromise)1 ServerChannel (io.netty.channel.ServerChannel)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)1 ConfigurationExtension (org.apache.ignite.internal.configuration.testframework.ConfigurationExtension)1 InjectConfiguration (org.apache.ignite.internal.configuration.testframework.InjectConfiguration)1 HandshakeManager (org.apache.ignite.internal.network.handshake.HandshakeManager)1 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)1 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)1