Ejemplo n.º 1
0
    def test_encode_offset_commit_request(self):
        header = "".join([
            struct.pack('>i', 99),  # Total message length
            struct.pack('>h', 8),  # Message type = offset commit
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 42),  # Correlation ID
            struct.pack('>h9s', 9, "client_id"),  # The client ID
            struct.pack('>h8s', 8, "group_id"),  # The group to commit for
            struct.pack('>i', 2),  # Num topics
        ])

        topic1 = "".join([
            struct.pack(">h6s", 6, "topic1"),  # Topic for the request
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 0),  # Partition 0
            struct.pack(">q", 123),  # Offset 123
            struct.pack(">h", -1),  # Null metadata
            struct.pack(">i", 1),  # Partition 1
            struct.pack(">q", 234),  # Offset 234
            struct.pack(">h", -1),  # Null metadata
        ])

        topic2 = "".join([
            struct.pack(">h6s", 6, "topic2"),  # Topic for the request
            struct.pack(">i", 1),  # One partition
            struct.pack(">i", 2),  # Partition 2
            struct.pack(">q", 345),  # Offset 345
            struct.pack(">h", -1),  # Null metadata
        ])

        expected1 = "".join([header, topic1, topic2])
        expected2 = "".join([header, topic2, topic1])

        encoded = KafkaProtocol.encode_offset_commit_request(
            "client_id", 42, "group_id", [
                OffsetCommitRequest("topic1", 0, 123, None),
                OffsetCommitRequest("topic1", 1, 234, None),
                OffsetCommitRequest("topic2", 2, 345, None),
            ])

        self.assertIn(encoded, [expected1, expected2])
Ejemplo n.º 2
0
    def test_encode_offset_commit_request(self):
        header = b"".join([
            struct.pack('>i', 99),               # Total message length

            struct.pack('>h', 8),                # Message type = offset commit
            struct.pack('>h', 0),                # API version
            struct.pack('>i', 42),               # Correlation ID
            struct.pack('>h9s', 9, b"client_id"),# The client ID
            struct.pack('>h8s', 8, b"group_id"), # The group to commit for
            struct.pack('>i', 2),                # Num topics
        ])

        topic1 = b"".join([
            struct.pack(">h6s", 6, b"topic1"),   # Topic for the request
            struct.pack(">i", 2),                # Two partitions
            struct.pack(">i", 0),                # Partition 0
            struct.pack(">q", 123),              # Offset 123
            struct.pack(">h", -1),               # Null metadata
            struct.pack(">i", 1),                # Partition 1
            struct.pack(">q", 234),              # Offset 234
            struct.pack(">h", -1),               # Null metadata
        ])

        topic2 = b"".join([
            struct.pack(">h6s", 6, b"topic2"),   # Topic for the request
            struct.pack(">i", 1),                # One partition
            struct.pack(">i", 2),                # Partition 2
            struct.pack(">q", 345),              # Offset 345
            struct.pack(">h", -1),               # Null metadata
        ])

        expected1 = b"".join([ header, topic1, topic2 ])
        expected2 = b"".join([ header, topic2, topic1 ])

        encoded = KafkaProtocol.encode_offset_commit_request(b"client_id", 42, b"group_id", [
            OffsetCommitRequest(b"topic1", 0, 123, None),
            OffsetCommitRequest(b"topic1", 1, 234, None),
            OffsetCommitRequest(b"topic2", 2, 345, None),
        ])

        self.assertIn(encoded, [ expected1, expected2 ])