public void ShouldAbleToParseBroker()
        {
            var broker = Broker.CreateBroker(1, @"{'host': 'host1', 'port': 1234}");

            broker.Id.Should().Be(1);
            broker.Host.Should().Be("host1");
            broker.Port.Should().Be(1234);
            broker.SizeInBytes.Should().Be(BitWorks.GetShortStringLength("host1", "UTF-8") + 8);
        }
        public void ShouldAbleToCreateBroker()
        {
            var broker = new Broker(1, "host1", 1234);

            broker.Id.Should().Be(1);
            broker.Host.Should().Be("host1");
            broker.Port.Should().Be(1234);
            broker.SizeInBytes.Should().Be(BitWorks.GetShortStringLength("host1", "UTF-8") + 8);
        }
示例#3
0
        public int GetRequestLength()
        {
            var size = DefaultHeaderSize8 +
                       FetchRequest.DefaultVersionIdSize +
                       FetchRequest.DefaultCorrelationIdSize +
                       BitWorks.GetShortStringLength(clientId, DefaultEncoding) +
                       DefaultNumberOfTopicsSize +
                       Topics.Sum(x => BitWorks.GetShortStringLength(x, DefaultEncoding));

            return(size);
        }
示例#4
0
 public int GetRequestLength()
 {
     return(DefaultRequestSizeSize +
            DefaultRequestIdSize +
            DefaultVersionIdSize +
            DefaultCorrelationIdSize +
            BitWorks.GetShortStringLength(this.ClientId, DefaultEncoding) +
            DefaultReplicaIdSize +
            DefaultMaxWaitSize +
            DefaultMinBytesSize +
            DefaultOffsetInfoSizeSize + this.OffsetInfo.Keys.Sum(x => BitWorks.GetShortStringLength(x, DefaultEncoding)) + this.OffsetInfo.Values.Select(pl => 4 + pl.Sum(p => p.SizeInBytes)).Sum());
 }
示例#5
0
        public void GetBytesValid()
        {
            const string topicName   = "topic";
            var          requestInfo = new Dictionary <string, List <PartitionOffsetRequestInfo> >();

            requestInfo[topicName] = new List <PartitionOffsetRequestInfo>()
            {
                new PartitionOffsetRequestInfo(0, OffsetRequest.LatestTime, 10)
            };
            var request = new OffsetRequest(requestInfo);

            // format = len(request) + requesttype + version + correlation id + client id + replica id + request info count + request infos
            int count = 2 + 2 + 4 + 2 + 4 + 4 + 4 +
                        BitWorks.GetShortStringLength("topic", AbstractRequest.DefaultEncoding) + 4 + 4 + 8 + 4;
            var ms = new MemoryStream();

            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);
            Assert.AreEqual(count, bytes.Length);

            var reader = new KafkaBinaryReader(ms);

            reader.ReadInt32().Should().Be(count - 4);                        // length
            reader.ReadInt16().Should().Be((short)RequestTypes.Offsets);      // request type
            reader.ReadInt16().Should().Be(0);                                // version
            reader.ReadInt32().Should().Be(0);                                // correlation id
            string.IsNullOrEmpty(reader.ReadShortString()).Should().BeTrue(); // client id
            reader.ReadInt32().Should().Be(-1);                               // replica id
            reader.ReadInt32().Should().Be(1);                                // request info count
            reader.ReadShortString().Should().Be("topic");
            reader.ReadInt32().Should().Be(1);                                // info count
            reader.ReadInt32().Should().Be(0);                                // partition id
            reader.ReadInt64().Should().Be(OffsetRequest.LatestTime);         // time
            reader.ReadInt32().Should().Be(10);                               // max offset
        }
示例#6
0
        public void GetBytesValidStructure()
        {
            string topicName     = "topic";
            int    correlationId = 1;
            string clientId      = "TestClient";
            int    maxWait       = 234;
            int    minBytes      = 345;

            var requestMap = new Dictionary <string, List <PartitionFetchInfo> >();

            requestMap[topicName] = new List <PartitionFetchInfo>()
            {
                new PartitionFetchInfo(2, 4000, 777)
            };


            var request = new FetchRequest(correlationId, clientId, maxWait, minBytes, requestMap);


            int requestSize = 4 +                                                                        //request size
                              2 +                                                                        //request type id
                              2 +                                                                        //versionId
                              4 +                                                                        //correlation id
                              BitWorks.GetShortStringLength(clientId, AbstractRequest.DefaultEncoding) + // client id length
                              4 +                                                                        //replica id
                              4 +                                                                        //max wait
                              4 +                                                                        //min bytes
                              4 +                                                                        //offset info count
                                                                                                         //=== offset info part
                              request.OffsetInfo.Keys.Sum(x => BitWorks.GetShortStringLength(x, AbstractRequest.DefaultEncoding)) +
                              request.OffsetInfo.Values.Select(pl => 4 + pl.Sum(p => p.SizeInBytes)).Sum();

            var ms = new MemoryStream();

            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);

            // add 4 bytes for the length of the message at the beginning
            Assert.AreEqual(requestSize, bytes.Length);

            // first 4 bytes = the message length
            Assert.AreEqual(requestSize - 4, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the request type
            Assert.AreEqual((short)RequestTypes.Fetch,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the version id
            Assert.AreEqual((short)FetchRequest.CurrentVersion,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the correlation id
            Assert.AreEqual(correlationId,
                            BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(8).Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the client id length
            Assert.AreEqual((short)clientId.Length,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(12).Take(2).ToArray <byte>()), 0));

            // next few bytes = the client id
            Assert.AreEqual(clientId, Encoding.ASCII.GetString(bytes.Skip(14).Take(clientId.Length).ToArray <byte>()));

            // next 4 bytes = replica id
            Assert.AreEqual(-1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(14 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = max wait
            Assert.AreEqual(maxWait,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(18 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = min bytes
            Assert.AreEqual(minBytes,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(22 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = offset info count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(26 + clientId.Length).Take(4).ToArray <byte>()), 0));

            //=== offset info part

            // next 2 bytes = the topic length
            Assert.AreEqual((short)topicName.Length,
                            BitConverter.ToInt16(
                                BitWorks.ReverseBytes(bytes.Skip(30 + clientId.Length).Take(2).ToArray <byte>()), 0));

            // next few bytes = the topic
            Assert.AreEqual(topicName,
                            Encoding.ASCII.GetString(
                                bytes.Skip(32 + clientId.Length).Take(topicName.Length).ToArray <byte>()));

            // next 4 bytes = partitions count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(32 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = partition
            Assert.AreEqual(2,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(36 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = offset
            Assert.AreEqual(4000,
                            BitConverter.ToInt64(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(40 + clientId.Length + topicName.Length).Take(8).ToArray <byte>()), 0));

            // next 4 bytes = fetch size
            Assert.AreEqual(777,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(48 + clientId.Length + topicName.Length).Take(8).ToArray <byte>()), 0));
        }