github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/protocol/produce/produce_test.go (about)

     1  package produce_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/segmentio/kafka-go/protocol"
     8  	"github.com/segmentio/kafka-go/protocol/produce"
     9  	"github.com/segmentio/kafka-go/protocol/prototest"
    10  )
    11  
    12  const (
    13  	v0 = 0
    14  	v3 = 3
    15  	v5 = 5
    16  	v8 = 8
    17  )
    18  
    19  func TestProduceRequest(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.TestRequest(t, v0, &produce.Request{
    25  		Acks:    1,
    26  		Timeout: 500,
    27  		Topics: []produce.RequestTopic{
    28  			{
    29  				Topic: "topic-1",
    30  				Partitions: []produce.RequestPartition{
    31  					{
    32  						Partition: 0,
    33  						RecordSet: protocol.RecordSet{
    34  							Version: 1,
    35  							Records: protocol.NewRecordReader(
    36  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil},
    37  							),
    38  						},
    39  					},
    40  					{
    41  						Partition: 1,
    42  						RecordSet: protocol.RecordSet{
    43  							Version: 1,
    44  							Records: protocol.NewRecordReader(
    45  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
    46  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
    47  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
    48  							),
    49  						},
    50  					},
    51  				},
    52  			},
    53  
    54  			{
    55  				Topic: "topic-2",
    56  				Partitions: []produce.RequestPartition{
    57  					{
    58  						Partition: 0,
    59  						RecordSet: protocol.RecordSet{
    60  							Version:    1,
    61  							Attributes: protocol.Gzip,
    62  							Records: protocol.NewRecordReader(
    63  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
    64  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
    65  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
    66  							),
    67  						},
    68  					},
    69  				},
    70  			},
    71  		},
    72  	})
    73  
    74  	prototest.TestRequest(t, v3, &produce.Request{
    75  		TransactionalID: "1234",
    76  		Acks:            1,
    77  		Timeout:         500,
    78  		Topics: []produce.RequestTopic{
    79  			{
    80  				Topic: "topic-1",
    81  				Partitions: []produce.RequestPartition{
    82  					{
    83  						Partition: 0,
    84  						RecordSet: protocol.RecordSet{
    85  							Version: 1,
    86  							Records: protocol.NewRecordReader(
    87  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil},
    88  							),
    89  						},
    90  					},
    91  					{
    92  						Partition: 1,
    93  						RecordSet: protocol.RecordSet{
    94  							Version: 1,
    95  							Records: protocol.NewRecordReader(
    96  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
    97  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
    98  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
    99  							),
   100  						},
   101  					},
   102  				},
   103  			},
   104  		},
   105  	})
   106  
   107  	headers := []protocol.Header{
   108  		{Key: "key-1", Value: []byte("value-1")},
   109  		{Key: "key-2", Value: []byte("value-2")},
   110  		{Key: "key-3", Value: []byte("value-3")},
   111  	}
   112  
   113  	prototest.TestRequest(t, v5, &produce.Request{
   114  		TransactionalID: "1234",
   115  		Acks:            1,
   116  		Timeout:         500,
   117  		Topics: []produce.RequestTopic{
   118  			{
   119  				Topic: "topic-1",
   120  				Partitions: []produce.RequestPartition{
   121  					{
   122  						Partition: 1,
   123  						RecordSet: protocol.RecordSet{
   124  							Version: 2,
   125  							Records: protocol.NewRecordReader(
   126  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers},
   127  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   128  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   129  							),
   130  						},
   131  					},
   132  				},
   133  			},
   134  
   135  			{
   136  				Topic: "topic-2",
   137  				Partitions: []produce.RequestPartition{
   138  					{
   139  						Partition: 1,
   140  						RecordSet: protocol.RecordSet{
   141  							Version:    2,
   142  							Attributes: protocol.Snappy,
   143  							Records: protocol.NewRecordReader(
   144  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers},
   145  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   146  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   147  							),
   148  						},
   149  					},
   150  				},
   151  			},
   152  		},
   153  	})
   154  }
   155  
   156  func TestProduceResponse(t *testing.T) {
   157  	prototest.TestResponse(t, v0, &produce.Response{
   158  		Topics: []produce.ResponseTopic{
   159  			{
   160  				Topic: "topic-1",
   161  				Partitions: []produce.ResponsePartition{
   162  					{
   163  						Partition:  0,
   164  						ErrorCode:  0,
   165  						BaseOffset: 0,
   166  					},
   167  					{
   168  						Partition:  1,
   169  						ErrorCode:  0,
   170  						BaseOffset: 42,
   171  					},
   172  				},
   173  			},
   174  		},
   175  	})
   176  
   177  	prototest.TestResponse(t, v8, &produce.Response{
   178  		Topics: []produce.ResponseTopic{
   179  			{
   180  				Topic: "topic-1",
   181  				Partitions: []produce.ResponsePartition{
   182  					{
   183  						Partition:      0,
   184  						ErrorCode:      0,
   185  						BaseOffset:     42,
   186  						LogAppendTime:  1e9,
   187  						LogStartOffset: 10,
   188  						RecordErrors:   []produce.ResponseError{},
   189  					},
   190  					{
   191  						Partition: 1,
   192  						ErrorCode: 1,
   193  						RecordErrors: []produce.ResponseError{
   194  							{BatchIndex: 1, BatchIndexErrorMessage: "message-1"},
   195  							{BatchIndex: 2, BatchIndexErrorMessage: "message-2"},
   196  							{BatchIndex: 3, BatchIndexErrorMessage: "message-3"},
   197  						},
   198  						ErrorMessage: "something went wrong",
   199  					},
   200  				},
   201  			},
   202  		},
   203  	})
   204  }
   205  
   206  func BenchmarkProduceRequest(b *testing.B) {
   207  	t0 := time.Now().Truncate(time.Millisecond)
   208  	t1 := t0.Add(1 * time.Millisecond)
   209  	t2 := t0.Add(2 * time.Millisecond)
   210  
   211  	prototest.BenchmarkRequest(b, v3, &produce.Request{
   212  		TransactionalID: "1234",
   213  		Acks:            1,
   214  		Timeout:         500,
   215  		Topics: []produce.RequestTopic{
   216  			{
   217  				Topic: "topic-1",
   218  				Partitions: []produce.RequestPartition{
   219  					{
   220  						Partition: 0,
   221  						RecordSet: protocol.RecordSet{
   222  							Version: 1,
   223  							Records: protocol.NewRecordReader(
   224  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: nil},
   225  							),
   226  						},
   227  					},
   228  					{
   229  						Partition: 1,
   230  						RecordSet: protocol.RecordSet{
   231  							Version: 1,
   232  							Records: protocol.NewRecordReader(
   233  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
   234  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   235  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   236  							),
   237  						},
   238  					},
   239  				},
   240  			},
   241  		},
   242  	})
   243  
   244  	headers := []protocol.Header{
   245  		{Key: "key-1", Value: []byte("value-1")},
   246  		{Key: "key-2", Value: []byte("value-2")},
   247  		{Key: "key-3", Value: []byte("value-3")},
   248  	}
   249  
   250  	prototest.BenchmarkRequest(b, v5, &produce.Request{
   251  		TransactionalID: "1234",
   252  		Acks:            1,
   253  		Timeout:         500,
   254  		Topics: []produce.RequestTopic{
   255  			{
   256  				Topic: "topic-1",
   257  				Partitions: []produce.RequestPartition{
   258  					{
   259  						Partition: 1,
   260  						RecordSet: protocol.RecordSet{
   261  							Version: 2,
   262  							Records: protocol.NewRecordReader(
   263  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers},
   264  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   265  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   266  							),
   267  						},
   268  					},
   269  				},
   270  			},
   271  		},
   272  	})
   273  }