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