public void Int16Tests(Int16 number, Byte[] expectedBytes)
 {
     var buffer = new byte[2];
     var encoder = new KafkaEncoder(buffer);
     encoder.Write(number);
     Assert.That(buffer, Is.EqualTo(expectedBytes));
 }
Пример #2
0
        public byte[] Serialize(int value)
        {
            var buffer  = new byte[4];
            var encoder = new KafkaEncoder(buffer);

            encoder.Write(value);
            return(buffer);
        }
 public void StringTests(String value, Byte[] expectedBytes)
 {
     var buffer = new byte[expectedBytes.Length];
     var encoder = new KafkaEncoder(buffer);
     encoder.Write(value);
     Assert.That(encoder.Offset, Is.EqualTo(expectedBytes.Length));
     Assert.That(buffer, Is.EqualTo(expectedBytes));
 }
        public void EnsureHeaderShouldPackCorrectByteLengths()
        {
            var encoder = new KafkaEncoder(new byte[14]);
            var request = new FetchRequest { ClientId = "test", CorrelationId = 123456789 };

            request.EncodeHeader(encoder);
            Assert.That(encoder.Offset, Is.EqualTo(14));
            Assert.That(encoder.Buffer, Is.EqualTo(new byte[] { 0, 1, 0, 0, 7, 91, 205, 21, 0, 4, 116, 101, 115, 116 }));
        }
Пример #5
0
 private KafkaConnection(IPEndPoint serverEndpoint, TcpClient client, int bufferSize = 1048576)
 {
     this.serverEndpoint = serverEndpoint;
     this.client         = client;
     this.stream         = client.GetStream();
     this.buffer         = new byte[bufferSize];
     decoder             = new KafkaDecoder(buffer);
     encoder             = new KafkaEncoder(buffer);
 }
        public void EnsureMessageEncodeAndDecodeAreCompatible(string key, string value)
        {
            var testMessage = new Message(key: key, value: value);

            var buffer = new byte[1024];
            var encoder = new KafkaEncoder(buffer);
            Message.EncodeMessage(testMessage, encoder);

            var decoder = new KafkaDecoder(buffer);
            var result = Message.DecodeMessage(0, 0, decoder, encoder.Offset);

            Assert.That(testMessage.Key, Is.EqualTo(result.Key));
            Assert.That(testMessage.Value, Is.EqualTo(result.Value));
        }
        public void DecodeMessageShouldThrowWhenCrcFails()
        {
            Assert.Throws(Is.TypeOf<FailCrcCheckException>(), () =>
            {
                var testMessage = new Message(value: "kafka test message.", key: "test");
                var buffer = new byte[1024];
                var encoder = new KafkaEncoder(buffer);

                Message.EncodeMessage(testMessage, encoder);
                buffer[0] += 1;

                var decoder = new KafkaDecoder(buffer, 0, encoder.Offset);
                var result = Message.DecodeMessage(0, 0, decoder, encoder.Offset);
            });
        }
Пример #8
0
        public static byte[] CreateMessage(long offset, byte[] key, byte[] payload, byte magicByte = 0, byte attributes = 0)
        {
            var message = new Message
            {
                Attribute = attributes,
                MagicNumber = magicByte,
                Key = key,
                Value = payload
            };

            var buffer = new byte[1024];
            var encoder = new KafkaEncoder(buffer);
            Message.EncodeMessageSet(encoder, new[] { message });

            var result = new byte[encoder.Offset];
            Array.Copy(encoder.Buffer, result, encoder.Offset);

            return result;
        }
        public void EncodeMessageSetEncodesMultipleMessages()
        {
            //expected generated from python library
            var expected = new byte[]
                {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 45, 70, 24, 62, 0, 0, 0, 0, 0, 1, 49, 0, 0, 0, 1, 48, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 16, 90, 65, 40, 168, 0, 0, 0, 0, 0, 1, 49, 0, 0, 0, 1, 49, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 16, 195, 72, 121, 18, 0, 0, 0, 0, 0, 1, 49, 0, 0, 0, 1, 50
                };

            var messages = new[]
                {
                    new Message("0", "1"),
                    new Message("1", "1"),
                    new Message("2", "1")
                };

            var buffer = new byte[expected.Length];
            var encoder = new KafkaEncoder(buffer);
            Message.EncodeMessageSet(encoder, messages);

            Assert.That(buffer, Is.EqualTo(expected));
        }
        public void WhenMessageIsTruncatedThenBufferUnderRunExceptionIsThrown()
        {
            Assert.Throws<BufferUnderRunException>(() =>
            {
                // arrange
                var offset = (Int64)0;
                var message = new Byte[] { };
                var messageSize = 5;
                var payloadBytes = new byte[16];
                var encoder = new KafkaEncoder(payloadBytes);
                encoder.Write(offset);
                encoder.Write(messageSize);
                encoder.Write(message);

                var decoder = new KafkaDecoder(payloadBytes);

                Message.DecodeMessageSet(0, decoder, payloadBytes.Length);
            });
        }