github.com/hack0072008/kafka-go@v1.0.1/produce_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hack0072008/kafka-go/compress"
    10  )
    11  
    12  func TestRequiredAcks(t *testing.T) {
    13  	for _, acks := range []RequiredAcks{
    14  		RequireNone,
    15  		RequireOne,
    16  		RequireAll,
    17  	} {
    18  		t.Run(acks.String(), func(t *testing.T) {
    19  			a := strconv.Itoa(int(acks))
    20  			x := RequiredAcks(-2)
    21  			y := RequiredAcks(-2)
    22  			b, err := acks.MarshalText()
    23  			if err != nil {
    24  				t.Fatal(err)
    25  			}
    26  
    27  			if err := x.UnmarshalText([]byte(a)); err != nil {
    28  				t.Fatal(err)
    29  			}
    30  			if err := y.UnmarshalText(b); err != nil {
    31  				t.Fatal(err)
    32  			}
    33  
    34  			if x != acks {
    35  				t.Errorf("required acks mismatch after marshal/unmarshal text: want=%s got=%s", acks, x)
    36  			}
    37  			if y != acks {
    38  				t.Errorf("required acks mismatch after marshal/unmarshal value: want=%s got=%s", acks, y)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func TestClientProduce(t *testing.T) {
    45  	client, topic, shutdown := newLocalClientAndTopic()
    46  	defer shutdown()
    47  
    48  	now := time.Now()
    49  
    50  	res, err := client.Produce(context.Background(), &ProduceRequest{
    51  		Topic:        topic,
    52  		Partition:    0,
    53  		RequiredAcks: -1,
    54  		Records: NewRecordReader(
    55  			Record{Time: now, Value: NewBytes([]byte(`hello-1`))},
    56  			Record{Time: now, Value: NewBytes([]byte(`hello-2`))},
    57  			Record{Time: now, Value: NewBytes([]byte(`hello-3`))},
    58  		),
    59  	})
    60  
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if res.Error != nil {
    66  		t.Error(res.Error)
    67  	}
    68  
    69  	for index, err := range res.RecordErrors {
    70  		t.Errorf("record at index %d produced an error: %v", index, err)
    71  	}
    72  }
    73  
    74  func TestClientProduceCompressed(t *testing.T) {
    75  	client, topic, shutdown := newLocalClientAndTopic()
    76  	defer shutdown()
    77  
    78  	now := time.Now()
    79  
    80  	res, err := client.Produce(context.Background(), &ProduceRequest{
    81  		Topic:        topic,
    82  		Partition:    0,
    83  		RequiredAcks: -1,
    84  		Compression:  compress.Gzip,
    85  		Records: NewRecordReader(
    86  			Record{Time: now, Value: NewBytes([]byte(`hello-1`))},
    87  			Record{Time: now, Value: NewBytes([]byte(`hello-2`))},
    88  			Record{Time: now, Value: NewBytes([]byte(`hello-3`))},
    89  		),
    90  	})
    91  
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	if res.Error != nil {
    97  		t.Error(res.Error)
    98  	}
    99  
   100  	for index, err := range res.RecordErrors {
   101  		t.Errorf("record at index %d produced an error: %v", index, err)
   102  	}
   103  }
   104  
   105  func TestClientProduceNilRecords(t *testing.T) {
   106  	client, topic, shutdown := newLocalClientAndTopic()
   107  	defer shutdown()
   108  
   109  	_, err := client.Produce(context.Background(), &ProduceRequest{
   110  		Topic:        topic,
   111  		Partition:    0,
   112  		RequiredAcks: -1,
   113  		Records:      nil,
   114  	})
   115  
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  }
   120  
   121  func TestClientProduceEmptyRecords(t *testing.T) {
   122  	client, topic, shutdown := newLocalClientAndTopic()
   123  	defer shutdown()
   124  
   125  	_, err := client.Produce(context.Background(), &ProduceRequest{
   126  		Topic:        topic,
   127  		Partition:    0,
   128  		RequiredAcks: -1,
   129  		Records:      NewRecordReader(),
   130  	})
   131  
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  }