Search in sources :

Example 1 with TestMessagesFactory

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

the class AllTypesMessageGenerator method generate.

/**
 * Generate a new {@link AllTypesMessage}.
 *
 * @param seed      Random seed.
 * @param nestedMsg {@code true} if nested messages should be generated, {@code false} otherwise.
 * @return Message.
 */
public static AllTypesMessage generate(long seed, boolean nestedMsg, boolean fillArrays) {
    try {
        var random = new Random(seed);
        AllTypesMessageBuilder message = new TestMessagesFactory().allTypesMessage();
        Field[] fields = AllTypesMessageImpl.builder().getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (!field.getType().isArray() || fillArrays) {
                field.set(message, randomValue(random, field, nestedMsg));
            }
        }
        if (nestedMsg) {
            message.netMsgArrV(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).toArray(NetworkMessage[]::new));
            message.netMsgCollW(IntStream.range(0, 10).mapToObj(unused -> generate(seed, false, fillArrays)).collect(toList()));
            message.newMsgMapX(IntStream.range(0, 10).boxed().collect(toMap(String::valueOf, unused -> generate(seed, false, fillArrays))));
        }
        return message.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Random(java.util.Random) TestMessagesFactory(org.apache.ignite.network.TestMessagesFactory) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 2 with TestMessagesFactory

use of org.apache.ignite.network.TestMessagesFactory 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 3 with TestMessagesFactory

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

the class InboundDecoderTest method testNullNestedMessage.

/**
 * Tests that the {@link InboundDecoder} is able to transfer a message with a nested {@code null} message (happy case is tested by
 * {@link #testAllTypes}).
 */
@Test
public void testNullNestedMessage() {
    NestedMessageMessage msg = new TestMessagesFactory().nestedMessageMessage().nestedMessage(null).build();
    NestedMessageMessage received = sendAndReceive(msg);
    assertNull(received.nestedMessage());
}
Also used : NestedMessageMessage(org.apache.ignite.internal.network.NestedMessageMessage) TestMessagesFactory(org.apache.ignite.network.TestMessagesFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TestMessagesFactory (org.apache.ignite.network.TestMessagesFactory)3 NetworkMessage (org.apache.ignite.network.NetworkMessage)2 Test (org.junit.jupiter.api.Test)2 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 Field (java.lang.reflect.Field)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 NestedMessageMessage (org.apache.ignite.internal.network.NestedMessageMessage)1 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)1 PerSessionSerializationService (org.apache.ignite.internal.network.serialization.PerSessionSerializationService)1 SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)1 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)1 TestMessage (org.apache.ignite.network.TestMessage)1