Search in sources :

Example 6 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class RequestTestUtils method serializeRequestHeader.

public static ByteBuffer serializeRequestHeader(RequestHeader header) {
    ObjectSerializationCache serializationCache = new ObjectSerializationCache();
    ByteBuffer buffer = ByteBuffer.allocate(header.size(serializationCache));
    header.write(buffer, serializationCache);
    buffer.flip();
    return buffer;
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBuffer(java.nio.ByteBuffer)

Example 7 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class MessageTest method testByteBufferRoundTrip.

private void testByteBufferRoundTrip(short version, Message message, Message expected) throws Exception {
    ObjectSerializationCache cache = new ObjectSerializationCache();
    int size = message.size(cache, version);
    ByteBuffer buf = ByteBuffer.allocate(size);
    ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
    message.write(byteBufferAccessor, cache, version);
    assertEquals(size, buf.position(), "The result of the size function does not match the number of bytes " + "written for version " + version);
    Message message2 = message.getClass().getConstructor().newInstance();
    buf.flip();
    message2.read(byteBufferAccessor, version);
    assertEquals(size, buf.position(), "The result of the size function does not match the number of bytes " + "read back in for version " + version);
    assertEquals(expected, message2, "The message object created after a round trip did not match for " + "version " + version);
    assertEquals(expected.hashCode(), message2.hashCode());
    assertEquals(expected.toString(), message2.toString());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) Message(org.apache.kafka.common.protocol.Message) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer)

Example 8 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class MessageTest method verifyWriteSucceeds.

private void verifyWriteSucceeds(short version, Message message) {
    ObjectSerializationCache cache = new ObjectSerializationCache();
    int size = message.size(cache, version);
    ByteBuffer buf = ByteBuffer.allocate(size * 2);
    ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
    message.write(byteBufferAccessor, cache, version);
    assertEquals(size, buf.position(), "Expected the serialized size to be " + size + ", but it was " + buf.position());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer)

Example 9 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class MessageTest method verifyWriteRaisesNpe.

private void verifyWriteRaisesNpe(short version, Message message) {
    ObjectSerializationCache cache = new ObjectSerializationCache();
    assertThrows(NullPointerException.class, () -> {
        int size = message.size(cache, version);
        ByteBuffer buf = ByteBuffer.allocate(size);
        ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
        message.write(byteBufferAccessor, cache, version);
    });
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer)

Example 10 with ObjectSerializationCache

use of org.apache.kafka.common.protocol.ObjectSerializationCache in project kafka by apache.

the class MessageTest method testLongTaggedString.

@Test
public void testLongTaggedString() throws Exception {
    char[] chars = new char[1024];
    Arrays.fill(chars, 'a');
    String longString = new String(chars);
    SimpleExampleMessageData message = new SimpleExampleMessageData().setMyString(longString);
    ObjectSerializationCache cache = new ObjectSerializationCache();
    short version = 1;
    int size = message.size(cache, version);
    ByteBuffer buf = ByteBuffer.allocate(size);
    ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
    message.write(byteBufferAccessor, cache, version);
    assertEquals(size, buf.position());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)21 ByteBuffer (java.nio.ByteBuffer)13 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)13 Test (org.junit.jupiter.api.Test)7 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 FetchResponseData (org.apache.kafka.common.message.FetchResponseData)2 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 OptionalLong (java.util.OptionalLong)1 ExecutionException (java.util.concurrent.ExecutionException)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 FetchRequestData (org.apache.kafka.common.message.FetchRequestData)1 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)1 Voter (org.apache.kafka.common.message.LeaderChangeMessage.Voter)1 RequestHeaderData (org.apache.kafka.common.message.RequestHeaderData)1 RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)1 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)1 Send (org.apache.kafka.common.network.Send)1