public void BufferedMessageSetWriteToValidSequence()
        {
            byte[] messageBytes = { 1, 2, 3, 4, 5 };
            var    msg1         = new Message(messageBytes)
            {
                Offset = 0
            };
            var msg2 = new Message(messageBytes);

            msg2.Offset = 1;
            MessageSet messageSet = new BufferedMessageSet(new List <Message>()
            {
                msg1, msg2
            }, 0);
            var ms = new MemoryStream();

            messageSet.WriteTo(ms);

            var reader     = new KafkaBinaryReader(ms);
            int baseOffset = 0;

            for (int i = 0; i < 2; ++i)
            {
                reader.ReadInt64().Should().Be(i);  // offset
                var msgLength = reader.ReadInt32(); // length
                msgLength.Should().Be(Message.DefaultHeaderSize + msg1.PayloadSize);
                reader.ReadUInt32().Should().Be(Crc32Hasher.ComputeCrcUint32(ms.GetBuffer(), baseOffset + 8 + 4 + 4, msgLength - 4));
                reader.ReadByte().Should().Be(0);                    // magic
                reader.ReadByte().Should().Be(msg1.Attributes);
                reader.ReadInt32().Should().Be(-1);                  // key length
                reader.ReadInt32().Should().Be(messageBytes.Length); // message length
                reader.ReadBytes(messageBytes.Length).SequenceEqual(messageBytes).Should().BeTrue();
                baseOffset += 8 + 4 + msgLength;
            }
        }
        public void GetBytesValidSequence()
        {
            var     payload = Encoding.UTF8.GetBytes("kafka");
            Message message = new Message(payload, CompressionCodecs.NoCompressionCodec);

            MemoryStream ms = new MemoryStream();

            message.WriteTo(ms);

            Assert.AreEqual(message.Size, ms.Length);

            var crc = Crc32Hasher.ComputeCrcUint32(ms.GetBuffer(), 4, (int)(ms.Length - 4));

            // first 4 bytes = the crc
            using (var reader = new KafkaBinaryReader(ms))
            {
                Assert.AreEqual(crc, reader.ReadUInt32());

                // magic
                Assert.AreEqual(message.Magic, reader.ReadByte());

                // attributes
                Assert.AreEqual((byte)0, reader.ReadByte());

                // key size
                Assert.AreEqual(-1, reader.ReadInt32());

                // payload size
                Assert.AreEqual(payload.Length, reader.ReadInt32());

                // remaining bytes = the payload
                payload.SequenceEqual(reader.ReadBytes(10)).Should().BeTrue();
            }
        }
 private uint ComputeChecksum(byte[] message, int offset, int count)
 {
     return(Crc32Hasher.ComputeCrcUint32(message, offset, count));
 }