public void ReadShouldStackReadRequestsAndReturnOneAtATime() { var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog); using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port)) { var messages = new[] { "test1", "test2", "test3", "test4" }; var expectedLength = "test1".Length; var payload = new KafkaWriter().Write(messages); using (var socket = new TcpSocket(endpoint, log: TestConfig.WarnLog)) { var tasks = messages.Select(x => socket.ReadAsync(x.Length, CancellationToken.None)).ToArray(); var send = server.SendDataAsync(payload.ToBytes()); Task.WaitAll(tasks); foreach (var task in tasks) { Assert.That(task.Result.Length, Is.EqualTo(expectedLength)); } } } }
public static 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 OffsetResponse) || 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.ToBytes()); } }
public void WhenMessageIsTruncatedThenBufferUnderRunExceptionIsThrown() { // arrange var offset = (long)0; var message = new byte[] { }; var messageSize = message.Length + 1; using (var writer = new KafkaWriter()) { writer.Write(offset) .Write(messageSize) .Write(message); var bytes = writer.ToBytes(); using (var reader = new BigEndianBinaryReader(bytes)) { // act/assert Assert.Throws <BufferUnderRunException>(() => reader.ReadMessages()); } } }