Search in sources :

Example 1 with Message

use of org.apache.kafka.common.protocol.Message 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 2 with Message

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

the class MessageTest method testMessageVersions.

/**
 * Verify that the JSON files support the same message versions as the
 * schemas accessible through the ApiKey class.
 */
@Test
public void testMessageVersions() {
    for (ApiKeys apiKey : ApiKeys.values()) {
        Message message = null;
        try {
            message = ApiMessageType.fromApiKey(apiKey.id).newRequest();
        } catch (UnsupportedVersionException e) {
            fail("No request message spec found for API " + apiKey);
        }
        assertTrue(apiKey.latestVersion() <= message.highestSupportedVersion(), "Request message spec for " + apiKey + " only " + "supports versions up to " + message.highestSupportedVersion());
        try {
            message = ApiMessageType.fromApiKey(apiKey.id).newResponse();
        } catch (UnsupportedVersionException e) {
            fail("No response message spec found for API " + apiKey);
        }
        assertTrue(apiKey.latestVersion() <= message.highestSupportedVersion(), "Response message spec for " + apiKey + " only " + "supports versions up to " + message.highestSupportedVersion());
    }
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Message(org.apache.kafka.common.protocol.Message) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Test(org.junit.jupiter.api.Test)

Example 3 with Message

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

the class MessageTest method testDuplication.

private void testDuplication(Message message) {
    Message duplicate = message.duplicate();
    assertEquals(duplicate, message);
    assertEquals(message, duplicate);
    assertEquals(duplicate.hashCode(), message.hashCode());
    assertEquals(message.hashCode(), duplicate.hashCode());
}
Also used : Message(org.apache.kafka.common.protocol.Message)

Example 4 with Message

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

the class MessageTest method testJsonRoundTrip.

private void testJsonRoundTrip(short version, Message message, Message expected) throws Exception {
    String jsonConverter = jsonConverterTypeName(message.getClass().getTypeName());
    Class<?> converter = Class.forName(jsonConverter);
    Method writeMethod = converter.getMethod("write", message.getClass(), short.class);
    JsonNode jsonNode = (JsonNode) writeMethod.invoke(null, message, version);
    Method readMethod = converter.getMethod("read", JsonNode.class, short.class);
    Message message2 = (Message) readMethod.invoke(null, jsonNode, version);
    assertEquals(expected, message2);
    assertEquals(expected.hashCode(), message2.hashCode());
    assertEquals(expected.toString(), message2.toString());
}
Also used : Message(org.apache.kafka.common.protocol.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method)

Aggregations

Message (org.apache.kafka.common.protocol.Message)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)1 Test (org.junit.jupiter.api.Test)1