@Test
    public void testParsingRecordWithGarbageAtEnd() {
        MetadataRecordSerde serde = new MetadataRecordSerde();
        RegisterBrokerRecord message = new RegisterBrokerRecord().setBrokerId(1).setBrokerEpoch(2);

        ObjectSerializationCache cache = new ObjectSerializationCache();
        ApiMessageAndVersion messageAndVersion = new ApiMessageAndVersion(message, (short) 0);
        int size = serde.recordSize(messageAndVersion, cache);
        ByteBuffer buffer = ByteBuffer.allocate(size + 1);

        serde.write(messageAndVersion, cache, new ByteBufferAccessor(buffer));
        buffer.clear();
        assertStartsWith("Found 1 byte(s) of garbage after",
                assertThrows(MetadataParseException.class,
                        () -> serde.read(new ByteBufferAccessor(buffer), size + 1)).getMessage());
    }
Test attempting to parse an event which has a malformed message version varint.
metadata/src/test/java/org/apache/kafka/metadata/MetadataRecordSerdeTest.java