public static ArraySegment <byte> EncodeResponseBytes <T>(IRequestContext context, T response) where T : IResponse { using (var writer = new KafkaWriter()) { // From http://kafka.apache.org/protocol.html#protocol_messages // // Response Header => correlation_id // correlation_id => INT32 -- The user-supplied value passed in with the request writer.Write(context.CorrelationId); // ReSharper disable once UnusedVariable var isEncoded = TryEncodeResponse(writer, context, response as ProduceResponse) || TryEncodeResponse(writer, context, response as FetchResponse) || TryEncodeResponse(writer, context, response as OffsetsResponse) || TryEncodeResponse(writer, context, response as MetadataResponse) || TryEncodeResponse(writer, context, response as OffsetCommitResponse) || TryEncodeResponse(writer, context, response as OffsetFetchResponse) || TryEncodeResponse(writer, context, response as GroupCoordinatorResponse) || TryEncodeResponse(writer, context, response as JoinGroupResponse) || TryEncodeResponse(writer, context, response as HeartbeatResponse) || TryEncodeResponse(writer, context, response as LeaveGroupResponse) || TryEncodeResponse(writer, context, response as SyncGroupResponse) || TryEncodeResponse(writer, context, response as DescribeGroupsResponse) || TryEncodeResponse(writer, context, response as ListGroupsResponse) || TryEncodeResponse(writer, context, response as SaslHandshakeResponse) || TryEncodeResponse(writer, context, response as ApiVersionsResponse) || TryEncodeResponse(writer, context, response as CreateTopicsResponse) || TryEncodeResponse(writer, context, response as DeleteTopicsResponse); return(writer.ToSegment()); } }
public void WhenMessageIsExactlyTheSizeOfBufferThenMessageIsDecoded() { // arrange var expectedPayloadBytes = new ArraySegment <byte>(new byte[] { 1, 2, 3, 4 }); using (var writer = new KafkaWriter()) { writer.Write(0L); using (writer.MarkForLength()) { new Message(expectedPayloadBytes, new ArraySegment <byte>(new byte[] { 0 }), 0, version: 0).WriteTo(writer); } var segment = writer.ToSegment(); // act/assert using (var reader = new KafkaReader(segment)) { var messages = reader.ReadMessages(0); var actualPayload = messages.First().Value; // assert var expectedPayload = new byte[] { 1, 2, 3, 4 }; CollectionAssert.AreEqual(expectedPayload, actualPayload); } } }
public void Int64Tests(long number, byte[] expectedBytes) { // arrange var writer = new KafkaWriter(); // act writer.Write(number); // assert var actualBytes = writer.ToSegment(false); Assert.That(actualBytes.ToArray(), Is.EqualTo(expectedBytes)); }
public void StringTests(string value, byte[] expectedBytes) { // arrange var writer = new KafkaWriter(); // act writer.Write(value); // assert var actualBytes = writer.ToSegment(false); Assert.That(actualBytes.ToArray(), Is.EqualTo(expectedBytes)); }
public void DecodeMessageShouldThrowWhenCrcFails() { var testMessage = new Message(value: "kafka test message.", key: "test"); using (var writer = new KafkaWriter()) { testMessage.WriteTo(writer); var encoded = writer.ToSegment(false); encoded.Array[encoded.Offset] += 1; using (var reader = new KafkaReader(encoded)) { Assert.Throws <CrcValidationException>(() => reader.ReadMessage(encoded.Count, 0).First()); } } }
public void EnsureMessageEncodeAndDecodeAreCompatible(string key, string value) { var testMessage = new Message(key: key, value: value); using (var writer = new KafkaWriter()) { testMessage.WriteTo(writer); var encoded = writer.ToSegment(false); using (var reader = new KafkaReader(encoded)) { var result = reader.ReadMessage(encoded.Count, 0).First(); Assert.That(testMessage.Key, Is.EqualTo(result.Key)); Assert.That(testMessage.Value, Is.EqualTo(result.Value)); } } }
public void WhenMessageIsTruncatedThenBufferUnderRunExceptionIsThrown() { // arrange var message = new byte[] { }; var messageSize = message.Length + 1; using (var writer = new KafkaWriter()) { writer.Write(0L) .Write(messageSize) .Write(new ArraySegment <byte>(message)); var segment = writer.ToSegment(); using (var reader = new KafkaReader(segment)) { // act/assert Assert.Throws <BufferUnderRunException>(() => reader.ReadMessages(0)); } } }
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") }; using (var writer = new KafkaWriter()) { writer.Write(messages); var result = writer.ToSegment(false); Assert.That(expected, Is.EqualTo(result)); } }