github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/rawproduce_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol"
    10  	ktesting "github.com/segmentio/kafka-go/testing"
    11  )
    12  
    13  func TestClientRawProduce(t *testing.T) {
    14  	// The RawProduce request records are encoded in the format introduced in Kafka 0.11.0.
    15  	if !ktesting.KafkaIsAtLeast("0.11.0") {
    16  		t.Skip("Skipping because the RawProduce request is not supported by Kafka versions below 0.11.0")
    17  	}
    18  
    19  	client, topic, shutdown := newLocalClientAndTopic()
    20  	defer shutdown()
    21  
    22  	now := time.Now()
    23  
    24  	res, err := client.RawProduce(context.Background(), &RawProduceRequest{
    25  		Topic:        topic,
    26  		Partition:    0,
    27  		RequiredAcks: -1,
    28  		RawRecords: NewRawRecordSet(NewRecordReader(
    29  			Record{Time: now, Value: NewBytes([]byte(`hello-1`))},
    30  			Record{Time: now, Value: NewBytes([]byte(`hello-2`))},
    31  			Record{Time: now, Value: NewBytes([]byte(`hello-3`))},
    32  		), 0),
    33  	})
    34  
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	if res.Error != nil {
    40  		t.Error(res.Error)
    41  	}
    42  
    43  	for index, err := range res.RecordErrors {
    44  		t.Errorf("record at index %d produced an error: %v", index, err)
    45  	}
    46  }
    47  
    48  func TestClientRawProduceCompressed(t *testing.T) {
    49  	// The RawProduce request records are encoded in the format introduced in Kafka 0.11.0.
    50  	if !ktesting.KafkaIsAtLeast("0.11.0") {
    51  		t.Skip("Skipping because the RawProduce request is not supported by Kafka versions below 0.11.0")
    52  	}
    53  
    54  	client, topic, shutdown := newLocalClientAndTopic()
    55  	defer shutdown()
    56  
    57  	now := time.Now()
    58  
    59  	res, err := client.RawProduce(context.Background(), &RawProduceRequest{
    60  		Topic:        topic,
    61  		Partition:    0,
    62  		RequiredAcks: -1,
    63  		RawRecords: NewRawRecordSet(NewRecordReader(
    64  			Record{Time: now, Value: NewBytes([]byte(`hello-1`))},
    65  			Record{Time: now, Value: NewBytes([]byte(`hello-2`))},
    66  			Record{Time: now, Value: NewBytes([]byte(`hello-3`))},
    67  		), protocol.Gzip),
    68  	})
    69  
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	if res.Error != nil {
    75  		t.Error(res.Error)
    76  	}
    77  
    78  	for index, err := range res.RecordErrors {
    79  		t.Errorf("record at index %d produced an error: %v", index, err)
    80  	}
    81  }
    82  
    83  func TestClientRawProduceNilRecords(t *testing.T) {
    84  	client, topic, shutdown := newLocalClientAndTopic()
    85  	defer shutdown()
    86  
    87  	_, err := client.RawProduce(context.Background(), &RawProduceRequest{
    88  		Topic:        topic,
    89  		Partition:    0,
    90  		RequiredAcks: -1,
    91  		RawRecords:   protocol.RawRecordSet{Reader: nil},
    92  	})
    93  
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  }
    98  
    99  func TestClientRawProduceEmptyRecords(t *testing.T) {
   100  	client, topic, shutdown := newLocalClientAndTopic()
   101  	defer shutdown()
   102  
   103  	_, err := client.Produce(context.Background(), &ProduceRequest{
   104  		Topic:        topic,
   105  		Partition:    0,
   106  		RequiredAcks: -1,
   107  		Records:      NewRecordReader(),
   108  	})
   109  
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  }
   114  
   115  func NewRawRecordSet(reader protocol.RecordReader, attr protocol.Attributes) protocol.RawRecordSet {
   116  	rs := protocol.RecordSet{Version: 2, Attributes: attr, Records: reader}
   117  	buf := &bytes.Buffer{}
   118  	rs.WriteTo(buf)
   119  
   120  	return protocol.RawRecordSet{
   121  		Reader: buf,
   122  	}
   123  }