public void StringTests(String expectedValue, Byte[] givenBytes)
 {
     var decoder = new KafkaDecoder(givenBytes);
     var actualValue = decoder.ReadString();
     Assert.That(decoder.Offset, Is.EqualTo(givenBytes.Length));
     Assert.That(actualValue, Is.EqualTo(expectedValue));
 }
Exemplo n.º 2
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 DecodeMessageSetShouldHandleResponseWithMaxBufferSizeHit()
        {
            //This message set has a truncated message bytes at the end of it
            var decoder = new KafkaDecoder(MessageHelper.FetchResponseMaxBytesOverflow);
            var result = Message.DecodeMessageSet(0, decoder, decoder.Length);

            var message = Encoding.UTF8.GetString(result.First().Value);
            
            Assert.That(message, Is.EqualTo("test"));
            Assert.That(result.Count, Is.EqualTo(529));
        }
        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);
            });
        }
 public void Int16Tests(Int16 expectedValue, Byte[] givenBytes)
 {
     var decoder = new KafkaDecoder(givenBytes);
     var actualValue = decoder.ReadInt16();
     Assert.That(actualValue, Is.EqualTo(expectedValue));
 }
        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);
            });
        }
        public void WhenMessageIsExactlyTheSizeOfBufferThenMessageIsDecoded()
        {
            // arrange
            var expectedPayloadBytes = new Byte[] { 1, 2, 3, 4 };
            var payload = MessageHelper.CreateMessage(0, new Byte[] { 0 }, expectedPayloadBytes);

            // act/assert
            var decoder = new KafkaDecoder(payload, 0, payload.Length);
            var messages = Message.DecodeMessageSet(0, decoder, payload.Length);
            var actualPayload = messages.First().Value;

            // assert
            var expectedPayload = new Byte[] { 1, 2, 3, 4 };
            CollectionAssert.AreEqual(expectedPayload, actualPayload);
        }