public void ReadShouldNotLoseDataFromStreamOverMultipleReads()
        {
            var endpoint = Endpoint.Resolve(TestConfig.ServerUri(), TestConfig.InfoLog);

            using (var server = new FakeTcpServer(TestConfig.InfoLog, endpoint.IP.Port))
                using (var test = new TcpSocket(endpoint, log: TestConfig.InfoLog))
                {
                    const int    firstMessage  = 99;
                    const string secondMessage = "testmessage";
                    var          bytes         = Encoding.UTF8.GetBytes(secondMessage);

                    var payload = new KafkaWriter()
                                  .Write(firstMessage)
                                  .Write(bytes, false);

                    //send the combined payload
                    var send = server.SendDataAsync(payload.ToBytesNoLength());

                    var firstResponse = test.ReadAsync(4, CancellationToken.None).Result.ToInt32();
                    Assert.That(firstResponse, Is.EqualTo(firstMessage));

                    var secondResponse = Encoding.ASCII.GetString(test.ReadAsync(secondMessage.Length, CancellationToken.None).Result);
                    Assert.That(secondResponse, Is.EqualTo(secondMessage));
                }
        }
        public void DecodeMessageShouldThrowWhenCrcFails()
        {
            var testMessage = new Message(value: "kafka test message.", key: "test");

            using (var writer = new KafkaWriter()) {
                writer.Write(testMessage, false);
                var encoded = writer.ToBytesNoLength();
                encoded[0] += 1;
                using (var reader = new BigEndianBinaryReader(encoded)) {
                    Assert.Throws <CrcValidationException>(() => reader.ReadMessage(encoded.Length, 0).First());
                }
            }
        }
        public void EnsureMessageEncodeAndDecodeAreCompatible(string key, string value)
        {
            var testMessage = new Message(key: key, value: value);

            using (var writer = new KafkaWriter()) {
                writer.Write(testMessage, false);
                var encoded = writer.ToBytesNoLength();
                using (var reader = new BigEndianBinaryReader(encoded)) {
                    var result = reader.ReadMessage(encoded.Length, 0).First();

                    Assert.That(testMessage.Key, Is.EqualTo(result.Key));
                    Assert.That(testMessage.Value, Is.EqualTo(result.Value));
                }
            }
        }
        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, false);
                var result = writer.ToBytesNoLength();
                Assert.That(expected, Is.EqualTo(result));
            }
        }