Search in sources :

Example 1 with TestMessage

use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.

the class InboundDecoderTest method testPartialReadWithReuseBuffer.

/**
 * Tests that an {@link InboundDecoder} can handle a {@link ByteBuf} where reader index is not {@code 0} at the start of the {@link
 * InboundDecoder#decode}.
 *
 * @throws Exception If failed.
 */
@Test
public void testPartialReadWithReuseBuffer() throws Exception {
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    var channel = new EmbeddedChannel();
    Mockito.doReturn(channel).when(ctx).channel();
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var perSessionSerializationService = new PerSessionSerializationService(serializationService);
    final var decoder = new InboundDecoder(perSessionSerializationService);
    final var list = new ArrayList<>();
    var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
    var msg = new TestMessagesFactory().testMessage().msg("abcdefghijklmn").build();
    MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
    ByteBuffer nioBuffer = ByteBuffer.allocate(10_000);
    writer.setBuffer(nioBuffer);
    // Write message to the ByteBuffer.
    boolean fullyWritten = serializer.writeMessage(msg, writer);
    assertTrue(fullyWritten);
    nioBuffer.flip();
    ByteBuf buffer = allocator.buffer();
    // Write first 3 bytes of a message.
    for (int i = 0; i < 3; i++) {
        buffer.writeByte(nioBuffer.get());
    }
    decoder.decode(ctx, buffer, list);
    // At this point a header and a first byte of a message have been decoded.
    assertEquals(0, list.size());
    // Write next 3 bytes of a message.
    for (int i = 0; i < 3; i++) {
        buffer.writeByte(nioBuffer.get());
    }
    // Reader index of a buffer is not zero and it must be handled correctly by the InboundDecoder.
    decoder.decode(ctx, buffer, list);
    // Check if reader index has been tracked correctly.
    assertEquals(6, buffer.readerIndex());
    assertEquals(0, list.size());
    buffer.writeBytes(nioBuffer);
    decoder.decode(ctx, buffer, list);
    assertEquals(1, list.size());
    TestMessage actualMessage = (TestMessage) list.get(0);
    assertEquals(msg, actualMessage);
}
Also used : ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) TestMessagesFactory(org.apache.ignite.network.TestMessagesFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) TestMessage(org.apache.ignite.network.TestMessage) NetworkMessage(org.apache.ignite.network.NetworkMessage) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TestMessage

use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.

the class ItScaleCubeNetworkMessagingTest method testSendMessageToSelf.

/**
 * Sends a message from a node to itself and verifies that it gets delivered successfully.
 *
 * @throws Exception in case of errors.
 */
@Test
public void testSendMessageToSelf(TestInfo testInfo) throws Exception {
    testCluster = new Cluster(1, testInfo);
    testCluster.startAwait();
    ClusterService member = testCluster.members.get(0);
    ClusterNode self = member.topologyService().localMember();
    class Data {

        private final TestMessage message;

        private final NetworkAddress sender;

        private final Long correlationId;

        private Data(TestMessage message, NetworkAddress sender, Long correlationId) {
            this.message = message;
            this.sender = sender;
            this.correlationId = correlationId;
        }
    }
    var dataFuture = new CompletableFuture<Data>();
    member.messagingService().addMessageHandler(TestMessageTypes.class, (message, senderAddr, correlationId) -> dataFuture.complete(new Data((TestMessage) message, senderAddr, correlationId)));
    var requestMessage = messageFactory.testMessage().msg("request").build();
    member.messagingService().send(self, requestMessage);
    Data actualData = dataFuture.get(3, TimeUnit.SECONDS);
    assertThat(actualData.message.msg(), is(requestMessage.msg()));
    assertThat(actualData.sender.consistentId(), is(self.name()));
    assertNull(actualData.correlationId);
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) TestMessage(org.apache.ignite.network.TestMessage) Test(org.junit.jupiter.api.Test)

Example 3 with TestMessage

use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.

the class ItConnectionManagerTest method testCanReconnectAfterFail.

/**
 * Tests that after a channel was closed, a new channel is opened upon a request.
 *
 * @throws Exception If failed.
 */
@Test
public void testCanReconnectAfterFail() throws Exception {
    String msgText = "test";
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    IgniteBiTuple<ConnectionManager, NettyBootstrapFactory> manager2 = startManager(port2);
    NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
    manager2.get1().stop();
    manager2.get2().stop();
    final NettySender finalSender = sender;
    assertThrows(ClosedChannelException.class, () -> {
        try {
            finalSender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw e.getCause();
        }
    });
    manager2 = startManager(port2);
    var fut = new CompletableFuture<NetworkMessage>();
    manager2.get1().addListener((obj) -> fut.complete(obj.message()));
    sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DecoderException(io.netty.handler.codec.DecoderException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) CompletableFuture(java.util.concurrent.CompletableFuture) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Example 4 with TestMessage

use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.

the class ItConnectionManagerTest method testReuseIncomingConnection.

/**
 * Tests that incoming connection is reused for sending messages.
 *
 * @throws Exception If failed.
 */
@Test
public void testReuseIncomingConnection() throws Exception {
    final String msgText = "test";
    TestMessage testMessage = messageFactory.testMessage().msg("test").build();
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    ConnectionManager manager2 = startManager(port2).get1();
    var fut = new CompletableFuture<NetworkMessage>();
    manager1.addListener((obj) -> fut.complete(obj.message()));
    NettySender senderFrom1to2 = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    // Ensure a handshake has finished on both sides by sending a message.
    // TODO: IGNITE-14085 When the recovery protocol is implemented replace this with simple
    // CompletableFuture#get called on the send future.
    var messageReceivedOn2 = new CompletableFuture<Void>();
    // If the message is received, that means that the handshake was successfully performed.
    manager2.addListener((message) -> messageReceivedOn2.complete(null));
    senderFrom1to2.send(new OutNetworkObject(testMessage, Collections.emptyList()));
    messageReceivedOn2.get(3, TimeUnit.SECONDS);
    NettySender senderFrom2to1 = manager2.channel(manager1.consistentId(), new InetSocketAddress(port1)).get(3, TimeUnit.SECONDS);
    InetSocketAddress clientLocalAddress = (InetSocketAddress) senderFrom1to2.channel().localAddress();
    InetSocketAddress clientRemoteAddress = (InetSocketAddress) senderFrom2to1.channel().remoteAddress();
    assertEquals(clientLocalAddress, clientRemoteAddress);
    senderFrom2to1.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Example 5 with TestMessage

use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.

the class ItConnectionManagerTest method testSentSuccessfully.

/**
 * Tests that a message is sent successfully using the ConnectionManager.
 *
 * @throws Exception If failed.
 */
@Test
public void testSentSuccessfully() throws Exception {
    String msgText = "test";
    int port1 = 4000;
    int port2 = 4001;
    ConnectionManager manager1 = startManager(port1).get1();
    ConnectionManager manager2 = startManager(port2).get1();
    var fut = new CompletableFuture<NetworkMessage>();
    manager2.addListener((obj) -> fut.complete(obj.message()));
    NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
    TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
    sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
    NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
    assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) TestMessage(org.apache.ignite.network.TestMessage) OutNetworkObject(org.apache.ignite.network.OutNetworkObject) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Aggregations

TestMessage (org.apache.ignite.network.TestMessage)8 Test (org.junit.jupiter.api.Test)7 CompletableFuture (java.util.concurrent.CompletableFuture)4 NetworkMessage (org.apache.ignite.network.NetworkMessage)4 OutNetworkObject (org.apache.ignite.network.OutNetworkObject)4 InetSocketAddress (java.net.InetSocketAddress)3 ClusterNode (org.apache.ignite.network.ClusterNode)3 ClusterService (org.apache.ignite.network.ClusterService)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 DecoderException (io.netty.handler.codec.DecoderException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1