Ejemplo n.º 1
0
        public static DescribeGroupsResponse FromBytes(IRequestContext context, ArraySegment <byte> bytes)
        {
            using (var reader = new KafkaReader(bytes)) {
                var groups = new Group[reader.ReadInt32()];
                for (var g = 0; g < groups.Length; g++)
                {
                    var errorCode    = (ErrorCode)reader.ReadInt16();
                    var groupId      = reader.ReadString();
                    var state        = reader.ReadString();
                    var protocolType = reader.ReadString();
                    var protocol     = reader.ReadString();

                    IMembershipEncoder encoder = null;
                    var members = new Member[reader.ReadInt32()];
                    for (var m = 0; m < members.Length; m++)
                    {
                        encoder = encoder ?? context.GetEncoder(protocolType);
                        var memberId         = reader.ReadString();
                        var clientId         = reader.ReadString();
                        var clientHost       = reader.ReadString();
                        var memberMetadata   = encoder.DecodeMetadata(protocol, reader);
                        var memberAssignment = encoder.DecodeAssignment(reader);
                        members[m] = new Member(memberId, clientId, clientHost, memberMetadata, memberAssignment);
                    }
                    groups[g] = new Group(errorCode, groupId, state, protocolType, protocol, members);
                }

                return(new DescribeGroupsResponse(groups));
            }
        }
Ejemplo n.º 2
0
        public static void AssertCanEncodeDecodeResponse <T>(this T response, short version, IMembershipEncoder encoder = null, T forComparison = null) where T : class, IResponse
        {
            var encoders = ImmutableDictionary <string, IMembershipEncoder> .Empty;

            if (encoder != null)
            {
                encoders = encoders.Add(encoder.ProtocolType, encoder);
            }

            var context = new RequestContext(16, version, "Test-Response", encoders, encoder?.ProtocolType);
            var data    = KafkaDecoder.EncodeResponseBytes(context, response);
            var decoded = GetType <T>().ToResponse(context, data.Skip(Request.IntegerByteSize + Request.CorrelationSize));

            if (forComparison == null)
            {
                forComparison = response;
            }
            Assert.That(forComparison.GetHashCode(), Is.EqualTo(decoded.GetHashCode()), "HashCode equality");
            var original = forComparison.ToString();
            var final    = decoded.ToString();

            Assert.That(original, Is.EqualTo(final), "ToString equality");
            Assert.That(decoded.Equals(final), Is.False);  // general test for equality
            Assert.That(decoded.Equals(decoded), Is.True); // general equality test for sanity
            Assert.That(forComparison.Equals(decoded), $"Original\n{original}\nFinal\n{final}");
            Assert.That(forComparison.Errors.HasEqualElementsInOrder(decoded.Errors), "Errors");
        }
Ejemplo n.º 3
0
        public static void AssertCanEncodeDecodeRequest <T>(this T request, short version, IMembershipEncoder encoder = null, T forComparison = null) where T : class, IRequest
        {
            var encoders = ImmutableDictionary <string, IMembershipEncoder> .Empty;

            if (encoder != null)
            {
                encoders = encoders.Add(encoder.ProtocolType, encoder);
            }

            var context = new RequestContext(17, version, "Test-Request", encoders, encoder?.ProtocolType);
            var bytes   = request.ToBytes(context);
            var decoded = KafkaDecoder.Decode <T>(bytes.Skip(4), context);

            if (forComparison == null)
            {
                forComparison = request;
            }
            Assert.That(forComparison.GetHashCode(), Is.EqualTo(decoded.GetHashCode()), "HashCode equality");
            Assert.That(forComparison.ShortString(), Is.EqualTo(decoded.ShortString()), "ShortString equality");
            var original = forComparison.ToString();
            var final    = decoded.ToString();

            Assert.That(original, Is.EqualTo(final), "ToString equality");
            Assert.That(decoded.Equals(final), Is.False);  // general equality test for sanity
            Assert.That(decoded.Equals(decoded), Is.True); // general equality test for sanity
            Assert.That(forComparison.Equals(decoded), $"Original\n{original}\nFinal\n{final}");
        }
Ejemplo n.º 4
0
 public static IKafkaWriter Write(this IKafkaWriter writer, IMemberAssignment assignment, IMembershipEncoder encoder)
 {
     encoder.EncodeAssignment(writer, assignment);
     return(writer);
 }
Ejemplo n.º 5
0
 public static IKafkaWriter Write(this IKafkaWriter writer, IMemberMetadata metadata, IMembershipEncoder encoder)
 {
     encoder.EncodeMetadata(writer, metadata);
     return(writer);
 }