public void GetBytesValidFormat() { List<ProducerRequest> requests = new List<ProducerRequest> { new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic b", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic c", 0, new List<Message> { new Message(new byte[10]) }) }; MultiProducerRequest request = new MultiProducerRequest(requests); // format = len(request) + requesttype + requestcount + requestpackage // total byte count = 4 + (2 + 2 + 144) byte[] bytes = request.GetBytes(); Assert.IsNotNull(bytes); Assert.AreEqual(152, bytes.Length); // first 4 bytes = the length of the request Assert.AreEqual(148, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray<byte>()), 0)); // next 2 bytes = the RequestType which in this case should be Produce Assert.AreEqual((short)RequestType.MultiProduce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray<byte>()), 0)); // next 2 bytes = the number of messages Assert.AreEqual((short)4, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray<byte>()), 0)); }
/// <summary> /// Sends a request to Kafka. /// </summary> /// <param name="request">The request to send to Kafka.</param> public void Send(MultiProducerRequest request) { if (request.IsValid()) { using (KafkaConnection connection = new KafkaConnection(Server, Port)) { connection.Write(request); } } }
public void IsValidTrue() { List<ProducerRequest> requests = new List<ProducerRequest> { new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic b", 0, new List<Message> { new Message(new byte[10]) }), new ProducerRequest("topic c", 0, new List<Message> { new Message(new byte[10]) }) }; MultiProducerRequest multiRequest = new MultiProducerRequest(requests); Assert.IsTrue(multiRequest.IsValid()); }
public void ProducerSendMultiRequest() { List<ProducerRequest> requests = new List<ProducerRequest> { new ProducerRequest("test", 0, new List<Message> { new Message(Encoding.UTF8.GetBytes("1: " + DateTime.UtcNow)) }), new ProducerRequest("test", 0, new List<Message> { new Message(Encoding.UTF8.GetBytes("2: " + DateTime.UtcNow)) }), new ProducerRequest("testa", 0, new List<Message> { new Message(Encoding.UTF8.GetBytes("3: " + DateTime.UtcNow)) }), new ProducerRequest("testa", 0, new List<Message> { new Message(Encoding.UTF8.GetBytes("4: " + DateTime.UtcNow)) }) }; MultiProducerRequest request = new MultiProducerRequest(requests); Producer producer = new Producer(KafkaServer, KafkaPort); producer.Send(request); }
public void IsValidNullRequests() { MultiProducerRequest multiRequest = new MultiProducerRequest(null); Assert.IsFalse(multiRequest.IsValid()); }
public void IsValidNoRequests() { MultiProducerRequest multiRequest = new MultiProducerRequest(new List<ProducerRequest>()); Assert.IsFalse(multiRequest.IsValid()); }
/// <summary> /// Writes a multi-producer request to the server. /// </summary> /// <remarks> /// Write timeout is defaulted to infitite. /// </remarks> /// <param name="request">The <see cref="MultiProducerRequest"/> to send to the server.</param> public void Write(MultiProducerRequest request) { Write(request.GetBytes()); }