github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/rawproduce/rawproduce_test.go (about) 1 package rawproduce_test 2 3 import ( 4 "bytes" 5 "testing" 6 "time" 7 8 "github.com/segmentio/kafka-go/protocol" 9 "github.com/segmentio/kafka-go/protocol/prototest" 10 "github.com/segmentio/kafka-go/protocol/rawproduce" 11 ) 12 13 const ( 14 v0 = 0 15 v3 = 3 16 v5 = 5 17 ) 18 19 func TestRawProduceRequest(t *testing.T) { 20 t0 := time.Now().Truncate(time.Millisecond) 21 t1 := t0.Add(1 * time.Millisecond) 22 t2 := t0.Add(2 * time.Millisecond) 23 24 prototest.TestRequestWithOverride(t, v0, &rawproduce.Request{ 25 Acks: 1, 26 Timeout: 500, 27 Topics: []rawproduce.RequestTopic{ 28 { 29 Topic: "topic-1", 30 Partitions: []rawproduce.RequestPartition{ 31 { 32 Partition: 0, 33 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 34 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, 35 ), 1, 0), 36 }, 37 { 38 Partition: 1, 39 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 40 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, 41 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 42 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 43 ), 1, 0), 44 }, 45 }, 46 }, 47 48 { 49 Topic: "topic-2", 50 Partitions: []rawproduce.RequestPartition{ 51 { 52 Partition: 0, 53 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 54 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, 55 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 56 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 57 ), 1, protocol.Gzip), 58 }, 59 }, 60 }, 61 }, 62 }) 63 64 prototest.TestRequestWithOverride(t, v3, &rawproduce.Request{ 65 TransactionalID: "1234", 66 Acks: 1, 67 Timeout: 500, 68 Topics: []rawproduce.RequestTopic{ 69 { 70 Topic: "topic-1", 71 Partitions: []rawproduce.RequestPartition{ 72 { 73 Partition: 0, 74 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 75 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, 76 ), 1, 0), 77 }, 78 { 79 Partition: 1, 80 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 81 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, 82 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 83 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 84 ), 1, 0), 85 }, 86 }, 87 }, 88 }, 89 }) 90 91 headers := []protocol.Header{ 92 {Key: "key-1", Value: []byte("value-1")}, 93 {Key: "key-2", Value: []byte("value-2")}, 94 {Key: "key-3", Value: []byte("value-3")}, 95 } 96 97 prototest.TestRequestWithOverride(t, v5, &rawproduce.Request{ 98 TransactionalID: "1234", 99 Acks: 1, 100 Timeout: 500, 101 Topics: []rawproduce.RequestTopic{ 102 { 103 Topic: "topic-1", 104 Partitions: []rawproduce.RequestPartition{ 105 { 106 Partition: 1, 107 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 108 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, 109 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 110 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 111 ), 2, 0), 112 }, 113 }, 114 }, 115 116 { 117 Topic: "topic-2", 118 Partitions: []rawproduce.RequestPartition{ 119 { 120 Partition: 1, 121 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 122 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, 123 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 124 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 125 ), 2, protocol.Snappy), 126 }, 127 }, 128 }, 129 }, 130 }) 131 } 132 133 func NewRawRecordSet(reader protocol.RecordReader, version int8, attr protocol.Attributes) protocol.RawRecordSet { 134 rs := protocol.RecordSet{Version: version, Attributes: attr, Records: reader} 135 buf := &bytes.Buffer{} 136 rs.WriteTo(buf) 137 138 return protocol.RawRecordSet{ 139 Reader: buf, 140 } 141 } 142 143 func BenchmarkProduceRequest(b *testing.B) { 144 t0 := time.Now().Truncate(time.Millisecond) 145 t1 := t0.Add(1 * time.Millisecond) 146 t2 := t0.Add(2 * time.Millisecond) 147 148 prototest.BenchmarkRequest(b, v3, &rawproduce.Request{ 149 TransactionalID: "1234", 150 Acks: 1, 151 Timeout: 500, 152 Topics: []rawproduce.RequestTopic{ 153 { 154 Topic: "topic-1", 155 Partitions: []rawproduce.RequestPartition{ 156 { 157 Partition: 0, 158 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 159 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil}, 160 ), 1, 0), 161 }, 162 { 163 Partition: 1, 164 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 165 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")}, 166 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 167 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 168 ), 1, 0), 169 }, 170 }, 171 }, 172 }, 173 }) 174 175 headers := []protocol.Header{ 176 {Key: "key-1", Value: []byte("value-1")}, 177 {Key: "key-2", Value: []byte("value-2")}, 178 {Key: "key-3", Value: []byte("value-3")}, 179 } 180 181 prototest.BenchmarkRequest(b, v5, &rawproduce.Request{ 182 TransactionalID: "1234", 183 Acks: 1, 184 Timeout: 500, 185 Topics: []rawproduce.RequestTopic{ 186 { 187 Topic: "topic-1", 188 Partitions: []rawproduce.RequestPartition{ 189 { 190 Partition: 1, 191 RecordSet: NewRawRecordSet(protocol.NewRecordReader( 192 protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers}, 193 protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")}, 194 protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")}, 195 ), 2, 0), 196 }, 197 }, 198 }, 199 }, 200 }) 201 }