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;
}
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());
}
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());
}
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);
});
}
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());
}
Aggregations